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:


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