如何在使用tar时设置bzip2的块大小?

我正在使用tar来备份一个linux服务器到磁带。 我正在使用-j选项来压缩文件与bzip2 ,但我看不到一种方法来调整从tar tar bzip2的块大小选项。 默认的块大小是900,000字节,它给出了最好的压缩,但是是最慢的。 我并不担心压缩比,所以我希望使bzip2以更小的块大小运行得更快。

 export BZIP=--fast tar cjf foo.tar.bz2 foo 

或者将tar的输出输出到bzip2 。

虽然你应该从bzip2手册中注意到:

     -1(或 -  fast)到-9(或 -  best)
              压缩时将块大小设置为100 k,200 k .. 900 k。
              解压缩时无效。 请参阅下面的内存pipe理。
               --fast和--best别名主要用于GNU gzip compat-
               ibility。 特别是,fast不会使事物变得显着,
              快得多。 而且 - 只是select默认行为。
 tar -cjf dir.tar.bz2 --options bzip2:compression-level=9 path/to/dir/ 

bzip2块大小

bzip2有一些块大小选项。 从手册页bzip2(1) :

 -1 (or --fast) to -9 (or --best) Set the block size to 100 k, 200 k .. 900 k when compressing. Has no effect when decompressing. See MEMORY MANAGEMENT below. The --fast and --best aliases are primarily for GNU gzip compatibility. In particular, --fast doesn't make things significantly faster. And --best merely selects the default behaviour. 

由于您希望使用bzip2来压缩比率更低,所以您似乎需要-1 (或--fast )选项。

使用tar时设置bzip2块大小

在使用tar时,可以设置bzip2块大小。

UNlX的方式

我最喜欢的方式,UNlX的方式,是你独立使用每一个工具,并通过pipe道结合他们。

 $ tar --create [FILE...] | bzip2 -1 > [ARCHIVE].tar.bz2 

你可以这样读:“用tar创build.tar – >用bzip2 – >把它写到[ARCHIVE].tar.bz2 ”。

环境variables

也可以通过环境variablesBZIP2设置bzip2选项。 从手册页bzip2(1) :

 bzip2 will read arguments from the environment variables BZIP2 and BZIP, in that order, and will process them before any arguments read from the command line. This gives a convenient way to supply default arguments. 

所以要用tar来使用,例如可以这样做:

 $ BZIP2=-1 tar --create --bzip2 --file [ARCHIVE].tar.bz2 [FILE...] 

更快的select

bzip2使用慢速压缩algorithm。 如果您担心速度问题,可以研究其他algorithm,例如gzip或lzop使用的algorithm。 这里是一个很好的文章比较压缩工具: https : //aliver.wordpress.com/2010/06/22/huge-unix-file-compresser-shootout-with-tons-of-datagraphs/

将tar输出发送到stdout ,然后分别通过bzip2pipe道:

 % tar cvf - _file_ | bzip2 _opts_ > output.tar.bz2 

它更容易:

 % tar -cvf dir.tar path/to/dir/ && bzip2 -9 dir.tar