Important: You are required to change your passwords used for Gentoo services and set an email address for your Wiki account if you haven't done so. See the full announcement and Wiki email policy change for more information.

A simple backup scheme using rsnapshot

From Gentoo Wiki
Jump to: navigation, search
External resources

This article describes a simple automated backup scheme using the tool rsnapshot, which is based on rsync. rsnapshot makes a specified number of incremental backups of specified file trees, using hard links to save space on the backup medium.

Setup

Installation

Install app-backup/rsnapshot:

root # emerge --ask rsnapshot

fstab

Add an entry like the following in your fstab:

File/etc/fstab

LABEL=backup    /mnt/backup    ext4    noatime,noauto    0 0

In this example, the filesystem is ext4, identified by the label backup, and attaches to a special mount point /mnt/backup. The noauto option means that this backup filesystem will not be mounted by default. The backup filesystem would normally be on an external disk.

Cron scripts

Create cron scripts for the different backup intervals:

File/etc/cron.daily/rsnapshot.daily

#!/bin/sh

echo "### RSNAPSHOT DAILY ###"
mount /mnt/backup && rsnapshot -c /etc/rsnapshot.d/daily.conf daily || echo "Backup failure"
umount /mnt/backup
echo
File/etc/cron.weekly/rsnapshot.weekly

#!/bin/sh

echo "### RSNAPSHOT WEEKLY ###"
mount /mnt/backup && rsnapshot -c /etc/rsnapshot.d/weekly.conf weekly || echo "Backup failure"
umount /mnt/backup
echo
File/etc/cron.monthly/rsnapshot.monthly

#!/bin/sh

echo "### RSNAPSHOT MONTHLY ###"
mount /mnt/backup && rsnapshot -c /etc/rsnapshot.d/monthly.conf monthly || echo "Backup failure"
umount /mnt/backup
echo
Note
These scripts must be executable.

rsnapshot configuration files

Set up the rsnapshot configuration files referred to in the scripts above.

Note
rsnapshot configuration files are tab delimited. Be careful to always use tabs instead of spaces for the options.
Note
Filetree specifications are in rsync format. See the rsync man page for details.

First a base configuration:

File/etc/rsnapshot.d/base.conf

# Default config version
config_version	1.2

# So the hard disk is not polluted in case the backup filesystem is not available
no_create_root	1

# Standard settings
cmd_cp			/bin/cp
cmd_rm			/bin/rm
cmd_rsync		/usr/bin/rsync
link_dest		1

# For convenience, so that mount points can be taken as backup starting points
one_fs			1

# Store all backups in one directory per machine
# A useful alternative may be to create a separate directory for each interval
snapshot_root   /mnt/backup/

Then, here are sample configuration files for daily, weekly and monthly backups:

File/etc/rsnapshot.d/daily.conf

include_conf   /etc/rsnapshot.d/base.conf

# Daily (30 increments)
retain	daily	30
backup		/home/me/		localhost/
File/etc/rsnapshot.d/weekly.conf

include_conf   /etc/rsnapshot.d/base.conf

# Weekly (12 increments)
retain	weekly	12
backup		/etc/			localhost/
# This is for Paludis; for Portage, use /var/lib/portage/world
backup		/var/db/pkg/world	localhost/
backup		/boot/			localhost/
File/etc/rsnapshot.d/monthly.conf

include_conf   /etc/rsnapshot.d/base.conf

# Monthly (6 increments)
retain	monthly	6
exclude		/home/me/
exclude		/tmp/**
exclude		/usr/portage/distfiles/**
exclude		/boot/
exclude		/home/scratch/**
exclude		/var/tmp/ccache/**
backup		/			localhost/

In these files, the second argument of backup specifies a container directory for the backups, usually referring to the machine (in this case, localhost). This can be changed to any name of your choosing.

Restoration

To restore the localhost backups specified above, we would use

root # mount /mnt/backup
root #
rsync -a /mnt/backup/localhost/monthly.0/ /mnt/myroot/
root #
rsync -a /mnt/backup/localhost/weekly.0/ /mnt/myroot/
root #
rsync -a /mnt/backup/localhost/daily.0/ /mnt/myroot/

where /mnt/myroot is the mount point of the fresh root filesystem. In the paths above *.0 refers to the latest increment.

Possible improvements

It is also possible to make remote backups via rsync or SSH -- see the rsnapshot man page for details.

BTRFS snapshots

If you are crazy enough to use btrfs you can leverage its snapshot feature with rsnapshot. Walter Werther has a guide on this.