Hetzner Backup Space / Storage Box: Incremental backups with rsnapshot
This guide shows how to set up incremental backups with rsnapshot on a Hetzner Backup Space (Storage Box).
From the archive. This guide applies to the software and hardware versions it describes.

This article describes how to set up incremental backups with rsnapshot on a Hetzner Storage Box.
I mount the Storage Box over SSHFS and save a disk image on it so that I can set file permissions.
I used Ubuntu 20.04 for this setup.
Mount the Storage Box over SSHFS
Generate and upload an SSH key
First, I generate an SSH key and upload it to the Storage Box so the server can authenticate automatically. In the commands below, replace [BENUTERNAME] with your Storage Box username.
# SSH Key generieren, alles akzeptieren, kein Passwort
$ ssh-keygen -t rsa -b 4096
# Quelle: wiki.hetzner.de/index.php/Backup_Space_SSH_Keys
$ cat /root/.ssh/id_rsa.pub >> storagebox_authorized_keys
$ echo -e "mkdir .ssh \n chmod 700 .ssh \n put storagebox_authorized_keys .ssh/authorized_keys \n
# [BENUTERNAME] bitte entsprechend durch den Benutzernamen ersetzen.
chmod 600 .ssh/authorized_keys" | sftp [BENUTERNAME]@[BENUTERNAME].your-storagebox.de
$ sftp -P23 -i ~/.ssh/id_rsa [BENUTERNAME]@[BENUTERNAME].your-storagebox.de
Create and upload the disk image
$ apt install sshfs
$ fallocate -l 100G backup.img
$ mkfs.ext4 backup.img
$ mkdir /mnt/backup/
$ mount -t fuse sshfs#[BENUTERNAME]@[BENUTERNAME].your-storagebox.de:/home/ /mnt/backup/ -o port=23 -o debug
$ mv backup.img /mnt/backup/
For extra protection, you can encrypt the disk image with cryptsetup/LUKS.
Add the mount entries to fstab
$ vim /etc/fstab
sshfs#[BENUTERNAME]@[BENUTERNAME].your-storagebox.de:/home/ /mnt/backup fuse uid=0,gid=0,umask=0007,port=23,reconnect,BatchMode=yes,_netdev 0 0
/mnt/backup/backup.img /var/cache/rsnapshot/ ext4 loop 0 0
$ mkdir -p /var/cache/rsnapshot/
$ mount /var/cache/rsnapshot/
Install and configure rsnapshot
$ apt install rsnapshot
Prepare MySQL backups
Backup storage is usually limited, so I compress the database dump. This can save a substantial amount of space.
$ cp /usr/share/doc/rsnapshot/examples/utils/backup_mysql.sh /usr/local/bin/
$ echo "gzip mysqldump_all_databases.sql" >> /usr/local/bin/backup_mysql.sh
$ chown root:root /usr/local/bin/backup_mysql.sh
$ chmod 755 /usr/local/bin/backup_mysql.sh
Prepare PostgreSQL backups (optional)
rsnapshot also provides an example script for PostgreSQL.
$ cp /usr/share/doc/rsnapshot/examples/utils/backup_pgsql.sh /usr/local/bin/
$ echo "gzip pg_dumpall.sql" >> /usr/local/bin/backup_pgsql.sh
$ chown root:root /usr/local/bin/backup_pgsql.sh
$ chmod 755 /usr/local/bin/backup_pgsql.sh
Configure rsnapshot
I normally use Vim, but my editor settings replace tabs with spaces. The rsnapshot configuration requires tabs, so I edit it in nano.
$ nano /etc/rsnapshot.conf
Only the modified lines are shown below. Make sure to use tabs as separators.
snapshot_root /var/cache/rsnapshot
verbose 4
loglevel 4
logfile /var/log/rsnapshot.log
retain daily 7
retain weekly 4
retain monthly 6
rsync_long_args --stats --delete --numeric-ids --relative --delete-excluded
backup /etc/ web/
backup /var/www/example.com/ web/ exclude=var/log,exclude=var/session
backup_script /usr/local/bin/backup_mysql.sh web/mysql/
backup_script /usr/local/bin/backup_pgsql.sh web/mysql/
Test and run the first backup
I set up rsnapreport to get an email summary of each backup. Replace backup@example.com with your own email address.
# Test-Durchlauf
$ rsnapshot -t daily
# Repots einrichten
$ cp /usr/share/doc/rsnapshot/examples/utils/rsnapreport.pl /usr/local/bin/rsnapreport
$ chown root:root /usr/local/bin/rsnapreport
$ chmod 755 /usr/local/bin/rsnapreport
# Initiales Backup
# backup@example.com durch die eigene Mail-Adresse ersetzen!
$ rsnapshot daily 2>&1 | rsnapreport | mail -s"Backup srv001.example.com" backup@example.com
Schedule backups with cron
Finally, I add cron jobs to run the backups automatically.
$ crontab -e
You can adjust the schedule. This example runs the daily backup at 03:30, the weekly backup on Monday at 06:30 and the monthly backup on the first day of the month at 07:30.
# täglich um 03:30
30 3 * * * rsnapshot daily 2>&1 | /usr/local/bin/rsnapreport | mail -s"Backup srv001.example.com" backup@example.com
# Jeden 1. Tag der Woche um 06:30
30 6 * * 1 rsnapshot weekly 2>&1
# Jeden 1. Tag des Monats um 07:30
30 7 1 * * rsnapshot monthly 2>&1
Sources
- https://wiki.hetzner.de/index.php/Backup_Space_SSH_Keys
- https://www.sotzny.de/2016/10/10/hetzner-storage-box-mit-rsnapshot/
The 21 September 2020 update switches from Backup Space to Storage Box and uses Ubuntu 20.04.
Enlarge the backup image
If you run low on storage, you can switch to a larger plan. You also need to enlarge the backup image. This example increases it from 100 GB to 1 TB.
First, unmount the loop device.
$ umount /var/cache/rsnapshot
Then enlarge the image file, remount it and resize the file system.
# 1TB = 1024*1024 MB
$ dd if=/dev/zero of=/mnt/backup/backup.img bs=1M count=0 seek=1048576
$ mount /var/cache/rsnapshot
$ resize2fs /mnt/backup/backup.img