Skip to content

Instantly share code, notes, and snippets.

@phpfunk
Last active November 28, 2016 17:59
Show Gist options
  • Save phpfunk/10385398 to your computer and use it in GitHub Desktop.
Save phpfunk/10385398 to your computer and use it in GitHub Desktop.
Back That Thang Up (Backing up Your Backups)

So you are making backups? That's good news, but are your backups backed up? As a good rule of thumb I back everything up to a network drive, then that network drive backs up to another drive and is also shipped to Backblaze for an offsite copy. Backblaze doesn't support network drives so I have them hooked up to an older mac mini that I use as a server. Backblaze is only $5/month for unlimited data, so you really can't lose there.

I always mirror my backup internally for when that drive fails and evenutally it will. That way I have a copy to swap out while I get another drive. Backblaze is used for total data loss. If all drives fail or burn up, are stolen, what have you; I will still have my data.

Yes I could use a RAID to mirror as well but this works for me now. A bit about my process is below.

  • Nightly script that backs up data from computers in the house to the network drive
  • Then it runs a script to mirror the backups
  • Then Backblaze fires off to start the upload process to offsite (You can either set the time or run continuously. I run my backups continuously.)

This all starts at 10PM on a nightly cron. The scripts are some simple shell commands run from a mac mini that connects to computers and syncs the info. You would have to substitute your drive names and paths. Also read up on rsync. To do that open terminal and type man rsync. If you can't do that part, I would recommend not doing this and just finding an app to do it for you.

Backup Primary Data

# Mount network drive only if not mounted already
if [ ! -d /Volumes/{DRIVE} ]; then
	mkdir /Volumes/{DRIVE}
	mount_afp afp://{USER}:{PASS}@{IP}/{DRIVE} /Volumes/{DRIVE}
fi

# Check if folder needed for syncing is found
# If not just skip
# Be careful with the --delete param, you can remove if you don't want to delete
# You can also use --dry-run to see what would happen before you really use it
if [ -d /Volumes/{DRIVE}/{FOLDER} ]; then
	echo '-- Syncing {DRIVE}/{FOLDER} to {BACKUP_DRIVE}/{FOLDER}'
	rsync -aruv --delete --exclude='.DS_Store' /Volumes/{DRIVE}/{FOLDER}/* /Volumes/{BACKUP_DRIVE}/{FOLDER}/
fi

#
# Repeat the above as many times as you need for sync data to backup drive
#

Backup your Backup

# Now it's time to mirror the connected backup drives
# Since these drives are local to the computer you can just use diskutil to mount
# run main diskutil to read more about it

# Make sure the secondary backup drive is mounted
if [ ! -d /Volumes/{BACKUP_BACKUP_DRIVE} ]; then
	diskutil mount {BACKUP_BACKUP_DRIVE}
fi

# Check if any of your folders exist on the backup drive before syncing
if [ -d /Volumes/{BACKUP_DRIVE}/{FOLDER} ] && [ -d /Volumes/{BACKUP_BACKUP_DRIVE}/{FOLDER} ]; then
	echo 'Syncing {BACKUP_DRIVE} --> {BACKUP_BACKUP_DRIVE}'
	rsync -aruv --delete /Volumes/{BACKUP_DRIVE}/* /Volumes/{BACKUP_BACKUP_DRIVE}/
fi

Automator Extra

I have this running as an app created with Automator. When the process finishes it will then print the entire result to a new text file which will be on my desktop in the morning for me to check.

Tools Used

  • rsync man rsync
  • diskutil man diskutil
  • mount_afp man mount_afp

Conclusion

This is just my backup routine, it may or may not work for you. For now it works really well in my household. In the end just make sure you are backing up your data and your backups regularly. It may come in handy someday since your entire life is digital now.

# Mount network drive only if not mounted already
if [ ! -d /Volumes/{DRIVE} ]; then
mkdir /Volumes/{DRIVE}
mount_afp afp://{USER}:{PASS}@{IP}/{DRIVE} /Volumes/{DRIVE}
fi
# Check if folder needed for syncing is found
# If not just skip
# Be careful with the --delete param, you can remove if you don't want to delete
# You can also use --dry-run to see what would happen before you really use it
if [ -d /Volumes/{DRIVE}/{FOLDER} ]; then
echo '-- Syncing {DRIVE}/{FOLDER} to {BACKUP_DRIVE}/{FOLDER}'
rsync -aruv --delete --exclude='.DS_Store' /Volumes/{DRIVE}/{FOLDER}/* /Volumes/{BACKUP_DRIVE}/{FOLDER}/
fi
#
# Repeat the above as many times as you need for sync data to backup drive
#
# Now it's time to mirror the connected backup drives
# Since these drives are local to the computer you can just use diskutil to mount
# run main diskutil to read more about it
# Make sure the secondary backup drive is mounted
if [ ! -d /Volumes/{BACKUP_BACKUP_DRIVE} ]; then
diskutil mount {BACKUP_BACKUP_DRIVE}
fi
# Check if any of your folders exist on the backup drive before syncing
if [ -d /Volumes/{BACKUP_DRIVE}/{FOLDER} ] && [ -d /Volumes/{BACKUP_BACKUP_DRIVE}/{FOLDER} ]; then
echo 'Syncing {BACKUP_DRIVE} --> {BACKUP_BACKUP_DRIVE}'
rsync -aruv --delete /Volumes/{BACKUP_DRIVE}/* /Volumes/{BACKUP_BACKUP_DRIVE}/
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment