How to Backup Your Data to a Hetzner Storage Box Using rsync


In the world of data management, having reliable backups is crucial. Whether you’re managing a server or just want to ensure your important files are safe, using a remote storage solution like a Hetzner Storage Box is a great option. This blog will guide you through the process of transferring files to your Hetzner Storage Box using rsync and show you how to run the transfer in the background, so it doesn’t disrupt your workflow.

Why Use rsync?

rsync is a powerful tool that allows you to efficiently transfer and synchronize files across systems. It only transfers the differences between the source and the destination, making it faster and more bandwidth-efficient compared to other methods like scp.

Setting Up Your Hetzner Storage Box

Before we dive into the rsync command, let’s assume you already have a Hetzner Storage Box set up. For this guide, we’ll use the following sample details:

  • Server: u12345.your-storagebox.de
  • User: u12345
  • SSH Port: 23
  • Password: your_password_here
  • Destination Directory: /home/backup/lyraold

Transferring Files with rsync

Suppose you have multiple backup directories on your local server located in /backup/, and you want to transfer these to your Storage Box. Here’s how you can do it:

  1. Basic rsync Command:
   rsync -avz -e 'ssh -p 23' /backup/2024-07-23 /backup/2024-07-25 /backup/2024-07-27 /backup/2024-07-30 /backup/2024-08-04 /backup/2024-08-07 /backup/2024-08-10 /backup/2024-08-11 [email protected]:/home/backup/lyraold/ --progress

This command does the following:

  • -a: Archive mode, which preserves symbolic links, permissions, timestamps, etc.
  • -v: Verbose mode, so you see the details of the transfer.
  • -z: Compress the data during transfer.
  • -e 'ssh -p 23': Specifies the SSH connection with port 23.
  • --progress: Shows progress during transfer.
  1. Running rsync in the Background To run this command in the background, use nohup and &:
   nohup rsync -avz -e 'ssh -p 23' /backup/2024-07-23 /backup/2024-07-25 /backup/2024-07-27 /backup/2024-07-30 /backup/2024-08-04 /backup/2024-08-07 /backup/2024-08-10 /backup/2024-08-11 [email protected]:/home/backup/lyraold/ --progress > rsync_backup.log 2>&1 &
  • nohup: Prevents the process from being terminated even if you close the terminal.
  • > rsync_backup.log 2>&1: Redirects the output to a log file so you can check the status later.
  • &: Runs the command in the background.
  1. Handling SSH Passwords When running rsync in the background, you won’t be prompted to enter your SSH password interactively. To handle this, consider setting up SSH key authentication or using sshpass if you must supply the password non-interactively:
   sshpass -p 'your_password_here' rsync -avz -e 'ssh -p 23' /backup/2024-07-23 /backup/2024-07-25 /backup/2024-07-27 /backup/2024-07-30 /backup/2024-08-04 /backup/2024-08-07 /backup/2024-08-10 /backup/2024-08-11 [email protected]:/home/backup/lyraold/ --progress > rsync_backup.log 2>&1 &

Monitoring the Background Process

Once the command is running in the background, you can monitor its progress by viewing the log file:

tail -f rsync_backup.log

This will show you the real-time progress of the file transfer. If you need to stop the process, find its PID using ps aux | grep rsync, and then kill it using:

kill <PID>

Conclusion

By using rsync, you can efficiently back up your data to a Hetzner Storage Box with minimal hassle. Whether you’re working on a single server or managing multiple systems, running the transfer in the background ensures that your workflow remains uninterrupted.

Implementing SSH key authentication is a good practice for automating such processes securely. If you encounter any issues, Hetzner’s support is always there to help.

Happy backing up!