1

我的盒子上有一个非常大的日志文件(9GB——我知道我需要修复它)。我需要拆分成块,以便将其上传到亚马逊 S3 进行备份。S3 的最大文件大小为 5GB。所以我想把它分成几个块,然后上传每个块。

这就是问题所在,我的服务器上只有 5GB 可用空间,所以我不能只进行简单的 unix 拆分。这是我想做的事情:

  1. 抓取日志文件的前 4GB 并吐出到一个单独的文件中(称为段 1)
  2. 将该段 1 上传到 s3。
  3. rm segment1 释放空间。
  4. 从日志文件中获取中间的 4GB 并上传到 s3。像以前一样清理
  5. 获取剩余的 1GB 并上传到 S3。

我找不到用偏移量分割的正确 unix 命令。Split 只做等量的事情,而 csplit 似乎也没有我需要的东西。有什么建议吗?

4

4 回答 4

3

一个(复杂的)解决方案是先压缩它。文本日志文件应该很容易从 9G 降到远低于 5G,然后删除原始文件,为您提供 9G 的可用空间。

然后直接通过管道传输该压缩文件,split以免占用更多磁盘空间。你最终会得到一个压缩文件和三个要上传的文件。

上传它们,然后删除它们,然后解压缩原始日志。

=====

一个更好的解决方案是只计算行数(比如 300 万行)并使用 awk 脚本来提取和发送各个部分:

awk '1,1000000 {print}' biglogfile > bit1
# send and delete bit1

awk '1000001,2000000 {print}' biglogfile > bit2
# send and delete bit2

awk '2000001,3000000 {print}' biglogfile > bit3
# send and delete bit3

bit1然后,在另一端,您可以bit3单独处理或重新组合它们:

mv bit1 whole
cat bit2 >>whole ; rm bit2
cat bit3 >>whole ; rm bit3

当然,这种拆分可以使用 Unix 中的任何标准文本处理工具来完成:perl, python, awk,head/tail组合。这取决于您对什么感到满意。

于 2009-05-09T02:10:30.707 回答
2

首先, gzip -9 你的日志文件。

然后,编写一个小的 shell 脚本来使用 dd:

#!/bin/env sh

chunk_size = 2048 * 1048576; #gigs in megabytes
input_file = shift;    

len = `stat '%s' $input_file`
chunks = $(($len/$chunk_size + 1))

for i in {0...$chunks}
do
  dd if=$input_file skip=$i of=$input_file.part count=1 bs=$chunk_size
  scp $input_file.part servername:path/$input_file.part.$i
done

我只是把它从我的头顶上掉下来,所以我不知道它是否会在没有修改的情况下工作,但你需要的是与此非常相似的东西。

于 2009-05-09T02:13:56.940 回答
2

你可以使用dd。您需要在每个块中指定 bs(内存缓冲区大小)、skip(要跳过的缓冲区数)和 count(要复制的缓冲区数)。

因此,使用 10Meg 的缓冲区大小,您将执行以下操作:

# For the first 4Gig
dd if=myfile.log bs=10M skip=0 count=400 of=part1.logbit
<upload part1.logbit and remove it>
# For the second 4Gig
dd if=myfile.log bs=10M skip=400 count=400 of=part2.logbit
...

您还可以从压缩要传输的数据中受益:

dd if=myfile.log bs=10M skip=800 count=400 | gzip -c > part3.logbit.gz

可能有更友好的方法。

dd has some real shortcomings. If you use a small buffer size, it runs much more slowly. But you can only skip/seek in the file by multiples of bs. So if you want to start reading data from a prime offset, you're in a real fiddle. Anyway I digress.

于 2009-05-09T02:14:48.433 回答
0

Coreutils split creates equal sized output sections, excepting for the last section.

split --bytes=4GM bigfile chunks
于 2009-05-09T02:16:23.390 回答