Switch to bzip2 compression on my server backup scripts

Previously I used gzip as compression tool to compress the web site files and mysql database.  It is still working on my Linode server. I learned something from the compairasion from this post .

So I tried to use bzip instead of gzip on my backup script.

Here is two examples.

1) Mysqldump file, which is a text sql file.

The original sql file is 52,317KB

  • gzip with level 9 (best) compression:  23,227KB  – Compress Ratio 44.39%
  • bzip2 with default (I think it is level 9) compression: 19,666KB – Compress Ratio 37.59%

Bzip2 is smaller than gzip file. It reduced about 15%.

2) Website files, including php, jpeg, png, css, any files used in web site.

The original tar gile is 187,670KB

  • gzip compression: 42,101KB – Ratio 22.43%
  • bzip2 compression: 34,753KB – Ratio 18.51%

Bzip2 is better. Backup file size is reduced by about 17%.

The following screenshot is the second example.

bz2-vs-gz

  • sites.tar is the original file.
  • sites.tar.gz is compressed by gzip.
  • sites.bz2 is compressed by bzip2.

Because I am using it as a backup tool, the space occuppied is very important. To save the disk space, I chose bzip2 to replace gzip.

 

So my mysql database backup script is changed as below:

</p>
<p>#!/bin/bash<br />
### MySQL Server Login Info ###<br />
MUSER=&quot;MySQL User name&quot;<br />
MPASS=&quot;MySQL Pass&quot;<br />
MHOST=&quot;localhost&quot;<br />
MYSQL=&quot;$(which mysql)&quot;<br />
MYSQLDUMP=&quot;$(which mysqldump)&quot;<br />
BAK=&quot;/home/usera/backup/mysql&quot;<br />
GZIP=&quot;$(which gzip)&quot;<br />
BZIP2=&quot;$(which bzip2)&quot;<br />
### FTP SERVER Login info ###<br />
FTPU=&quot;FTP User name&quot;<br />
FTPP=&quot;FTP Pass&quot;<br />
FTPS=&quot;backup.dreamhost.com&quot;<br />
NOW=$(date +&quot;%d-%m-%Y&quot;)<br />
[ ! -d $BAK ] &amp;&amp; mkdir -p $BAK || /bin/rm -f $BAK/*<br />
DBS=&quot;$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')&quot;<br />
for db in $DBS<br />
do<br />
FILE=$BAK/$db.$NOW-$(date +&quot;%T&quot;).gz<br />
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $BZIP2&gt; $FILE<br />
done<br />
lftp -u $FTPU,$FTPP -e &quot;mkdir /vps/backup/mysql/$NOW;cd /vps/backup/mysql/$NOW; mput /home/usera/backup/mysql/*; quit&quot; $FTPS<br />
find /home/usera/backup/mysql -ctime +4 -exec rm {} \;</p>
<p>