Time Machine: Setup on and restore from a NAS device

Nov 10, '09 07:30:00AM

Contributed by: valvoline

This hint explained how to set up a network attached storage (NAS) device as a Time Machine backup. To make this process easier, I've created an AppleScript that makes the creation and setup a one-step process -- just drag and drop your mounted NAS onto the following AppleScript to ready the drive for Time Machine use.

on open names
  set volumeName to names as text
  set macAddress to (do shell script "ifconfig en0 | grep ether | tr -d '\\011' | sed s/ether// | sed 's/ //g' | sed s/://g")
  set hostName to (do shell script "hostname -fs")
  tell application "Finder"
    set theSize to round (((capacity of startup disk) / 1024 / 1024) / 1024)
  end tell
  
  do shell script "defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1" with administrator privileges
  
  do shell script "sudo hdiutil create -size " & theSize & " -type SPARSEBUNDLE -nospotlight -volname \"Backup of " & hostName & "\" -fs \"Journaled HFS+\" ~/" & hostName & "_" & macAddress & ".sparsebundle" with administrator privileges
  do shell script "mv ~/" & hostName & "_" & macAddress & ".sparsebundle /Volumes/" & volumeName & "/" with administrator privileges
  
  tell application "Finder" to eject volumeName
  
  tell application "System Preferences"
    activate
  end tell
  
  tell application "System Events"
    tell application process "System Preferences"
      set frontmost to true
      click menu item "Time Machine" of menu "View" of menu bar 1
    end tell
  end tell
  
end open
(You can also download the script (and an application bundle version of it) from this entry on my site.)

After using the script on the drive, open the Time Machine System Preferences panel. Now you should be able to use your network volume as a target disk for Time Machine backups. Read on to find out how to then restore from this disk, assuming you've had a complete hard drive failure and you're starting from scratch with the OS X installation disc...

To restore from your NAS, first insert your Mac OS installation disk and start your Mac. After the installer loads, choose Terminal from the Utilities menu, and mount your external disk. The Installer enables (by default) the DHCP for interface en0; if the NAS is configured for DHCP, then it will get a reachable IP address. To discover that address, try to ping the whole submask using the ping tool:

$ ping 169.254.255.255
PING 169.254.255.255 (169.254.255.255): 56 data bytes
64 bytes from 169.254.101.39: icmp_seq=0 ttl=64 time=0.299 ms
64 bytes from 169.254.101.39: icmp_seq=1 ttl=64 time=0.368 ms
64 bytes from 169.254.101.39: icmp_seq=2 ttl=64 time=0.368 ms
--- 169.254.255.255 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.299/0.345/0.368/0.033 ms
The address that responds to your ping (169.254.101.39 in the above example) is the NAS. Let's make a mount point for our disk:
$ mkdir /Volumes/myNAS
Next, you have to mount the NAS in your freshly created mountpoint:
$ mount -t afp afp://admin:admin@169.254.101.39/myshare /Volumes/myNAS
Be sure to specify the entire address of your NAS server, including the resource (myshare in the above example). Finally, you have to notify the Finder about your newly-mounted device. Start by getting a list of the mounted devices with disktool -l. Look for the rows beginning with:
***Disk Appeared ('something',...
Find the entry containing your NAS mountpoint. Write down the first entry and use it as a parameter (replacing your_entry) in the following command:
$ disktool -m your_entry
Close Terminal and proceed as normal with your installation. When asked to resume from a Time Machine backup, choose the network disk.

[robg adds: The above script is recreated here just in case the original linked version ever vanishes. The linked version may contain newer code, though, so check there first. On the linked page, you'll also find screenshots and more detail on the entire process.]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2009103103534090