Bash script for incremental backups
Replace in the scripts the references to:
DIR
: the directory we want to backup/path/to/dir
: the path to the directory CONTAININGDIR
/path/to/workspace
: the path to a directory that we use temporarily to host the tar/snar/backup
: a directory where the backups are located, probably a storage server mounted in a local directoryDATE
: a variable that I declare to reference the date and use it in file names (no need to change it)
First run the script full-back-up.sh by hand to initialize the files incremental and snapshot.
These scripts are mainly used to save states of a directory incrementally. In other words, we can build these scripts to make a backup of a site that we have published without having to copy all the files, only the ones that have changed. Example of reference variables:
DIR
: /var/www/mysite/path/to/dir
: /var/www/path/to/workspace
: /var/backups/website/backup
: /mount/backupserverNFS
For these scripts to make sense, they must be put in the cron.
You have to be careful that the account that generates the crontab has read/write permissions on the directories corresponding.
Then, we do not want to run the full backup very often, in my case it is every 10 days at 3AM (or, as they say, every time the day of the month is a multiple of 10). Ex:
0 3 */10 * * /path/to/full-backup.sh
And for the incremental to run every day at 4AM:
0 4 * * * /path/to/incremental-backup.sh
It is important that you run the full-back-up.sh by hand the first time (not wait for the cron to do it).
The tar incremental can be used in 2 ways:
- Creating direct incrementals on a full back up (simple)
- Creating incremental levels (less simple)
In the first case, an incremental is done using the --listed-incremental
parameter and a file of
snapshot (.snar) that is responsible for comparing and defining the differences to include in the incremental. To restore
DIR
it will suffice to extract the full backup and then the date to be restored.
tar -xzpf DIR-20130720-full.tar.gz tar -xzpf DIR-20130724-incremental.tar.gz
In the second, the incremental is always done over another incremental. While this form gives more control over
the incremental and the file size, when you want to restore DIR
it will have to be done sequentially
How are incrementals made? Example considering that we have 4 incrementals:
tar -xzpf DIR-20130720-full.tar.gz tar -xzpf DIR-20130721-incremental.tar.gz tar -xzpf DIR-20130722-incremental.tar.gz tar -xzpf DIR-20130723-incremental.tar.gz tar -xzpf DIR-20130724-incremental.tar.gz