Sunday, December 22, 2013

Networked Time Machine Backups on Mountain Lion

I need to upgrade my Macbook Pro to Mavericks. But I need to backup my Mac before that. The best way to do that is via Time Machine. The only problem is Time Machine requires a local hard disk and the only ones I own have terabytes of data on them and are NTFS formatted.

So I decided to get Time Machine running over the network.

I found some guides here:
This doesn't work on Mountain Lion unfortunately - for some reason Time Machine on Mountain Lion won't show unsupported Network drives.

Fortunately I found this guide which led me to the tmutil utility.

Also I found Time Machine was creating "snapshots" on the local hard disk which was using up hard disk space. This can be disabled via tmutil disablelocal

So, from start:
  1. Open Time Machine preferences, click on Options
  2. Exclude everything you don't need. I excluded everything except my Home Folder. In my Home folder, I exclude ~/Library/Caches, ~/Applications, SteamApps folder and ~/Library/Developer/Shared. You should also include /.DocumentRevisions-V100 folder as this supports the Auto Save/Versions functionality. My final backup size is just 10 GB after excluding all media folders in my Home folder.
  3. See the estimated size of the full backup and create a sparse disk to approximately 5 times that size
  4. Copy the sparse disk to your Windows share
  5. Mount the Network Drive using Samba
  6. Mount the Sparse Disk using hdiutil
  7. Use tmutil to set the sparse disk as a destination for backup, enable time machine and disable local snapshots
  8. Use tmutil to start the backup
  9. After the backup, you can show Time Machine in Menu Bar and Enter Time Machine from your Hard Drive root to see what all is being backed up.
  10. Unmount the sparse disk and network share
I created a Bash shell script for all the functions, including creating the sparse bundle:
if [ "$1" == mount -o "$1" == all ]; then
    echo "Creating mount folder"
    sudo mkdir $MOUNTPOINT
    echo "Mounting Samba"
    sudo mount_smbfs -o nosuid,nodev $NETWORKSHARE $MOUNTPOINT
    echo "Attaching Sparse Disk"
    sudo hdiutil attach $MOUNTPOINT/$SPARSEBUNDLE.sparsebundle/
if [ "$1" == enable -o "$1" == all ]; then
    echo "Setting Destination..."
    sudo tmutil setdestination /Volumes/$SPARSEBUNDLE/
    sudo tmutil enable
    sudo tmutil disablelocal
if [ "$1" == backup -o "$1" == all ]; then
    echo "Starting Backup..."
    sudo tmutil startbackup --block
if [ "$1" == unmount ]; then
    echo "Detaching Sparse Disk"
    hdiutil detach /Volumes/$SPARSEBUNDLE/
    echo "Unmounting Samba"
    sudo umount $MOUNTPOINT
if [ "$1" == createblankdisk ]; then
    echo sudo hdiutil create -size "$2"G -fs HFS+J -volname "$3" "$3".sparsebundle
if [ "$1". == . -o "$1" == --help ]; then
    echo Time Machine Manager Script
    echo Options:
    echo all - Mount Remote HDD, Setup Time Machine, Create Backup
    echo mount - Mount Remote HDD
    echo enable - Setup Time Machine
    echo backup - Create Backup
    echo unmount - Unmount Remote HDD
    echo createblankdisk SIZE_IN_GB NAME - Create a blank disk with requested size in current folder. The created file has name NAME.sparsebundle
    echo "Done"

Note normally you just have to give mount, enable and backup once. After that you just need to use mount and unmount. Time Machine should auto backup on mount, or you can run backup manually. (You can also use the Backup Now menubar entry for Time Machine)

To stop OSX Auto-Save and Versions functionality, see here and to disable Saved Application State see here (I don't recommend doing that though) In general you can use Grand Perspective or another Disk Space Visualizer to see what are the large sized folders on your mac.

BTW, I found this guide on how to enable NTFS write on OSX. I havent tried it out though.