Full System Backup and Restore

This article details how to backup a FreeBSD system using rsnapshot which leverages rsync to create snapshot backups of the file system. FreeBSD's builtin tar will be used to archive the the most recent file system snapshot into a single file. The resulting snapshot backups can be used to easily and quickly restore the entire system after a hardware failure or to move the system to a bigger storage device or potentially even to an entirely different system.

Restoring the system is usually as simple as:

  • Installing FreeBSD on the new / replacement storage device.
  • Extracting the backup snapshot over top of the freshly installed system.
  • Rebooting.

 

Install and Configure rsnapshot

IMPORTANT: You must be logged in as root for all provided command-line and configurations examples to work properly. Sudo is insufficient and will not work. Programs such as rsync and tar operate with some required functionality disabled if not run as root.

 

Start by installing sysutils/rsnapshot via your preferred port/package installation method.

Copy the default configuration file: cp /usr/local/etc/rsnapshot.conf.default /usr/local/etc/rsnapshot.conf

 

To configure rsnapshot edit /usr/local/etc/rsnapshot.conf

NOTE: rsnapshot.conf requires the use TABs and not SPACEs in all configured options. If you copy and paste those options from the examples in this article, it will be correctly formatted with copy/pastable TABs

Edit the backup levels / intervals section by creating a new "retain" entry configured with your preferred total number of snapshot directories to retain and commenting out the others:

#########################################
#     BACKUP LEVELS / INTERVALS         #
# Must be unique and in ascending order #
# e.g. alpha, beta, gamma, etc.         #
#########################################

retain	weekly	4
#retain	alpha	6
#retain	beta	7
#retain	gamma	4
#retain	delta	3

 

Uncomment "logfile" to enable logging:

# If you enable this, data will be written to the file you specify. The
# amount of data written is controlled by the "loglevel" parameter.
#
logfile	/var/log/rsnapshot

 

Uncomment and edit the default rsync arguments section:

# Default rsync args. All rsync commands have at least these options set.
#
rsync_short_args	-aAHX
rsync_long_args	--delete --numeric-ids --relative --delete-excluded --no-D --fileflags

Two important notes:
1) rsync_short_args options must be all next to each other. For example, "-aAHX" is valid while "-a -A -H -X" is not.
2) a TAB must immediately follow both "rsync_short_args" and "rsync_long_args" however SPACEs are used between each specified rsync_long_args option.

 

Edit the include/exclude section by adding the following list of exclusions:

#include	???
#exclude	???

exclude	/dev/*
exclude	/mnt/*
exclude	/proc/*
exclude	/tmp/*
exclude	/var/run/*
exclude	/var/tmp/*
exclude	/usr/ports/
exclude	/usr/src/
exclude	/.snapshots/

 

Uncomment and enable "link_dest":

# If your version of rsync supports --link-dest, consider enabling this.
# This is the best way to support special files (FIFOs, etc) cross-platform.
# The default is 0 (off).
#
link_dest	1

 

Edit the Backup Points / Scripts section. Create a new single backup point at " / " and comment out the others:

###############################
### BACKUP POINTS / SCRIPTS ###
###############################

# LOCALHOST
backup	/	localhost/

#backup	/home/	localhost/
#backup	/etc/	localhost/
#backup	/usr/local/	localhost/
#backup	/var/log/rsnapshot	localhost/

Close and save rsnapshot.conf

 

Create a Snapshot Backup

To create the snapshot, execute the following command: chflags -R 0 /.snapshots/; rsnapshot weekly, the backup will be written to /.snapshots/weekly.0/localhost/

NOTE: When using the --fileflags rsync option to preserve file flags in your backup (recommended), those flags must be cleared before rsnapshot runs and attempts to rotate the snapshot backup directories. Files with Immutable and undeletable flags such as schg and sunlnk that were previously backed up will prevent rsnapshot from rotating the snapshot backup directories.
The following command chflags -R 0 /.snapshots/ should be run just prior to rsnapshot to quickly clear any file flags from previous backups. 0 (zero) clears all file flags.

 

Create a tar Archive of the Backup

Execute the following command to tar the snapshot:
cd /.snapshots/weekly.0/localhost/; tar acf /.snapshots/example.tar.bz2 *

Note that it is essential to cd into the root of the snapshot backup directory before running tar. This ensures that the root of the tar'ed archive is the same as the root of the file system snapshot. Freebsd's tar (bsdtar) does have a -C option that was supposed to deprecate the need to do the initial cd but it does not work as expected.

Upon completion, copy the tar'ed snapshot to removable media or your preferred offline destination.

 

Automate the Backup

Add a cron job to run the sequence of commands on a (as name implies) weekly basis. Note that the backup level / interval name as well as the actual interval you schedule rsnapshot to run are both completely arbitrary.

The complete command sequence one-liner is:
chflags -R 0 /root/.snapshots/; rsnapshot weekly; cd /.snapshots/weekly.0/localhost; tar acf /.snapshots/example.tar.bz2 *

 

Restore a System from the tar Archive

To restore a system from the tar'ed archive:

Start by installing the same version of FreeBSD of the backed up system to the intended storage device.

Copy the tar'ed snapshot backup file to the system to untar it or alternatively extract directly from removable media.

Execute the following command to untar the snapshot overwriting the file system of the freshly installed base OS with your backup snapshot:
tar -xf example.tar.bz2 -C / --clear-nochange-fflags

Reboot to your restored system.

 

Post Restore

Check all relevant system logs to ensure all installed services and applications are operating normally.

The example configuration provided above deliberately excludes the /usr/ports/ and /usr/src/ directories to reduce the size of the snapshot. The ports tree can be easily restored with portsnap as usual. Sources can be quickly downloaded from any official FTP mirror.