Skip to content

Instantly share code, notes, and snippets.

@mrrooijen
Last active March 30, 2018 09:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrrooijen/14c08f1256bf28db4b766bde034ab7fb to your computer and use it in GitHub Desktop.
Save mrrooijen/14c08f1256bf28db4b766bde034ab7fb to your computer and use it in GitHub Desktop.

This is a shell script for backing up a configurable $path to your iCloud Drive or Dropbox folder for off-site storage. Backups are lzma compressed and gpg encrypted using your public gpg key. Additionally, backups are automatically rotated using $keep.

Prerequisites:

  • GPG public key
  • iCloud Drive or Dropbox

Procedure (assuming path=/Developer; storage=iCloud; keep=7):

  1. Creates archive of $HOME/Developer
  2. Compresses archive using lzma
  3. Encrypts archive using a gpg public key
  4. Moves archive to an iCloud Drive directory, causing it to sync with iCloud Drive Server
  5. Rotates backups, keeping the latest 7 backups, and deleting the rest

Ensure the iCloud symlink has been setup if using iCloud Drive:

ln -nfs "$HOME/Library/Mobile Documents/com~apple~CloudDocs" $HOME/iCloud

Write the script to a file and make it executable:

chmod a+x $HOME/scripts/backup

Setup a crontab to backup the directory on a daily basis:

0 0 * * * $HOME/scripts/backup

Or run it manually:

$HOME/scripts/backup
#! /bin/sh
# CONFIGURATION
path=/Developer # Path to a directory, relative to $HOME
storage=iCloud # iCloud or Dropbox
public_key=you@example.com # Your GPG UID to access your public key
keep=7 # How many backups to keep (rotates oldest)
# END CONFIGURATION
export PATH=/usr/local/bin:$PATH
timestamp=`date +%s`
filename=$timestamp.tar.lzma.gpg
source_path=$HOME/$path
tmp_path=/tmp/Backups/$path
backup_path=$HOME/$storage/Backups/$path
mkdir -p $tmp_path $backup_path
tar --lzma -C $source_path -c . | gpg -er $public_key > $tmp_path/$filename
mv $tmp_path/$filename $backup_path/$filename
ls $backup_path/* | ghead -n -$keep | xargs rm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment