How to backup to cloud with rclone

This article should show you how to back up your machine to cloud using rclone. As you know, regular backup is very important (if you don't want to lose your data). It's good to have one backup on an external hard drive at home and one "off-site" backup somewhere else. In this case, the off-site is cloud (someone else's computer). In the article, I'll be using Hetzner's storage box they offer, but you can choose your own cloud. Rclone is supporting several different services.

Set up your cloud service

Of course, one of the first things you should do is register with one of many providers supported by rclone. As mentioned above, I chose to use Hetzner's BX11 storage box - it's the cheapest one they offer, but I think that should be enough for personal use.

When the cloud service is set up, check if you can connect properly to it. ssh example.com -p23

Install rclone

You can follow rclone's own guide for many platforms. Or, if you use Fedora, you can just dnf install rclone.

Setting up rclone

Note: I wasn't able to edit config file in rclone, instead I had to write in a file manually.

When you run rclone config, you'll get an option to make a new remote (option N). Enter name for the remote (this is the name you'll use in your commands - I named mine simply storagebox), then choose which service (type of storage) you're using. In my case it would be Hetzner.

When I tried to add credentials, it didn't work for me for some reason. If this happens to you, too, you can open your favourite text editor and create your config file yourself. The path is ~/.config/rclone/rclone.conf and write following settings (if required, change as needed):

[storagebox]
type = sftp
host = [address]
user = [user]
port = 23
pass = [password used to connect to the host]
shell_type = unix
md5sum_command = md5 -r
sha1sum_command = sha1 -r

Now we can try and see if we can connect rclone ls storagebox. Hopefully everything works (as it should!) and we can continue to the next step.

If you'd like, you can also setup "crypt remote" where rclone takes care of encryption of transferred files. We will use gpg to encrypt our files before transfer (see below).

Creating and sending backup

Update : I have written about using GPG in my article How to encrypt and decrypt with GPG, if you're interested to read a little bit more about it.

I wrote a small script to automate everything. To use the script, copy the code and save it somewhere. Create two new files (I keep them in my HOME directory.

First file is password, which will contain password for your key, so we can automatically run the script. Make sure the permissions are safe, for example only readable by root, so unauthorised people can't read your password. The second file is exclude.txt, where I write all files and directories I don't want to be part of my backup. Write whole paths to them, one path per line. When you've done that, it's time to run the script.


#!/bin/sh
# Backup script for storagebox using rclone
# Date of creation: 2023-11-12
# Author: bugbear ( https://alt.lawndale.space/@bugbear )

# PATHS - Change this based on your preferences
# BACKUP_DIR is where we write the file temporarily before sending it to the cloud
BACKUP_DIR='[where you'd like your temporary backup file saved]'
HOME_DIR='/home/[user]'

cd $BACKUP_DIR

# Today's date for filename
DATE=$(date '+%Y-%m-%d')

# In case of prior script interruption, delete the temporary backup file left after script interruption.
if [ -f $BACKUP_DIR/$DATE.tar.gz.gpg ]; then
	rm $BACKUP_DIR/$DATE.tar.gz.gpg
fi

# Notify the user we're starting to backup the computer
notify-send --urgency=normal -i "Backup is starting."

# Compress and encrypt the home directory.
# Get saved passphrase from a file for encryption.
# Read list of files and directories to exclude from exclude.txt file
echo "Creating the encrypted backup file"
tar -cz -X $HOME_DIR/exclude.txt $HOME_DIR | gpg -c -o $BACKUP_DIR/$DATE.tar.gz.gpg --batch --passphrase-file $HOME_DIR/password

# Copy the file to storagebox
echo "Sending files"
rclone copy $BACKUP_DIR/$DATE.tar.gz.gpg storagebox:backup

# Remove the temporary backup file.
echo "Deleting the temporary backup file."
rm $BACKUP_DIR/$DATE.tar.gz.gpg
cd $HOME_DIR

# Notify user we finished
notify-send --urgency=normal -i "Backup finished."
echo "Done!"

I think the script is self-explanatory[citation needed], but if you have any questions, please let me know on fedi @bugbear@alt.lawndale.space


see you, space cowboy

Share on fediverse: