FreeBSD Install Rsnapshot Filesystem Snapshot Backup Utility

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.

ADVERTISEMENTS

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 type the following pkg command:
# pkg install rsnapshot

FreeBSD install rsnapshot using the pkg command

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

Alternatively, we can install it using the FreeBSD port collection too:
# 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 using the cp command:
# cp -v /usr/local/etc/rsnapshot.conf.default /usr/local/etc/rsnapshot.conf

/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  daily   7
retain  hourly  6
retain  monthly 12
retain  weekly  4
 
# 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  root@10.12.249.194:/etc/        wwwserver1/
backup  root@10.12.249.194:/root/       wwwserver1/
backup  root@10.12.249.194:/home/       wwwserver1/
backup  root@10.12.249.194:/scripts/    wwwserver1/
backup  root@10.12.249.194:/usr/share/lxc/      wwwserver1/
backup  root@10.12.249.194:/lib/ufw/    swwwserver1/
backup  root@10.12.249.194:/var/www/    swwwserver1/
 
# Backup mysql database too
backup_script   /usr/bin/ssh root@10.12.249.194 "/scripts/mysql/backup.nas.sh"        unused1/
backup_script   /usr/bin/scp -r root@10.12.249.194:/backups/mysql/nas/ /securebackup/root.user.only/rsnapshot/tmp/    mysql/wwwserver1/
 
# Backup v.backup2 server too
backup  root@v.backup2:/root/   backup2/
backup  root@v.backup2:/etc/    backup2/
backup  root@v.backup2:/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.5
 
Number of packages to be installed: 1
 
35 KiB to be downloaded.
 
Proceed with this action? [y/N]: y
[rsnapshot] [1/1] Fetching keychain-2.8.5.txz: 100%   35 KiB  36.1kB/s    00:01    
Checking integrity... done (0 conflicting)
[rsnapshot] [1/1] Installing keychain-2.8.5...
[rsnapshot] [1/1] Extracting keychain-2.8.5: 100%

Append the following entries in your shell startup file. I’m using default csh on my FreeBSD as root user. For rsa key, enter:

## repalce 'webserver' with actual `hostname` ##
echo '/usr/local/bin/keychain --clear $HOME/.ssh/id_rsa' >> /root/.cshrc 
echo 'source $HOME/.keychain/webserver-csh' >> /root/.cshrc

If you are using ed25519 key, try:

## repalce 'webserver' with actual `hostname` ##
echo '/usr/local/bin/keychain --clear $HOME/.ssh/id_ed25519' >> /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 ed25519
## For rsa key type ##
# 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 root@webserver
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:
### only needed on an older version of FreeBSD. Skip it on the FreeBSD 11.x/12.x server and above ###
# 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 user@10.12.249.194
# ssh-copy-id -i $HOME/.ssh/id_rsa.pub user@v.backup2

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 daily backup:
# rsnapshot -v daily
For example, one can make hourly backups as follows:
# rsnapshot -v daily

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/ root@10.12.249.194:/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 root@10.86.115.66:/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.

Conclusion

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. You can read rsnapshot docments online.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • Lavi Nov 24, 2015 @ 7:18

    Perl is Back!! :)

  • subi Nov 24, 2015 @ 20:39

    Great article, thanks for share

  • Vamp898 Sep 28, 2016 @ 11:37

    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

    • 🐧 Vivek Gite Sep 29, 2016 @ 7:31

      Take a look at /var/log/rsnapshot file:
      tail -f /var/log/rsnapshot

  • Joerg Feb 1, 2017 @ 22:55

    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 Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.