10 July 2019 Backups Server Monitoring
Server Backup via rsync on Hetzner HiDrive
It should not be necessary to explain the importance of backups. Here is a short article on how to perform simple backups from Debian based servers to Strato HiDrive (rsync) storage.
First of all, we need to know which directories we want to backup and 2 new directories:
mkdir /backup
mkdir /backup_latest
Let’s assume /home, /etc and /opt are the directories we want to safe.
This is the main script:
#!/bin/bash
# Directory name structure
FOLDER=$(printf $(date +%Y%m%d) && printf "-" && printf $(date +%H%M%S))
# Directories to be backed up
rsync -a /home/ /backup_latest/home/
rsync -a /etc/ /backup_latest/etc/
rsync -a /opt/ /backup_latest/opt/
# You can stop services if needed
# systemctl stop <YOUR SERVICE>.service
rsync --delete -a /home/ /backup_latest/home/
rsync --delete -a /etc/ /backup_latest/etc/
rsync --delete -a /opt/ /backup_latest/opt/
# Start perviously stopped services
# systemctl start <YOUR SERVICE>.service
# Package backup
tar -zcvf /backup/$FOLDER.tar.gz /backup_latest/
# Remove backups older than 10 days
find /backup -mtime +10 -exec rm -f {} \;
# Copy files via rsync to HiDrive
# rsync --delete -avze "ssh" /backup/ meinserverbackup-userxyz@rsync.hidrive.strato.com:/users/backups-user/backups/webserver
The last line is commented out for testing purposes. This should be changed in production.
You should use a dedicated user for backups. After creating the user, OpenSSH Keys needs to be placed in the Settings and activate the rsync protocol: