Skip to content

Instantly share code, notes, and snippets.

@dsanson
Created September 15, 2010 15:17
Show Gist options
  • Save dsanson/580875 to your computer and use it in GitHub Desktop.
Save dsanson/580875 to your computer and use it in GitHub Desktop.
backup to sdhc
#!/bin/sh
# A script that first checks if an external disk is mounted and
# then uses rsync to perform a time machine style backup.
#
# I need to work on the how the script behaves when the disk is
# full. Ideally, it would delete the oldest backup.
#
original="/"
uuid="FEBD96D7-1F10-3D49-BA02-9D482B6839C8"
date=`date "+%Y-%m-%dT%H-%M-%S"`
data=`diskutil info $uuid`
# Verify that the disk is mounted
if echo $data | grep "Could not find disk"; then
echo "Backup Disk not Mounted."
exit
fi
# Determine mount point (path) of disk
diskpath=`echo "$data" | grep "Mount Point:" | perl -pe "s|.*?/|/|"`
destination="$diskpath/Backups"
# Check for free space
freespace=`echo "$data" | grep "Free Space:"` | perl -pe "s/.*\((\d+).* Bytes.*/\1/"
if [ $freespace < 10000 ]; then
echo "Disk almost full! I will try deleting the oldest backup."
echo "Autodeletion has not been tested"
echo "To try it, please edit the script..."
oldest="$destination/"`ls -t "$destination" | grep back | head -1`
echo "If allowed, the script would delete $oldest."
# rm -rf "$oldest"
echo "Then it would try to run itself again."
# $0
exit
fi
rsync -aP \
--delete \
--delete-excluded \
--exclude-from=/Users/david/.rsync/exclude-sdhc \
--link-dest="$destination/current" "$original" "$destination/back-$date" \
&& rm -f "$destination/current" \
&& ln -s "$destination/back-$date" "$destination/current"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment