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 onto 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 differently with some required functionality disabled if not run as root.

 

Install sysutils/rsnapshot:

pkg install rsnapshot or
make -C /usr/ports/sysutils/rsnapshot install clean

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 destination directory that snapshots will be written to, if desired:

###########################
# SNAPSHOT ROOT DIRECTORY #
###########################

# All snapshots will be stored under this root directory.
#
snapshot_root	/.snapshots/

 

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	/usr/ports/*
exclude	/usr/src/*
exclude	/var/db/freebsd-update/*
exclude	/var/empty/*
exclude	/var/run/*
exclude	/var/tmp/*
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

 

Test the Configuration

Execute rsnapshot with the -t option to run a test to ensure that the configuration is correct:
rsnapshot -t weekly

 

Create a Snapshot Backup

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 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.

To create the snapshot, execute the following command: chflags -R 0 /.snapshots/; rsnapshot weekly or just rsnapshot weekly if you have not configured the --fileflags option.
The backup will be written to /.snapshots/weekly.0/localhost/

 

Create a tar Archive of the Backup

Execute the following command to tar the snapshot:
tar acf /.snapshots/example.tar.bz2 -C /.snapshots/weekly.0/localhost .

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 /.snapshots/; rsnapshot weekly; tar acf /.snapshots/example.tar.bz2 -C /.snapshots/weekly.0/localhost .

 

Restore a System from the tar Archive

To restore a system from the tar'ed archive:

Start by installing the same version of FreeBSD as the backed up system that you intend to restore.

Copy the tar'ed snapshot backup file to the system to untar it, or mount and 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 installed with the operating system or can be downloaded from any official FreeBSD mirror.