Full and differential backup with 7zip on Linux

Full and differential backup with 7zip on Linux

Backups are a must! I’ve said this time and time again and I’ll say it every time backup are the topic: One can never have enough backups.

There are a lot of advanced / dedicated backup programs out there, but for the simple task sometimes simpler programs are simpler… Here is where 7zip (a powerful multi-platform archiver) comes in – readily available for the Linux platform via the p7zip package (on RHEL-compatible distros).

I’m using 7zip to create full monthly backups and differential daily backups. Differential backups are good because they only hold the differences from the target existing archive. But you don’t want to rely entirely on differential backups as you’ll also need to keep the first ever complete backup (which may become very old very fast).

Assuming you want to backup you www folder, the following script should to a very good job at it:

#!/bin/bash
# www nightly backup
# last update 20141016
#

TODAY=`(set \`date +%Y%m%d\`; echo $1)`
MONTH=`(set \`date +%Y%m\`; echo $1)`

DIRR="/backup/nightly"
echo "Starting backup...." > log.
echo " "

# www backup
if [ ! -f $DIRR/www-$MONTH.7z ];
then
echo "www - creating monthly backup."
7za a $DIRR/www-$MONTH.7z /var/www -xr!www/domains/somedomain.tld/somebigfolder -xr!www/domains/anotherdomain.tld
else
echo "www - creating nightly backup."
7za u $DIRR/www-$MONTH /var/www -ms=off -t7z -u- -up0q3r2x2y2z0w2!$DIRR/www-$TODAY.7z -xr!www/domains/somedomain.tld/somebigfolder -xr!www/domains/anotherdomain.tld
fi

echo
echo "Done!"

where,

  • DIRR is the backup folder
  • /var/www is the www folder to be backup up
  • -xr!www[...]somebigfolder and -xr!www[...]/anotherdomain.tld are exclusion patterns; somebigfolder and anotherdomain.tld (and all their contents) will be excluded from the backup

The script creates 2 separate archive sets:

  • a full monthly backup, named www-YYYYMM.7z
  • a daily (nightly) differential backup, named www-YYYYMMDD.7z, which will contain just the changes since the previous monthly archive was done; if you run it every day, it will create one monthly archive at the beginning of the month and a differential one each day after.

A plus of the script is that it creates the full monthly archive the first time it is run that month, so you can schedule to run at various time periods. To restore to a particular date, you’ll need the monthly archive plus the one from the day you want to restore to.

WARNING: 7zip does not save filesystem specific information like permissions and ownership. Do not use this script and 7zip to perform system or mission critical backups.

Source: (1).

 

 

Leave a Reply