FreeBSD Install Rsnapshot Filesystem Snapshot Backup Utility

Posted on in Categories , , , last updated February 7, 2017

How do I install rsnapshot a filesystem snapshot utility based on rsync on my FreeBSD server to backup my local/remote Linux and Unix-based desktop, server and laptop system?

The rsnapshot is a filesystem snapshot utility based on rsync command. rsnapshot makes it easy to make periodic snapshots of local machines, and remote machines over ssh. The code makes extensive use of hard links whenever possible, to greatly reduce the disk space required. It is written entirely in perl with no module dependencies, and has been tested with Perl versions 5.004 through 5.8.1. In this tutorial you will learn how to create FreeBSD based backup server to keep backups in hourly, daily, monthly, and yearly format.

Related: Debian/Ubuntu Linux user read this page, and CentOS/RHEL/Fedora Linux user read this page about setting and use the rsnapshot to make backups.

Install rsnapshot on FreeBSD server

Type the following command to update pkg database:
# pkg update
Sample outputs:

Updating FreeBSD repository catalogue...
FreeBSD repository is up-to-date.
All repositories are up-to-date.

To install the binary package, enter:
# pkg install rsnapshot
Sample outputs:

Fig.01: To add the package type 'pkg install rsnapshot' command
Fig.01: To add the package type ‘pkg install rsnapshot’ command

Or install it using the port
# cd /usr/ports/sysutils/rsnapshot/
# make install clean

Configure rsnapshot on FreeBSD box

First, copy /usr/local/etc/rsnapshot.conf.default to /usr/local/etc/rsnapshot.conf, run:
# cp -v /usr/local/etc/rsnapshot.conf.default /usr/local/etc/rsnapshot.conf
Next, edit /usr/local/etc/rsnapshot.conf, enter:

Attention! This file requires tabs between elements to avoid syntax errors.

# vi /usr/local/etc/rsnapshot.conf
Update and uncomment as follows:

# All snapshots will be stored under this root directory.
snapshot_root   /securebackup/root.user.only/rsnapshot/
 
#########################################
#     BACKUP LEVELS / INTERVALS         #
# Must be unique and in ascending order #
# e.g. alpha, beta, gamma, etc.         #
#########################################
# 6 hourly snapshots - daily (every 4 hour)
# 1 daily snapshot - daily 
# 4 weekly snapshots - weekly (Sunday)
# 12 monthly snapshots - 1st day of every month
################################################
 
retain  hourly  6
retain  daily   7
retain  weekly  4
retain  monthly 12
 
# If you enable this, data will be written to the file you specify. 
logfile /var/log/rsnapshot
 
# Exclude these files from backups
exclude_file    /usr/local/etc/rsnapshot.exclude.txt
 
###################################
### SET BACKUP POINTS / SCRIPTS ###
###################################
 
# LOCALHOST/nas04 Backup our own local freebsd box first
backup  /home/          nas04/
backup  /etc/           nas04/
backup  /usr/local/     nas04/
 
# Okay backup our remote web server called 10.12.249.194
backup  [email protected]:/etc/        wwwserver1/
backup  [email protected]:/root/       wwwserver1/
backup  [email protected]:/home/       wwwserver1/
backup  [email protected]:/scripts/    wwwserver1/
backup  [email protected]:/usr/share/lxc/      wwwserver1/
backup  [email protected]:/lib/ufw/    swwwserver1/
backup  [email protected]:/var/www/    swwwserver1/
 
# Backup mysql database too
backup_script   /usr/bin/ssh [email protected] "/scripts/mysql/backup.nas.sh"        unused1/
backup_script   /usr/bin/scp -r [email protected]:/backups/mysql/nas/ /securebackup/root.user.only/rsnapshot/tmp/    mysql/wwwserver1/
 
# Backup v.backup2 server too
backup  [email protected]:/root/   backup2/
backup  [email protected]:/etc/    backup2/
backup  [email protected]:/var/www/    backup2/

Install mysql backup helper script in 10.12.249.194 remote server

Create a script as follows at /scripts/mysql/backup.nas.sh location on your MySQL server:

#!/bin/bash
# Name: backup.nas.sh
# Author: Vivek Gite <www.cyberciti.biz> under GPL v2.x+
# Purpose: Backup mysql databases for rsnapshot
# Tested on: RHEL/CentOS/Debian/Ubuntu only 
# ---------------------------------------------------------
MUSER='YOUR-USERNAME-HERE'
MPASS='YOUR-PASSWORD-HERE'
MHOST='YOUR-HOSTNAME-HERE'
DEST="/backups/mysql/nas"
NOWFORMAT="%m_%d_%Y_%H_%M_%S%P"
MYSQL="/usr/bin/mysql"
MYSQLDUMP="/usr/bin/mysqldump"
MKDIR="/bin/mkdir"
RM="/bin/rm"
GZIP="/bin/gzip"
DATE="/bin/date"
SED="/bin/sed"
 
[  ! -d "$DEST" ] && $MKDIR -p "$DEST"
 
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
DBS="$($SED -e 's/performance_schema//' -e 's/information_schema//' <<<$DBS)"
$RM -f $DEST/* >/dev/null 2>&1
for db in $DBS
do
                tTime=$(date +"${NOWFORMAT}")
                FILE="$DEST/${db}.${tTime}.gz"
                $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

Install keychain on FreeBSD server

Type the following command to install user-friendly front-end to ssh-agent for password less login to the remote server:
# pkg install keychain
Sample outputs:

Updating FreeBSD repository catalogue...
FreeBSD repository is up-to-date.
All repositories are up-to-date.
The following 1 package(s) will be affected (of 0 checked):
 
New packages to be INSTALLED:
	keychain: 2.8.2
 
The process will require 95 KiB more space.
35 KiB to be downloaded.
 
Proceed with this action? [y/N]: y
[webserver] Fetching keychain-2.8.2.txz: 100%   35 KiB  35.8kB/s    00:01    
Checking integrity... done (0 conflicting)
[webserver] [1/1] Installing keychain-2.8.2...
[webserver] [1/1] Extracting keychain-2.8.2: 100%

Append the following entries in your shell startup file. I’m using default CSH on my FreeBSD as root user:

echo '/usr/local/bin/keychain --clear $HOME/.ssh/id_rsa' >> /root/.cshrc 
echo 'source $HOME/.keychain/webserver-csh' >> /root/.cshrc

Install and configure ssh keys on FreeBSD box only

You need to set up public key authentication and use keychain to manage your keys:
# ssh-keygen -t rsa
Sample outputs:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):      
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): {ENTER-YOUR-PASSPHRASE-HERE}
Enter same passphrase again: {again-ENTER-YOUR-PASSPHRASE-HERE}
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
5b:74:30:c8:4a:10:78:a3:eb:ce:bd:ee:87:40:82:7f [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|   .oo . .o      |
|  . o . o  o     |
|.  o o .  . .    |
|o o   .  . .     |
| + .    S .      |
|  + E    o       |
| . o .  .        |
| .... .          |
| .oo=o           |
+-----------------+

You need to set the Key Pair location and name. I recommend you use the default location if you do not yet have another key there, for example: $HOME/.ssh/id_rsa. You will be prompted to supply a passphrase (password) for your private key. I suggest that you setup a passphrase when prompted. Install the public key in remote server called v.backup2 and 10.12.249.194. First, install ssh-copy-id – a small script which copies your ssh public-key to a remote host, run:
# pkg install ssh-copy-id
Sample outputs:

Updating FreeBSD repository catalogue...
FreeBSD repository is up-to-date.
All repositories are up-to-date.
The following 1 package(s) will be affected (of 0 checked):
 
New packages to be INSTALLED:
	ssh-copy-id: 5.6p1
 
The process will require 2 KiB more space.
2 KiB to be downloaded.
 
Proceed with this action? [y/N]: y
[webserver] Fetching ssh-copy-id-5.6p1.txz: 100%    2 KiB   2.5kB/s    00:01    
Checking integrity... done (0 conflicting)
[webserver] [1/1] Installing ssh-copy-id-5.6p1...
[webserver] [1/1] Extracting ssh-copy-id-5.6p1: 100%

Now, install the pub keys:
# ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]
# ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]

Test your configuration on FreeBSD

Type the following command:
# rsnapshot configtest
# rsnapshot -c /usr/local/etc/rsnapshot.conf configtest

Sample outputs:

Syntax OK

You can see shell commands that would be executed by this utility:
# rsnapshot -t hourly
# rsnapshot -t daily
# rsnapshot -t weekly
# rsnapshot -t monthly

Run rsnapshot and make your first backups on FreeBSD

Type the following command to make hourly backup:
# rsnapshot -v hourly
Sample outputs:

Fig.02: Rsnapshot in action
Fig.02: Rsnapshot in action

Configuring cron to run backups on FreeBSD

Type the following command to configure backup cron job:
# crontab -e
Append the following entries:

30 */4 * * *        /usr/local/bin/rsnapshot hourly
40 21 * * *         /usr/local/bin/rsnapshot daily
20 22 * * 6         /usr/local/bin/rsnapshot weekly
10 23 1 * *         /usr/local/bin/rsnapshot monthly

Save and close the file.

How do I restore backups?

Let us say you would like to restore a backup for wwwserver1. Type the command as follows (select day and date from the ls -l output):
#############################
# Login to your FreeBSD server #
# cd to backup location #
# Select hourly, weekly etc #
# scp or rsync to destination #
###################################
# cd /securebackup/root.user.only/rsnapshot/
# ls -l
# cd hourly.0/wwwserver1/
# scp -r var/www/ [email protected]:/var/www/

How do I see rsnapshot log file?

Type the following command:
# grep 'text-here' /var/log/rsnapshot
# tail -f /var/log/rsnapshot

[2016-09-29T12:39:37] /usr/local/bin/rsync -az --delete --numeric-ids --relative --delete-excluded --exclude-from=/usr/local/etc/rsnapshot.exclude.txt --rsh=/usr/bin/ssh -p 22 -C [email protected]:/cosmosdata/server1/ /securebackup/root.user.only/rsnapshot/hourly.0/server2/server1/
[2016-09-29T12:39:44] touch /securebackup/root.user.only/rsnapshot/hourly.0/
[2016-09-29T12:39:44] rm -f /var/run/rsnapshot.pid
[2016-09-29T12:39:44] /usr/local/bin/rsnapshot hourly: completed successfully	

How to see backup disk usage

Type the following command:
# rsnapshot du
Sample outputs:

7.3G	/securebackup/root.user.only/rsnapshot/hourly.0/
115M	/securebackup/root.user.only/rsnapshot/hourly.1/
115M	/securebackup/root.user.only/rsnapshot/hourly.2/
115M	/securebackup/root.user.only/rsnapshot/hourly.3/
115M	/securebackup/root.user.only/rsnapshot/hourly.4/
115M	/securebackup/root.user.only/rsnapshot/hourly.5/
115M	/securebackup/root.user.only/rsnapshot/daily.0/
115M	/securebackup/root.user.only/rsnapshot/daily.1/
115M	/securebackup/root.user.only/rsnapshot/daily.2/
115M	/securebackup/root.user.only/rsnapshot/daily.3/
115M	/securebackup/root.user.only/rsnapshot/daily.4/
115M	/securebackup/root.user.only/rsnapshot/daily.5/
115M	/securebackup/root.user.only/rsnapshot/daily.6/
117M	/securebackup/root.user.only/rsnapshot/weekly.0/
115M	/securebackup/root.user.only/rsnapshot/weekly.1/
286M	/securebackup/root.user.only/rsnapshot/weekly.2/
186M	/securebackup/root.user.only/rsnapshot/weekly.3/
266M	/securebackup/root.user.only/rsnapshot/monthly.0/
511M	/securebackup/root.user.only/rsnapshot/monthly.1/
390M	/securebackup/root.user.only/rsnapshot/monthly.2/
306M	/securebackup/root.user.only/rsnapshot/monthly.3/
251M	/securebackup/root.user.only/rsnapshot/monthly.4/
107M	/securebackup/root.user.only/rsnapshot/monthly.5/
425M	/securebackup/root.user.only/rsnapshot/monthly.6/
 11G	total

How to compare the two most recent snapshots/backups?

Type the following command:
# rsnapshot diff
Sample outputs:

Comparing /securebackup/root.user.only/rsnapshot/hourly.1 to /securebackup/root.user.only/rsnapshot/hourly.0
Between /securebackup/root.user.only/rsnapshot/hourly.1 and /securebackup/root.user.only/rsnapshot/hourly.0:
  11483 were added, taking 25777440 bytes
  11483 were removed, saving 25776712 bytes

It also accepts two optional arguments which can be either filesystem paths or backup directories within the snapshot_root:
# rsnapshot diff dir1 dir2
dir1 and dir2 are the only compulsory parameters, and should be the names of two directories to compare. Their order doesn't matter, rsnapshot-diff will always compare the younger to the older, so files that appear only in the older will be reported as having been removed, and files that appear only in the younger will be reported as having been added.

And, there you have it, the rsnapshot backup utility installed and working correctly on the latest version of FreeBSD server to backup local and remote servers or desktop system.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

6 comment

  1. Where is the Output of rsync? how do i know what exactly rsync did and if it succeeded doing that?

    I use rsnapshot on Linux, there you get the exact Output of rsync, but this doesnt work on FreeBSD

  2. This is a great article which helped me a lot!
    Please correct the crontab script which is wrong if you call crontab -e! Then you must remove the whole user column ‘root’. With that column you can add these lines to an existing /etc/crontab which is the system’s crontab which needs the user configuration.

Leave a Comment