Skip to content

Instantly share code, notes, and snippets.

@dbernheisel
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save dbernheisel/a33111099eb070dff96c to your computer and use it in GitHub Desktop.

Select an option

Save dbernheisel/a33111099eb070dff96c to your computer and use it in GitHub Desktop.
This script will sync an external drive with a NAS.
#!/bin/bash
remote="/Volumes/Volume_1"
local="/Volumes/Storage"
LOCK=~/Desktop/Synching
logging=~/backup-rsync-log.txt
sleeptime=20
maxthreads=1
set -e
function cleanup {
echo "Removing Synching folder"
rm -rf $LOCK
}
trap cleanup EXIT
mkdir $LOCK || { echo "Backup already running" ; exit 1 ; }
Rez -append ~/backup.rsrc -o ~/Desktop/Synching/$'Icon\r'
SetFile -a C ~/Desktop/Synching
sleep 15 # give time for mounting
# Check if external drive is mounted
if mount | grep "on ${local}" > /dev/null; then
echo "Storage drive is mounted"
# Check if network NAS is mounted
if mount | grep "on ${remote}" > /dev/null; then
echo "Remote NAS is mounted"
echo "Running rsync"
# loop over all root external drive folders and do an rsync for each, up to a limit
for dir in "$local"/*/; do
if [ "$dir" = "Network Trash Folder" ]; then
continue
fi
folder=`basename "$dir"`
echo "Synching $folder"
# create the folder if it doesn't exist, keeping permissions
if [ ! -d "${remote}/${folder}" ]; then
mkdir -p "$remote"/"$folder"
#chown --reference="$dir" "${remote}/${folder}"
chmod --reference="$dir" "${remote}/${folder}"
fi
echo -e "\nStarting rsync $(date +%Y%m%d_%H%M%S) of" $dir >> $logging
# don't go crazy with rsync, so limit it
#while [ `ps -ef | grep -c [r]sync` -gt ${maxthreads} ]; do
# sleep ${sleeptime}
#done
# start rsync parallel threads.
#nohup rsync -avh --progress --delete "$dir" "$remote"/"$folder"/ </dev/null >>$logging 2>&1 &
rsync -avh --progress --delete "$dir" "$remote"/"$folder"/ #&
done
wait
echo "Done"
else
echo "Remote NAS is NOT mounted"
fi
else
echo "Storage drive is NOT mounted"
fi
###########
~/Library/LaunchAgents/launch-backup.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<dict>
<key>Label</key>
<string>storage.backup</string>
<key>LowPriorityIO</key>
<true/>
<key>Program</key>
<string>/Users/memyselfi/backup.sh</string>
<key>ProgramArguments</key>
<array>
<string>backup.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Volumes</string>
</array>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment