Remote Backup to Hetzner Storage Box via rsync

So just after I figured out how to backup to an external HDD by using rsync, I started researching about ways to backup my computer files on a remote storage. And guess what… I figured out that as well.

Again, I am using my Macbook Air M2 and backing up remotely to a 1TB Hetzner Storage Box, and obviously you can get a bigger box if you have more data.

So… now let’s go through the steps:

Step 1: Order a Hetzner Storage Box

At the very first, order a storage box that fits your needs. I only had a few hundred GBs of data, so I got the 1TB box. You will need to create an account on Hetzner, if you don’t have one already and then just place the order.

Hetzner Storage Box Confirmation Email

It takes some time before they activate your request, and it just took 3 minutes in my case. I received a confirmation email as you see above after the box gets activated.

After you receive the email, it’s time to move to the next step.

Step 2: Get Storage Box details and login via ssh

First of all, open the robot.hetzner.com/storage URL and open the box shown on the screen and you should see something like the screenshot below. You need to turn on the SSH support option.

Hetzner Storage Box Settings

Click on the Reset password button (see above screenshot) and set a new password to be used later.

And now run the following command in your computer’s terminal. Not to mention, replace uXXXXXX with your actual user name (see above screenshot).

ssh -p23 [email protected]

It will ask for the password, so provide the password and you should now be logged in. You should now see the following screen.

SSH Terminal Login to Hetzner Storage Box

After logging in, you can run the help command to see the available options. It will be shown like this.

Terminal Hetzner Storage Box help Command

Now, let’s go to the next step.

Step 3: Install rsync on your computer

Although, you can go through their official website, the easiest way is to install via Homebrew on your Macbook by running the following command.

brew install rsync

It will just take a few minutes to get installed and then you can move to the next step.

Step 4: Decide folders you want to back up

I back up my entire Users>deepak folder except a few folders like Library, Applications, etc. You can use the --exclude flag to exclude any number of folders. My final command looks something like this:

rsync -avh --progress -e 'ssh -p23' --recursive --exclude 'Library' --exclude '.*' --exclude 'Applications' /Users/<USERNAME>/ <uXXXXXX>@<uXXXXXX>.your-storagebox.de:<REMOTE_FOLDER>

In the above command, you will have to replace the following placeholders with the actual value:

  • <USERNAME>: Write the user name of your computer
  • <uXXXXXX>: Replace this with the user name of your Hetzner Storage Box
  • <REMOTE_FOLDER>: Replace this with the folder name that you want to save your files in on the storage box

You can either run this command directly, or set up an alias so that you don’t have to run this big command every time you back up.

Step 5: Create an easy-to-remember alias

To avoid typing or copy-pasting the huge command every time, let’s set up an alias. First, open the terminal on your computer and type the following command:

nano ~/.zshrc

Add the below line at the very end of the file with the alias you want to keep. For example, if I keep rsync_hetzner then I would write:

alias rsync_hetzner="rsync -avh --progress -e 'ssh -p23' --recursive --exclude 'Library' --exclude '.*' --exclude 'Applications' /Users/<USERNAME>/ <uXXXXXX>@<uXXXXXX>.your-storagebox.de:<REMOTE_FOLDER>"

Make sure to replace the variables as explained previously in the step #4. After that, save and exit the nano editor by pressing Ctrl + X, then Y, and then Enter.

Then, reload your shell by running the following command:

source ~/.zshrc

And you’re done.

From the next time, you just have to open your terminal and run rsync_hetzner the command and the sync process will start automatically.

Important Notes:

However, every time you run the command, it will ask for your Hetzner Storage Box password in the terminal. But if you don’t want to enter the password every time, you can follow this tutorial to set up SSH keys.

Also, you might need to provide Full Disk Access to your Terminal app on your Macbook so that it can access all files and folders. I have explained this in detail in the step #3 of this blog post.

To re-download the backup from the remote storage box to your computer, you will have to run the following command.

rsync -avh --progress -e 'ssh -p23' --recursive <uXXXXXX>@<uXXXXXX>.your-storagebox.de:<REMOTE_FOLDER> /Users/<USERNAME>/

You just have to reverse the order of local folder and remote folder, and it works. And this time you will obviously have to remove the --exclude flags. You will find more about this here.

That’s it.

Hope this helps.


Recent Posts

Comments

Leave a Reply

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