Sync folders to network drive

Written by Haydn Williams

I finally found time last night to set up a script to sync my “Music” folder from the MacBook to our NAS drive. It actually turned out to be fairly simple, after browsing a couple of sites to refresh my memory about rsync. The script below will mount a network drive, sync a folder across to it, and then unmount it again:

#! /bin/bash
mkdir /Volumes/music
mount_smbfs //nas_drive/music /Volumes/music -o username=myusername,password=mypassword
rsync --verbose --progress --stats --compress --recursive --times --perms --delete \
    /Users/me/Music/ /Volumes/music/ >> ~/Users/me/Desktop/music_sync.log
umount /Volumes/music/

Simple but effective. Remember that the line beginning ‘rsync’ and the one below (beginning ‘/Users’) are all part of the same command; I’ve just used a backslash at the end of the first line to continue the command on the next line for clarity on this blog.