In this section we shall examine how to perform local backups using the rsnapshot application. As an example we shall be creating a daily backup of our system configuration (all files located under /etc/) and a weekly backup of the list of installed packages (all files located under /var/lib/portage/) which we shall then merge to create a single unified backup which we shall retain using a monthly rotation scheme.
The first step is to create a common configuration file for all the backup rotations for the localhost backup target. This file will simply include the base rsnapshot configuration file and set the snapshot_root for this backup target. This file will then be included in all other configurations for this target.
# Include the base configuration file.
include_conf /etc/rsnapshot/rsnapshot.base
# Set the snapshot_root for this host
snapshot_root /mnt/snapshots/localhost
Now that the common options for this backup target have been configured we can start to specify the backup rotations which we shall be using for this target. The file below specifies a single retention period which will cause rsnapshot to keep 14 daily backups of any backup points specified. As you can see we then specify a single backup point as an example, the /etc directory. Feel free to include any other locations which you would like to include in the daily snapshot by inserting additional entries on separate lines.
# Include the common configuration for this host
include_conf /etc/rsnapshot/localhost.common
# Configure the retention period for daily backups
retain daily 14
# Configure the backup source locations
backup /etc/ ./
Next we shall create a second file in which we shall configure an additional set of rotations and backup points. As you can see we are making a backup of the /var/lib/portage directory as an example and we are storing the backups for 8 weeks and then 12 months. We have also added a call to a script which will be run after the backups are performed which will merge the 7th daily backup with the weekly backup performed by this configuration file.
# Include the common configuration for this host
include_conf /etc/rsnapshot/localhost.common
# Configure the retention period for weekly and monthly backups
retain weekly 8
retain monthly 12
# Configure the backup source locations
backup /var/lib/portage/ ./
# Merge the daily backup from one week ago with the weekly backup from the same time
backup_script /usr/local/bin/merge-snapshots localhost daily.7 weekly.1 .merge/
In the previous section we decided that we wanted to use more than one rotation scheme for the files we are going to back-up. This will enable us, for example, to create a snapshot of our configuration files every day while only making a backup of our list of installed packages every week. To be of maximum use these snapshots then need to be merged together to form a single unified snapshot for any rotations after the first weekly rotation.
Unfortunately the rsnapshot package does not provide a script to merge snapshots in this manner so we shall have to create one. Thankfully it is a very simple task consisting of checking for the existence of the source and destination directories for the snapshots in question and, if they both exist, copying the contents of one to the other using hard linked files in the same way as the rsnapshot application. The example script below should suffice for this.
#! /bin/bash
# If the source and destination directories exist then perform the merge
[ -d /mnt/snapshots/$1/$2 ] && [ -d /mnt/snapshots/$1/$3/ ] && \
/bin/cp -al /mnt/snapshots/$1/$2/* /mnt/snapshots/$1/$3/
exit 0
We must also make the script executable, otherwise the rsnapshot application will not be able to use it.
The final step is to create another script which will be used to execute rsnapshot with the correct parameters to perform the backups we have defined in the correct sequence. The example script below can be run daily and will execute all rsnapshot configurations which are required for that particular day performing a daily backup every day, a weekly backup on every Sunday and a monthly backup on the first Sunday of every month.
#! /bin/bash
# Set default configuration values
SNAPSHOT_WEEKLY_DAY=6
# Get the current day of the week and day of the month
DAY_OF_THE_WEEK=$(date +%u)
DAY_OF_THE_MONTH=$(date +%d)
DO_MONTHLY=false
DO_WEEKLY=false
if [ $DAY_OF_THE_WEEK -eq $SNAPSHOT_WEEKLY_DAY ]; then
DO_WEEKLY=true
fi
if $DO_WEEKLY && [ $DAY_OF_THE_MONTH -le 7 ]; then
DO_MONTHLY=true
fi
if $DO_MONTHLY ; then
echo "Performing monthly snapshots:"
awk '/^retain.+monthly/ { print FILENAME }' /etc/rsnapshot/* | \
xargs -r -n 1 -I{} bash -c "echo ' {}' && rsnapshot -c /etc/rsnapshot/{} monthly"
fi
if $DO_WEEKLY ; then
echo "Performing weekly snapshots:"
awk '/^retain.+weekly/ { print FILENAME }' /etc/rsnapshot/* | \
xargs -r -n 1 -I{} bash -c "echo ' {}' && rsnapshot -c /etc/rsnapshot/{} weekly"
fi
echo "Performing daily snapshots:"
awk '/^retain.+daily/ { print FILENAME }' /etc/rsnapshot/* | \
xargs -r -n 1 -I{} bash -c "echo ' {}' && rsnapshot -c /etc/rsnapshot/{} daily"
As before we must now make the script executable otherwise we will not be able to use it.
We can now test our backup script to see if it works by executing it as shown below. Assuming that all goes well you should end up with a snapshot of the directories specified in the daily backup plan located at /mnt/snapshots/localhost/daily.0. If the do-snapshots script is executed on a Sunday a snapshot of the directories specified in the weekly backup plan should also be performed with the result being located at /mnt/snapshots/localhost/weekly.0.
Once you are satisfied that the do-snapshots script is operating as expected and is actually producing daily, weekly and monthly snapshots of the required directories and merging them as required you may wish to automate the process to ensure that the routine task of performing a backup is never forgotten.
To accomplish this you will first need to ensure that a suitable cron daemon is installed. In the example below we install the sys-process/vixie-cron package although any other compliant cron daemon should suffice so feel free to install whichever you prefer.
lisa ~ # emerge vixie-cron
Once the cron daemon in installed it should be started and added to the default run-level, as shown below.
We can now edit the crontab file using the crontab -e command.
The example crontab entry below will schedule a backup for 8:00am every morning.
#Mins Hours Days Months DOTW Job
00 08 * * * do-snapshots
Eventually, after many runs of the do-snapshots script on appropriate days, you should have a complete set of daily backup snapshots (named daily.0 to daily.13), a single snapshot of the weekly files (named weekly.0), multiple unified daily and weekly snapshots (named weekly.1 to weekly.7) and multiple unified monthly snapshots (named monthly.0 to monthly.11).