Skip to content

Instantly share code, notes, and snippets.

@ibejohn818
Last active August 29, 2015 14:14
Show Gist options
  • Save ibejohn818/f2bdecc0ba857792694e to your computer and use it in GitHub Desktop.
Save ibejohn818/f2bdecc0ba857792694e to your computer and use it in GitHub Desktop.
rsync mac script
.HFS+\ Private\ Directory\ Data
/.journal
/.journal_info_block
.AppleDouble
/lost+found
.metadata_never_index
.com.apple.timemachine.donotpresent
.VolumeIcon.icns
/TheVolumeSettingsFolder
Saved\ Application\ State
.DocumentRevisions-V100
.Spotlight-V100
.fseventsd
/.fseventsd
/.hotfiles.btree
/private/var/db/dyld/dyld_*
/System/Library/Caches/com.apple.bootstamps/*
/System/Library/Caches/com.apple.corestorage/*
/System/Library/Caches/com.apple.kext.caches/*
/.com.apple.NetBootX
/Volumes/*
/dev/*
/automount
/Network
/.vol/*
/net
/private/var/folders/*
/private/var/vm/*
/private/tmp/*
/cores
.Trash
.Trashes
/Backups.backupdb
/.MobileBackups
/stuff/*
/Users/johnhardy/VirtualMachines/*
/Users/johnhardy/Downloads/*
/Users/johnhardy/Music/*
#!/bin/bash
PROG=$0
# Set paths to programs
RSYNC=/opt/local/bin/rsync
MOUNT=/sbin/mount
BASENAME=/usr/bin/basename
AWK=/usr/bin/awk
GREP=/usr/bin/grep
DF=/bin/df
TAIL=/usr/bin/tail
TIME=/usr/bin/time
SLEEP=/bin/sleep
ECHO=/bin/echo
# os x specifics
TMUTIL=/usr/bin/tmutil
BLESS=/usr/sbin/bless
# Set backup targets
SRC="/"
DST="/Volumes/XFER/"
#exclude file path
RSYNC_EXC=$HOME/.backupignore
# Check if $SRC is readable and $DST is writeable
if [ ! -r "$SRC" ]; then
${ECHO} "$PROG: Source '$SRC' not readable - Cannot start the sync process"
exit;
fi
if [ ! -w "$DST" ]; then
${ECHO} "$PROG: Destination '$DST' not writeable - Cannot start the sync process"
exit;
fi
# Check if destination is in output of mount(2), because it should be an external disk
DSTBASE=$(${BASENAME} "${DST}")
DISKID=$(${MOUNT} | ${GREP} "${DSTBASE}" | ${AWK} '{print$1}')
if [ ! "${DSTBASE}" -o ! "${DISKID}" ]; then
${ECHO} "$PROG: Destination '$DST' not found in mount(2) - are you sure this is an external disk?"
exit;
fi
# Check if df -k $DST output is unequal to df -k / output
# From <http://stackoverflow.com/questions/8110530/check-disk-space-for-current-directory-in-bash>
DFDST=$(${DF} -k "${DST}" | ${TAIL} -n 1)
DFSRC=$(${DF} -k "${SRC}" | ${TAIL} -n 1)
DFROOT=$(${DF} -k / | ${TAIL} -n 1)
if [ "${DFDST}" = "${DFSRC}" ]; then
${ECHO} "$PROG: Destination and source seem to be on the same disk, aborting..."
exit;
fi
if [ "${DFDST}" = "${DFROOT}" ]; then
${ECHO} "$PROG: Destination is same on same disk as /? aborting..."
exit;
fi
# User should be root
if [ $USER != "root" ]; then
${ECHO} "$PROG: Should be run as root - Cannot start the sync process"
exit;
fi
#if [ -x ${TMUTIL} ]; then
# # Shutdown time machine if it's running
# ${ECHO} "$PROG: Temporarily disabling Time Machine"
# ${TMUTIL} disable
#fi
${ECHO} "$PROG: everything ok! Running backup from '$SRC' to '$DST' in 1 second..."
${SLEEP} 1
# Extra verbosity flags, if required
#VRBFLAGS=" --dry-run -v --progress "
VRBFLAGS="-v --progress"
# Run backup
${TIME} ${RSYNC} \
${VRBFLAGS} \
--archive \
--hard-links \
--fileflags \
--acls \
--hfs-compression \
--protect-decmpfs \
--crtimes \
--one-file-system \
--delete \
--inplace \
--exclude-from="${RSYNC_EXC}" "${SRC}" "${DST}"
${ECHO} "$PROG: backup completed"
#if [ -x ${TMUTIL} ]; then
# Re-enable time machine
# ${ECHO} "$PROG: Re-enabling Time Machine"
# ${TMUTIL} enable
#fi
if [ -x ${BLESS} -a -e "${DST}"/System/Library/CoreServices ]; then
# Make the backup bootable
${BLESS} -folder "${DST}"/System/Library/CoreServices
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment