FreeBSD backup and recovery
The simplest and most straightforward way I have found to perform this type of task, is by using rsnapshot. Although we can use rsync + tar in the traditional way, the restore becomes a bit cumbersome, and you don’t always get an accurate image, it is usually necessary to reinstall software and touch some symlinks.
Installning rsnapshot
pkg install rsnapshot
cp /usr/local/etc/rsnapshot.conf.default /usr/local/etc/rsnapshot.conf
Configuration
We need to edit and adapt to our needs the file mentioned above, which contains the configuration of rsnapshot.
Copy destination directory
###########################
# SNAPSHOT ROOT DIRECTORY #
###########################
# All snapshots will be stored under this root directory.
#
snapshot_root /.snapshots/
Retention
In this case we will make the retention policy 7 copies. This will depend on how often we schedule them.
#########################################
# BACKUP LEVELS / INTERVALS #
# Must be unique and in ascending order #
# e.g. alpha, beta, gamma, etc. #
#########################################
#retain alpha 6
retain beta 7
#retain gamma 4
#retain delta 3
Logging
We remove the comment from the line so that we can audit the process of each copy.
# 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
Arguments
Since rsnapshot is based on rsync , there are certain arguments to which we must set its usage to make the copy as close to the original system as possible.
# 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
What do we copy and what we do exclude
These sections are dedicated to include or exclude from the copy certain directories or files. In this case we will remove the dynamic directories, since it does not make sense, for example, to copy the PIDs of the running processes. Among other things.
# The include and exclude parameters, if enabled, simply get passed directly
# to rsync. If you have multiple include/exclude patterns, put each one on a
# separate line. Please look up the --include and --exclude options in the
# rsync man page for more details on how to specify file name patterns.
#
#include ???
#include ???
#exclude ???
#exclude ???
exclude /dev/
exclude /mnt/
exclude /proc/
exclude /tmp/
exclude /usr/ports/
exclude /usr/src/
exclude /var/empty/
exclude /var/run/
exclude /var/tmp/
exclude /.snapshots/
And finally we indicate that the starting point of the copy is the root of the file system.
###############################
### BACKUP POINTS / SCRIPTS ###
###############################
# LOCALHOST
backup / localhost/
Testing
Before we start using this new configuration, we check that it has no errors. The configuration file of rsnapshot , only accepts TAB as separator, so it is very easy to make a mistake.
rsnapshot -t beta
root@freebsd:~ # rsnapshot -t beta
echo 1756 > /var/run/rsnapshot.pid
mv /.snapshots/beta.1/ /.snapshots/beta.2/
mv /.snapshots/beta.0/ /.snapshots/beta.1/
/usr/local/bin/rsync -aAHX --delete --numeric-ids --relative \
--delete-excluded --no-D --fileflags --exclude=/dev/ --exclude=/mnt/ \
--exclude=/proc/ --exclude=/tmp/ --exclude=/usr/ports/ \
--exclude=/usr/src/ --exclude=/var/empty/ --exclude=/var/run/ \
--exclude=/var/tmp/ --exclude=/.snapshots/ --filter=-/_/.snapshots \
--link-dest=/.snapshots/beta.1/localhost/ / \
/.snapshots/beta.0/localhost/
touch /.snapshots/beta.0/
This output corresponds to a system that already has programmed copies, so the beta.1 destination already exists.
Flags
Because we use --fileflags
to preserve them in the copy destination and restore them if necessary on the system to be recovered, we have to reset them in the copy destination directory, to prevent rsnapshot from rotating the destination directory itself.
chflags -R 0 /.snapshots/
Schedule
We then schedule the copy to be made automatically at the periodicity we need. Remember that periodicity and retention work together to maintain copies for a certain period of time.
That is to say, with a retention of 7 and a scheduled copy every 12 hours, we will have only 3 days and a half of backup copies. Pay attention to this detail.
root@freebsd:~ # crontab -l
0 10 * * * chflags -R 0 /.snapshots/; rsnapshot beta; tar acf /.snapshots/`date + "%m%d%y"`.backup.tar.bz2 -C /.snapshots/beta.0/localhost .
In this particular case we would have 1 week of complete daily copies. In addition, each copy is packaged marking the date on which it is made. In this way it is easier to transfer it if, as in this example, we are copying in the same system. **This is not recommended and is only done for educational purposes.
Restore
The restore process is as simple as extracting and overwriting the files we need from the appropriate copy date on the system itself. Or if we have to restore the system from scratch, after the installation of the system:
tar -xf 111322.backup.tar.bz2 -C / --clear-nochange-fflags