Main menu

Snapshot backup under linux with rsnapshot

I have used made many backup scripts which archive the data in intervals and download that data in different system or NFS shares. But if you have a large data and you are taking full backup everyday, storage really become a problem. I was looking for a solution and found rsnapshot. It’s provide a way of incremental file level backup in Linux system which reduce the size of backups. It only take the backup which is changed after last backups. Let’s see how can we snapshot backup under Linux with rsnapshot..

rsnapshot is an rsync based snapshot(backup) utility written in perl. rsnapshot use rsync to take backups and hard links to maintain incremental and differential backups. At the first time it will take a full backup then use hard-links to maintane each backup. Each backup appear to be a full backup.

Some of the rsnapshot features..

  • Automatic schedule backup.
  • Use hard links to maintain differential and incremental backups.
  • Automatic cleanup of old backups.
  • Capable of local and remote backups.
  • Easy to configure.

In this artical we will install and configure rsnapshot backup, backup retention and backup scheduling.

Install rsnapshot in CentOS or Ubuntu

in CentOS or RHEL 
 # yum install epel-release
 # yum install rsnapshot
in Ubuntu
 # apt-get install rsnapshot

Configuring rsnapshot

Before dig diving into backups you have to tweak rsnapshot according your requirement. There are some setting you need to modify.

Specify the backup location.

 # vi /etc/rsnapshot.conf
 snapshot_root             /var/backups/

rsnapshot need some system command to run, make sure all these parameters are un-commented.

 cmd_cp          /usr/bin/cp
 cmd_rm          /usr/bin/rm
 cmd_rsync       /usr/bin/rsync
 cmd_ssh         /usr/bin/ssh
 cmd_logger      /usr/bin/logger
 cmd_du          /usr/bin/du

in next section you have to specify the backup retention policy. How many hourly, daily, weekly or monthly backup you need to store.

 interval        hourly  6
 interval        daily   7
 interval        weekly  4
 interval        monthly 3

If you are taking remote backups and SSH is running on different port you need to specify the SSH port in ssh_args.

ssh_args        -p 7851

Remote backup setting

If you want to take remote backups with rsnapshot you need to configure the SSH keys for remote machines. rsnapshot use rsync in back-end so rsync should be able to login into remote machine without any password or you have to enter it manually every time you run rsnapshot.

# ssh-keygen –t rsa
# ssh-copy-id -i /root/.ssh/

Backup targets

Syntax : backup    Backup_Target    Backup_Directory_Name
backup        /home/        localhost/
backup        /etc/            localhost/
backup    Server01/

Note: Make sure to use tabs instead of space or config test will fail.

Testing the configuration

# rsnapshot configtest
 Syntax OK

Exclude files and directories

You can exclude files and directories in backups. Specify the exclude file name is rsnapshot configuration.

exclude_file    /var/backups/files.exclude
# /var/backups/files.exclude
 + /home
 - /home/tmp
 + /var
 - /var/tmp
 - /*

Here we excluded all the files and taking only backup of selected. Add + for adding in backups and – for excluding in backups

Scheduling the backup jobs

Check if /etc/cron.d/rsnapshot is available in your rsnapshot installation and tweak according your need. If it’s not available crate a job manually.

# vi /etc/cron.d/rsnapshot
 0     */6    * * *    root    /usr/bin/rsnapshot hourly
 0      0     * * *    root    /usr/bin/rsnapshot daily
 0      6     * * 6    root    /usr/bin/rsnapshot weekly
 0      3     1 * *    root    /usr/bin/rsnapshot monthly
  • Run every 6 hours and store backup under /var/backups/hourly(n)/ directory
  • Run every day at 12AM  and store backup under /var/backups/daily(n)/ directory
  • Run every Saturday at 6AM and store backup under /var/backups/weekly(n)/ directory
  • Run every 1st day of month at 3AM and store backup under /var/backups/monthly(n)/ directory

Now rsnapshot is configured, you can verify the configuration test

# rsnapshot configtest
 Syntax OK

if everything looks good you can run a backup manually

 # rsnapshot hourly


Now your rsnapshot configuration is done. rsnapshot comes with many other small utility stored under “”/usr/share/doc/rsnapshot-1.3.1/utils” directory. Let’s have a brief look..

Reports : script sends an alert regarding your backups.

 # cp /usr/share/doc/rsnapshot-*/utils/ /usr/local/bin
 # chmod +x /usr/local/bin/

Open you rsnapshot config file and add –stats in rsync_long_args
vi /etc/rsnapshot.conf

rsync_long_args --stats    --delete        --numeric-ids   --delete-excluded

Edit your crontab for sending emails.

0     */6    * * *    root    /usr/bin/rsnapshot hourly 2>&1  | | mail -s "Hourly Backup" your_@_email_here

Some useful rsnapshot commands

Checking the disk usage by rsnapshot

 # rsnapshot du

Diff between to snapshots

 # rsnapshot diff daily.0 daily1

Common warnings and error

Lchown module not found

# yum install cpan
# cpan -A Lchown