Advanced backup using rsnaphot

From Gentoo Wiki
Jump to: navigation, search

This article describes a advanced automated remote backup scheme using the tool rsnapshot from 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 configuration

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 configuration

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

RSNAPSHOT configuration

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 --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         /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 snapshop 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

To be DONE

postgress backup

To be DONE


To restore the backups specified above, we would use:

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

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

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