How to use rSync to backup to Hetzner Storage Box BX11 and receive email confirmation

I bought a Hetzner Storage Box BX11 with 1TB storage and unlimited traffic.

The price is € 3.81 monthly + € 0.00 once-off setup fee. The price includes 19% VAT which is waived if the customer is not resident in Germany. This price is cheaper than the price charged by Amazon S3 and Google Cloud. The fact that bandwidth is free makes it definitely irresistible compared to the other two.

The system allots a password which cannot be changed by the customer. It has to be reset in case it is forgotten.

To create an SSH connection on your Storage Box, first activate the SSH support setting for your Storage Box via the Robot administration interface.

The storage server can be connected on Port 23.

Create SSH Key to connect remote server to storage server.

This is necessary to use SCP, SFTP, rsync or BorgBackup to be able to log in using SSH key authentication without entering a password.

The detailed tutorial is given here.

Generating SSH keys

You can use ssh-keygen to generate a new pair of SSH keys:

server> ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
cb:3c:a0:39:69:39:ec:35:d5:66:f3:c5:92:99:2f:e1 root@server
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|         .   =   |
|      . S = * o  |
|   . = = + + =   |
|    X o =   E .  |
|   o + . .   .   |
|    .            |
+-----------------+

Warning: With the default settings, using ssh-keygen will overwrite an existing SSH key! As an alternative, with the parameter -f, you can specify a different file path.

Upload

Please activate the SSH service of your Storage Box within Robot. Afterwards you can upload your public key with the following command for both Storage Box ports:

server> cat ~/.ssh/id_rsa.pub | ssh -p23 uXXXXX@uXXXXX.your-storagebox.de install-ssh-key
uXXXXX@uXXXXX.your-storagebox.de's password:
Key No. 1 (ssh-rsa user@host) was installed in RFC4716 format
Key No. 1 (ssh-rsa user@host) was installed in OpenSSH format

Test

You should be able to log in without a password on port 23:

sftp -P 23 < username >@< username >.your-storagebox.de
Connected to < username >.your-storagebox.de.
sftp> quit

After this you can use the public SSH key to login without a password.

use rsync to transfer files from the remote server to the storage server

This tutorial is given here https://docs.hetzner.com/robot/storage-box/access/access-ssh-rsync-borg

You can use rsync to upload the current state of your file directories to your Storage Box. For example, to upload a local directory to the Storage Box, you can use rsync as follows:

 rsync --progress -e 'ssh -p23' --recursive < local_directory > < username >@< username >.your-storagebox.de:< target_directory >

To re-download a directory from the Storage Box, you only need to swap the directories:

 rsync --progress -e 'ssh -p23' --recursive < username >@< username >.your-storagebox.de:< remote_directory > < local_directory >

To also remove files which you have already deleted from the source system from the destination, add the –delete parameter. Otherwise deleted files will remain on the target system, and only changes or new files will be transferred. For example:

rsync --progress --delete -e 'ssh -p23' --recursive < local_directory > < username>@.your-storagebox.de:< target_directory >

Instead of the “–progress” flag, it is better to use the “–stats” flag to get information on how many files were uploaded.

 rsync --stats -e 'ssh -p23' --recursive < local_directory > < username >@< username >.your-storagebox.de:< target_directory >

If you want the details to be written to a log, create the log file with the command

 nano backup.log 
 rsync --stats -e 'ssh -p23' --recursive < local_directory > < username >@< username >.your-storagebox.de:< target_directory > >> /var/www/backup.log 2>&1

Create a bash script for doing the sync and sending the mail

 nano rsyncbackup.sh
#!/bin/sh

# sync to Hetzner Storage Server

rsync --stats -e 'ssh -p23' --recursive < local_directory > < username >@< username >.your-storagebox.de:< target_directory > >> /var/www/backup.log 2>&1

##################################################################
# Email notification
##################################################################
SUBJECT="rSync: FULL BACKUP Of SITE FILES To HETZNER STORAGE"
# use form to send from whom the email address has sent
FROM="admin@mywebsite.com"
DESCRIPTION="THIS IS AN AUTO-GENERATED MESSAGE"
#Put the email address of the person to whom you want to send with comma seperator
EMAIL="me@gmail.com"
EMAILMESSAGE=/tmp/emailmessage
 
 echo "From: $FROM" > $EMAILMESSAGE
 echo "To: $EMAIL" >> $EMAILMESSAGE
 echo "Subject: $SUBJECT " >> $EMAILMESSAGE
 echo "************************************************************************" >> $EMAILMESSAGE
 echo "$DESCRIPTION : DO NOT REPLY TO THIS EMAIL MESSAGE. " >> $EMAILMESSAGE
 echo "************************************************************************" >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo "The files of the site has been uploaded to Hetzner Storage Server. Please verify that everything is Okay. " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo "Thanks, " >> $EMAILMESSAGE
 echo "$FROM" >> $EMAILMESSAGE
 cat $EMAILMESSAGE | /usr/sbin/sendmail -f admin@website.com me@gmail.com
chmod 0700 rsyncbackup.sh

Note: See How To Send Server Mails Through Sendmail And Amazon SES (Ubunto 20.04)

Test the bash script

 bash rsyncbackup.sh

Create a Cron Job to automatically create the sync

 crontab -e 
 0 2 * * * /var/www/rsyncbackup.sh 

Check whether the cron job is listed

 crontab -l 

This will create the sync at 2.00 am. (Check the date and time of the server with the date command)

Leave a Reply

Your email address will not be published. Required fields are marked *