Pragmatism in the real world

Backing up to an external HDD using rsync

I have a Linux-based server that acts as my Plex server amongst other things. It’s fanless and I added an additional SSD for to hold the media data so it is nice and quiet.

I’m a little bit of a belt-and-braces person when it comes to backing up my data, so in addition to backing up to the cloud, I also back up the data from the SSD to an external HDD every night using a cron job. I realised that I hadn’t written down what I did for this, so now I’ve documented it!

This is the script I use:

/usr/local/bin/backup-to-ehdd1:

#!/usr/bin/env bash

mount /media/ehdd1

rm -f /var/log/backup-ssd1.log

rsync -azq --delete --log-file=/var/log/backup-ssd1.log "/media/ssd1/data/" "/media/ehdd1/data/"

umount /media/ehdd1

I leave the external HDD unmounted as that way it goes to sleep and so is quieter. I should do some sort of rotation for the log file, but I’m lazy so I only keep the logs for the current run by deleting the log file before we start. One day I’ll do something more clever!

The actual work is done using rsync so that I only copy over files that have changed making the whole process more efficient.

rsync -azq --delete --log-file=/var/log/backup-ssd1.log "/media/ssd1/data/" "/media/ehdd1/data/"

rsync has many parameters. For this backup, I use:

  • -a: Archive mode (recursive sync everything, keeping permissions and ownership the same)
  • -z: Compress the file during transfer
  • -q: Quiet mode, to suppress non-error messages
  • --delete: Delete all files in destination that aren’t in source, so that if I delete a file, it goes from the backup too
  • --log-file=/var/log/backup-ssd1.log: Write a log file

Then we have the directories:

  • "/media/ssd1/data/": Source directory
  • "/media/ehdd1/data/": Destination directory

Naming things is hard, so ssd1 for the internal SSD and ehdd1 for the external spinnning hard drive. Not very imaginative, but also, easy to understand!

I then have a file in /etc/cron.d to actually run it:
/etc/crond.d/backup-to-ehdd1

MAILTO={my email here}
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# Backup to external drive daily
15 1  * * * root /usr/local/bin/backup-to-ehdd1

This runs the backup every day at 01:15 and will email me if there’s an error.

One thought on “Backing up to an external HDD using rsync

  1. Are you sure you want to use the –delete flag? If /media/ssd1/data/ gets accidentally erased than with the next backup /media/ehdd1/data/ would also be emptied.

Comments are closed.