Advanced backup using rsnaphot

From Gentoo Wiki
Jump to:navigation Jump to:search

This article describes a advanced automated remote backup scheme using the tool rsnapshot as non-root user, which is based on rsync.

rsnapshot makes a specified number of incremental backups of specified file trees from remote servers via ssh with non-user root using sudo, with help of hard links to save space on the backup medium.

The following backup scheme will login to remote user via ssh, fetch all required files with rsync to host, rotate the backups on a daily, weekly and monthly basis. That means, it will keep a daily snapshot for 7 days, a weekly snapshot for 4 weeks and a monthly snapshot for 12 month. Furthermore, it uses an extra partition for the backup which will be mounted only for the time of the backup process.



Install app-backup/rsnapshot:

root #emerge --ask app-backup/rsnapshot


Remote server

First, we will setup remote host for backup. Remote host is host, which we want backup. For example, it is Gentoo server, that serves web server and MySQL database.

Backup user

All operations on remote server will be executed from non-root user. Lets create such user: #useradd -m backup

Backup user must have permissions to run rsync as root, as most of files on belong to root or other users. As we need to backup them, rsync requires root permissions. Lets give those permission to it: #vim /etc/sudoers

And add to sudoers to group backup ability to run [rsync] from root

FILE /etc/sudoers
# backup group can do anything
%backup ALL=(ALL) NOPASSWD: /usr/bin/rsync

rsync wrapper

Remote backup server will login to this server and execute command. This wrapper requires for sudo. Lets create those dummy wrapper script

FILE /home/backup/
logger -t backup $@
/usr/bin/sudo /usr/bin/rsync "$@";

And give executable flag for those script $chmod ug+x /home/backup/

That all. This ready for remote backuping

Backup server

Backup server will connect to server via ssh public key. All backup files will be save to /mnt/backup directory.

Backup user

SSH keys, configurations for backup will be stored in backup user Lets create those user and group #useradd -m backup


All backups will be saving to /mnt/backup directory. We will create backup directory #mkdir -p /mnt/backup #chown backup:backup /mnt/backup #chmod 770 /mnt/backup

SSH keys

rsnapshot will login to remote servers via ssh public keys. Lets generate private/public ssh keys for all next ssh sessions. #sudo -i -u backup $ssh-keygen

Save ssh keys to default path without password. After this, copy ssh key to remote server with ssh-copy-id: $ssh-copy-id

And lets recheck, that everything is file $ssh

No password should be asked and you simply login to


Set up the rsnapshot configuration file.

rsnapshot configuration files are tab delimited. Be careful to always use tabs instead of spaces for the options.
Filetree specifications are in rsync format. See the rsync man page for details.

Default rsnapshot config file:

FILE /etc/rsnapshot.conf
# Default config version
config_version  1.2

# So the hard disk is not polluted in case the backup filesystem is not available
no_create_root  1

# Standard settings
cmd_cp                  /bin/cp
cmd_rm                  /bin/rm
cmd_rsync               /usr/bin/rsync
cmd_ssh                 /usr/bin/ssh
link_dest               1

rsync_long_args         -evaAX --numeric-ids --rsync-path=/home/backup/
ssh_args                -i /home/backup/.ssh/id_rsa

# For convenience, so that mount points can be taken as backup starting points
one_fs                  1

# Store all backups in one directory per machine
# A useful alternative may be to create a separate directory for each interval
snapshot_root   /mnt/backup/

# increments, which are kept
retain  daily   7
retain  weekly  4
retain  monthly 12

# Exclude pattern (refer to --exclude-from from rsync man page)
exclude         /dev/*
exclude         /proc/*
exclude         /sys/*
exclude         /run/*
exclude         /var/tmp/*
exclude         /var/run/*
exclude         /tmp/*
exclude         /usr/portage/distfiles/*
exclude         /lost+found

# backup of server

This files have such params:

rsync_long_args Parameters, that will directly passed to rsync command:

   -e specify the remote shell to use 
   -v increase verbosity
   -a archive mode. Cause rsync to backup file owners and permissions
   -A acl. This option causes rsync to update the destination extended acl attributes to be the same as the source ones
   -X xattrs. This option causes rsync to update the destination extended attributes to be the same as the source ones
   rsync-path Execute on remote server rsync wrapper script /home/backup/

ssh_args Path to public key, that should be used for remote ssh login

snapshot_root Path to directory, where all backup files will be stored

backup specifies a container directory for the backups, usually referring to the machine (in this case, This can be changed to any name of your choosing. The final snapshots will be saved under /mnt/backup/{daily,weekly,monthly}.[0-9]*/

exclude This directory will be excluded from backup

To check rnsnapshot configuration file for errors, run
user $rsnapshot configtest
rsnapshot will always take the last daily snapshot to create the first weekly snapshot and the last weekly snapshot to create the first monthly one. It will not take the 7th daily snapshot to create the first weekly snapshot. Therefore, it is possible to keep less or more than 7 daily snapshots, but is this case the first weekly snapshot is not one week old.

cron jobs

Add cron job to run backup-ing $crontab -e
If you want full backup of remote system, with full backup of file owners, groups, permission, attributes, you should run rsnaphost cron jobs from root user. In other words, command below should be run from root #crontab -e
#3am each day
0 3 * * *    ionice -c 3 nice -n +19 /usr/bin/rsnapshot daily
#4am each week
0 4 * * 1    ionice -c 3 nice -n +19 /usr/bin/rsnapshot weekly
#4am each month
0 4 1 * *    ionice -c 3 nice -n +19 /usr/bin/rsnapshot monthly

rsnaphost jobs will run rsnapshot with minimum CPU and I/O priority.

MySQL backup

This backup configuration are workable for small non-production databases, that doesn't have too many transactions. For more advanced MySQL backup, see (replication or Binary Log backup)

Login to backup user: $sudo -i -u backup

Create file .my.cnf with such content

FILE /home/backup/.my.cnf
host = localhost
port = 3306
user = backup

This file are used every time, when mysqldump tool will be called. Be sure, that only backup user have access to /home/backup/.my.cnf file

Create bash script mysql_dump.bash, that will for backup:

FILE /home/backup/mysql_dump.bash
/usr/bin/mysqldump --all-databases | bzip2 -c > /backup/mysql/`date +%Y.%m.%d_%H.%M.%S.sql.bz2`

Add executable flag for script: $chmod +x /home/backup/mysql_dump.bash

Create directory /backup/mysql, that contain all MySQL backups and grand permissions: #mkdir -p /backup/mysql #chown backup /backup/mysql #chmod 700 /backup/mysql

Create MySQL user backup with access to all databases (like root user, but for backup) #mysql

and type:


Last step - create cron job, that will call mysql_dump.bash script and dump all databases. Execute from backup user $crontab -e

and add such line (run every day at 01:00)

0 1 * * * /home/backup/mysql_dump.bash

PostgreSQL backup

To be DONE


To restore the backups specified above, we would use:

root #mount /mnt/backup
root #rsync -aAX /mnt/backup/ /mnt/myroot/
root #rsync -aAX /mnt/backup/ /mnt/myroot/
root #rsync -aAX /mnt/backup/ /mnt/myroot/

If backup are on remote server, rsync can be done via ssh

root #rsync -aAX /

where /mnt/myroot is the mount point of the fresh root filesystem. In the paths above *.0 refers to the latest increment.


Technically, MySQL dump (created from upper section) are just bzipped text file with SQL commands to MySQL database. Those command will unzip archive, and send sql command to mysql

root #bzcat /backup/mysql/some_ziped_archive.bz2 | mysql --user=root

Possible improvements

  • Use remote device for storing backups /mnt/backup - TO BE DONE
  • Use encryption to crypt backups /mnt/backup - TO BE DONE

See also

  • Rsnapshot — an automated backup tool based on the rsync protocol and written in Perl.

External resources

  • [1] - Documentation from rsnapshot site with some help about configuring rsnapshot
  • [2] - HOWTO configure the same, but more general