Using any NAS as Time Machine

Yes, there are many posts on the web about this.  All the one my google-fu found has manual steps to set one up.  As an engineer, I like to automate things, why do the same steps over and over when you can write a script to package it all up for you?

So here is a shell script to setup sparsebundle ready for you to copy to your NAS and begin using as your Time Machine backup.  Save it to a file, I called mine: create-TimeMachine-bundle.sh.

Then run it with the size that you want to create for your backup.

E.g.  create-TimeMachine-bundle.sh 500g

will create a sparsebundle with size of 500GB.


#!/bin/bash

###################################################################################
# NOTE: even if you connect over WLAN, you MUST use en0 (ethernet) MAC address for
# TimeMachine sparsebundle!!!!

INTERFACE=en0
MACHINENAME=`uname -n | cut -d '.' -f 1,1`

if [ $# -lt 1 ]
then
echo "Usage: $0 SIZE"
echo " Where SIZE is total size (k|m|g|t) allowed for TimeMachine backup usage on NAS."
exit 1
fi
SIZE=$1

MAC_ADDRESS=`ifconfig ${INTERFACE}|grep 'ether '|awk '{print $2;}'|sed 's/://g'`

hdiutil create -verbose -type SPARSEBUNDLE -size $SIZE -fs "Case-sensitive Journaled HFS+" -nospotlight -imagekey sparse-band-size=262144 -volname "${MACHINENAME} Backups" ${MACHINENAME}_${MAC_ADDRESS}

defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1