Backing up files on Linux with rsync

For a couple of years now I have been using Linux on my computer at home. Having bounced around a few distros I have settled on Linux Mint, and it is filling all my needs so far.

Until recently I never really had a backup strategy. That meant that up until now if there were any issues with my computer then any data on that computer was lost forever. While it presented a good opportunity to refresh, I have lost untold number of videos, photos and music that I would have liked to keep hold of.

Well, no more!

Using rsync – a linux command line utility – allows for the creation of backups of entire directories into another folder. To prevent from losing the backup in case of hard drive failure we can set the backup folder to be on a removable drive, such as a USB drive. That way I still have all of my files if my hard drive explodes!

For an added layer of security the USB drive will be encrypted, that way anyone who stumbles across the USB drive won’t have access to all the precious data.

So let’s get started.

Stage 1: Encrypt USB drive.

On Linux Mint the program used to format and encrypt USB drives is called “Disks”

 

Once the Disks program has opened select your USB drive from the menu on the left then click the small gear icon to open the menu. FOr a comprehensive wipe of the USB drive select ‘Overwrite existing data with zeroes’. The Type dropdown is where we choose our USB to be encrypted. This particular method of encryption will only work on Linux machines, but since we only want it for use on a single machine to act as a back up for our data this is totally fine. Finally select a strong password and click format.

 

This process could take a while so this is a good point to grab yourself a coffee.

Once complete click the little lock icon to lock the USB drive. It will now be ready to use.

When you now navigate to the USB drive you will be prompted for the passphrase to unlock the encryption.

 

Stage 2: Bash script to back up files

On my machine there are only a few main folders (and corresponding files and subdirectories) that I need backup copies of. Namely Documents, Calibre Library, Downloads, miniconda3 and Standard Notes. With rsync I can back them up to my USB drive as follows:

#!/bin/bash

# Bash script to back up important data to encrypted USB hard drive

rsync -av /home/fuzzylumpkins/Documents "/media/fuzzylumpkins/Samsung CocaCola/lumpkins backup"

rsync -av "/home/fuzzylumpkins/Calibre Library" "/media/fuzzylumpkins/Samsung CocaCola/lumpkins backup"

rsync -av /home/fuzzylumpkins/Downloads "/media/fuzzylumpkins/Samsung CocaCola/lumpkins backup"

rsync -av /home/fuzzylumpkins/miniconda3 "/media/fuzzylumpkins/Samsung CocaCola/lumpkins backup"

rsync -av "/home/fuzzylumpkins/Standard Notes Backups" "/media/fuzzylumpkins/Samsung CocaCola/lumpkins backup"

Essentially just pass the folder directory to backup, and the location to put the backup to be to the rsync function. It’s that easy.

To backup files on a schedule simply make the script runnable:

chmod a+x runmypythonscript.sh

Then set it up as a cron task.

Now I have weekly backups of all my important files on an encrypted USB drive! The beauty of rsync is that it uses a delta-transfer algorithm which means it only updates any changes that are made in the files. This means that the first backup might take a while but subsequent backups will be extremely quick.

 

Send a Comment

Your email address will not be published.