Magical OSX Script to Backup Remote Directories Locally

You can use the following script to backup directory structures from a remote host.  

It will create a time stamped folder structure that will allow you to track multiple backups over time.

I use this to backup my Dreamhost website before I do any big updates.  Likely could also be used in crypto scenarios. This may work on Ubuntu as well – have not tried it.

It uses rsync which is an awesome directory synchronization tool. It can be used to synchronize directories and only transfer what files have changed (i.e. the deltas).

Here it is:

#!/bin/sh

read -p "Continue with PROD -> LOCAL BACKUP (yes/no)? " CONT

if [ "$CONT" == "yes" ]; then
        backup_parent_dir="$HOME/Desktop/backup prod"
        backup_date=`date +%Y_%m_%d_%H_%M`
        backup_dir=${backup_parent_dir}/${backup_date}

        mkdir -p "$backup_dir" # The -p flag will create all subdirectories needed to creat the target directory

        rsync -avz -C --delete some_username@somewebsite.com:/some/awesome/folder/path/ "$backup_dir"
else
        echo "OK...not doing anything";
fi

Bold indicates areas you will need to customize per your lifestyle.

Note on the bolded some_username@somewebsite.com part…that only works without username and password because I set up the two machines to trust each other as explained here.