Skip to content

Instantly share code, notes, and snippets.

@koehn
Last active September 25, 2017 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save koehn/7603998 to your computer and use it in GitHub Desktop.
Save koehn/7603998 to your computer and use it in GitHub Desktop.
Script for backing up a Mac OS X folder to an encrypted disk image on a remote AFP server. You must first create the encrypted disk image using Disk Utility. I recommend making it a sparsebundle for better use of space and performance. You can run this with cron and append the output to a log file. Modify the `rsync` command to exclude subdirect…
#!/bin/bash
echo =====================================
echo `date` Backing up Documents
# The URL to your remote AFP server. You can test your URL using command-K in the Finder.
AFP_URL=afp://user:password@afp.server.com/Share
# The place where the AFP server is mounted. By default this will be in /Volumes someplace.
AFP_DIRECTORY=/Volumes/Share
# A path relative to `AFP_DIRECTORY` where the encrypted disk image can be found. Create this yourself
# with Disk Utility as an encrypted sparsebundle Disk Image. Be sure to make the maximum size large enough.
IMAGE_PATH=backups/Backups.sparsebundle
# The password you used when you create the disk image.
IMAGE_PASSWORD=disk_image_password
# Where the encrypted disk image is mounted when you open it in the Finder. Usually this will be somewhere in /Volumes.
IMAGE_MOUNT_POINT=/Volumes/Backups
# The directory you'd like to back up.
BACKUP_DIRECTORY=/Users/koehn/Documents
# Mount the remote AFP volume. We need to create the local directory where the mount will occur.
mkdir -p "$AFP_DIRECTORY"
mount_afp "$AFP_URL" "$AFP_DIRECTORY"
# Mount the backup image. In this mode the user must enter the password on the command line.
# Here we DON'T need to create the directory where the mount will occur. Go figure.
echo -n "$IMAGE_PASSWORD" | hdiutil attach -stdinpass "$AFP_DIRECTORY/$IMAGE_PATH"
# Backup the software to the remote encrypted volume using Apple's rsync 2.6.9 from 2006.
# rsync -avE --exclude .DS_Store "$BACKUP_DIRECTORY" "$IMAGE_MOUNT_POINT"
# Use this if you manually installed the (better) rsync 3.1.0 or later (limits bandwidth to 500KB/s)
/usr/local/bin/rsync -aviAEX --bwlimit=500 --delete --exclude .DS_Store "$BACKUP_DIRECTORY" "$IMAGE_MOUNT_POINT"
# Unmount the encrypted disk image.
hdiutil detach "$IMAGE_MOUNT_POINT"
# Unmount the remote directory (will delete the directory we crated with `mkdir` above).
diskutil unmount "$AFP_DIRECTORY"
echo `date` Done backing up Documents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment