Skip to content

Instantly share code, notes, and snippets.

@samdoran
Last active December 18, 2015 08:09
Show Gist options
  • Save samdoran/5752315 to your computer and use it in GitHub Desktop.
Save samdoran/5752315 to your computer and use it in GitHub Desktop.
Backup files on a Time Capsule to the local machine using rsnapshot.
#!/bin/bash
#
# Backup files on Time Capsule excluding sparseimages
# Needs to be passed [hourly] [daily] [weekly] [monthly]
# --- Variables --- #
# See if the Time Capsule (TC) is currently mounted.
MOUNT_TEST=$( mount | grep -i afp | wc -l | tr -d " " )
# Name of the mountpoint where TC will be mounted
SOURCE="[local mount point of TC]"
# Where the backup will be stored
DESTINATION="[backup destination]"
# These parameters are passed to mount_afp in line 78
AFP_USERNAME=$(id -un) # Use an account on the TC if the TC has separate user accounts
AFP_PASSWORD='[password on TC]'
TC_NAME="[mdns name of TC]"
# --- Mount the Time Capsule --- #
if [[ $MOUNT_TEST != 0 ]]; then
# Unmount network drive to make sure I can read it to do the backup
AFP_MOUNTS=$( mount | grep -i afp | /usr/bin/awk '{print $3}' )
echo "Unmounting $AFP_MOUNTS"
diskutil unmount force $AFP_MOUNTS
# If other users on the machine have AFP mounts,
# then this command needs root privileges in order
# to unmount them. Add the following to /etc/sudoers:
# [USERNAME] ALL = NOPASSWD: /usr/sbin/diskutil
fi
sleep 1 # Allows the directories in /Volumes to be cleaned up
# Log which backup is running
echo "Performing $1 backup of Time Capsule."
# Create mount point if it does not exist
if [ ! -d $SOURCE ]; then
echo "$(date "+[%d%b%Y %T]") Creating $SOURCE"
mkdir $SOURCE
fi
# Mount the Time Capsule on $SOURCE
echo "$(date "+[%d%b%Y %T]") Mounting TC on $SOURCE"
mount_afp -i "afp://${AFP_USERNAME}:${AFP_PASSWORD}@${TC_NAME}._afpovertcp._tcp.local/Data" $SOURCE
# --- Do the backup --- #
echo "$(date "+[%d%b%Y %T]") Executing rsnapshot $1"
/usr/local/bin/rsnapshot $1
# Date stamp the rsnapshot backups
/usr/local/bin/datestamp_backups.py
# Unmount the Time Capsule
echo "$(date "+[%d%b%Y %T]") Unmouting $SOURCE"
diskutil unmount force $SOURCE
printf "$(date "+[%d%b%Y %T]") Backup complete\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment