Hosts:

  • a: local
  • b: remote

Local settings

Users and keys

a% sudo adduser b
...
a% sudo -H -u b sh -c 'cd $HOME && ssh-keygen -t rsa'
...
a$ scp ~b/.ssh/id_rsa.pub b:id_rsa.pub-b

rsnapshot

% sudo apt-get install rsnapshot
% sudo mkdir /var/backups/rsnapshot/

rsnapshot.conf

% sudo vi /etc/rsnapshot.conf
...
% diff -u ...
--- a/rsnapshot.conf
+++ b/rsnapshot.conf
...
 # All snapshots will be stored under this root directory.
 #
-snapshot_root  /.snapshots/
+snapshot_root  /var/backups/rsnapshot/
...
 # Uncomment this to enable remote ssh backups over rsync.
 #
-#cmd_ssh       /path/to/ssh
+cmd_ssh        /usr/bin/ssh
...
 #include       ???
 #exclude       ???
 #exclude       ???
+exclude        /cdrom/
+exclude        /dev/
+exclude        /floppy/
+exclude        /mnt/
+exclude        /proc/
+exclude        /sys/
+exclude        /tmp/
+exclude        /var/backups/rsnapshot/
+exclude        /var/cache/
+exclude        /var/run/
...
 backup /home/          localhost/
 backup /etc/           localhost/
 backup /usr/local/     localhost/
...
+# b
+backup root@b:/            /b/ ssh_args=-o BatchMode=yes -i /home/b/.ssh/id_rsa

Remote settings

a% ssh b
...
b$ sudo vi /root/validate-rsync
...
b$ cat /root/validate-rsync
#!/bin/sh

case "$SSH_ORIGINAL_COMMAND" in
*\&*)
echo "Rejected"
;;
*\(*)
echo "Rejected"
;;
*\{*)
echo "Rejected"
;;
*\;*)
echo "Rejected"
;;
*\<*)
echo "Rejected"
;;
*\`*)
echo "Rejected"
;;
*\|*)
echo "Rejected"
;;
rsync\ --server*)
$SSH_ORIGINAL_COMMAND
;;
*)
echo "Rejected"
;;
esac
...
b$ sudo chmod +x /root/validate-rsync

b$ sudo cat id_rsa.pub-b >> /root/.ssh/authorized_keys
b$ sudo vi /root/.ssh/authorized_keys
b$ sudo cat /root/.ssh/authorized_keys
...
command="/root/validate-rsync" ssh-rsa ... == b@a
...
b$ sudo rm id_rsa.pub-b

Execution

Testing

a% sudo rsnapshot configtest
a% sudo rsnapshot -t -v hourly
a% sudo rsnapshot -v hourly
a% ls -al /var/backups/rsnapshot/
...

Registration to cron

--- a/cron.d/rsnapshot
+++ b/cron.d/rsnapshot
...
-# 0 */4        * * *           root    /usr/bin/rsnapshot hourly
-# 30 3         * * *           root    /usr/bin/rsnapshot daily
-# 0  3         * * 1           root    /usr/bin/rsnapshot weekly
-# 30 2         1 * *           root    /usr/bin/rsnapshot monthly
+0 */4  * * *           root    /usr/bin/rsnapshot hourly
+30 3   * * *           root    /usr/bin/rsnapshot daily
+0  3   * * 1           root    /usr/bin/rsnapshot weekly
+30 2   1 * *           root    /usr/bin/rsnapshot monthly

Tips

  • rsnapshot:
    • val1<TAB>val2<TAB>val3…
    • dir/
  • cron:
    • Enable hourly, or nothing will ever happen.

See also