How to back up to an external hard drive using rsync

Recently I’ve been getting myself more organised and focused with my business, and part of that process made me want to have a reliable back-up procedure in place for the work I do in creating my online courses.

My case is that I have:

  • a “master” folder on Dropbox in which I put all of my online business resources
  • a “backup” folder on an external hard disk drive to which I want to replicate the master folder

I’m on a Mac and wrote the following script (in Bash) which backs up my Dropbox master folder to my external hard disk backup folder – just save it in the Bash profile that you use to store your environment variables, aliases and functions (in my case it’s the file ~/.bashrc but you might use ~/.bash_profile instead), use it and enjoy!

You’ll need to modify the variables MASTER_SOURCE_FOLDER to point to the folder you want to have backed up, and BACKUP_TARGET_FOLDER to point to the folder where the backup will be made. Keep these consistent between runs and you should be fine!

NOTE: The trailing slashes are critically important otherwise the backup will not work correctly

Here’s the script:

function backupToHardDisk() {    
  # IMPORTANT!!:  ensure directories end with a trailing slash!

  [ ! -d "$MASTER_SOURCE_FOLDER" ] && echo "Master source folder [$MASTER_SOURCE_FOLDER] does not exist. Bailing." && return 1
  [ ! -d "$BACKUP_TARGET_FOLDER" ] && echo "Backup target folder [$BACKUP_TARGET_FOLDER] does not exist. Bailing." && return 1

  echo '************* BACKUP DRY RUN REPORT *************'
  echo "Master Source Folder:  $MASTER_SOURCE_FOLDER"
  echo "Backup Target Folder:  $BACKUP_TARGET_FOLDER"
  echo -ne 'Start real backup? (type YES to initiate):  '
  read answer
  if [ "$answer" == 'YES' ]; then
    echo '************* PERFORMING ACTUAL BACKUP *************'
    rsync -arvh --delete "$MASTER_SOURCE_FOLDER" "$BACKUP_TARGET_FOLDER"
    echo '************* Finished! *************'

The script uses rsync to do the backup which you can read more about online on the man page for it.

The flags I’ve set basically do an incremental backup (i.e. it will detect the differences of files and folders and only action these, ignoring the rest), preserving permissions and timestamps etc. It will basically ensure that the target backup folder is an identical mirror image of the master source folder, also taking care to delete any extra stuff which is in the target folder which isn’t any longer in the source folder (so just use this as a one-way backup to a folder you don’t ever modify manually yourself, otherwise it will delete those changes you’ve made to the backup folder).

I suggest you create two test folders, run it a few times after making various alterations to the master folder and double check you’re happy with what it does to the target backup folder. Then when you’re happy and familiar with the way it functions, swap in the real folders and make it part of you weekly backup routine.

Enjoy and see you in the next post! 😉