Skip to content

Instantly share code, notes, and snippets.

@chrisfcarroll
Last active May 2, 2023 11:43
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chrisfcarroll/e05f1133de0aaf94d7d4 to your computer and use it in GitHub Desktop.
Save chrisfcarroll/e05f1133de0aaf94d7d4 to your computer and use it in GitHub Desktop.
A commandline script for MacOS, to create a TimeMachine Backup Volume on a non-Apple formatted disk
#!/bin/bash
# 2015 Updates to sunkid's script. chrisfcarroll
# - added more quote marks to cope with e.g. spaces in volume names.
# - get machine UUID from ioreg instead of parsing and system report.
# - added steps to tell Time Machine to use your new backup drive.
# - renamed.
# Make it executable with e.g. chmod a+x tmMakeImage && mv tmMakeImage /usr/local/bin.
#
# A bash script to create a time machine disk image suitable for
# backups with OS X 10.6 (Snow Leopard) and later.
# This script probably only works for me (and me!), so try it at your own peril!
# Use, distribute, and modify as you see fit but leave this header intact.
# (R) sunkid - September 5, 2009
#
# This will create a time machine ready disk image named with your
# computer's name with a maximum size of 600GB and copy it to
# /Volumes/backup. The image "file" (it's a directory, really) will
# contain the property list file that SL's TM needs.
#
# . ./tmMakeImage 600GB /Volumes/backup
#
# Based on a script found here :
# http://www.insanelymac.com/forum/index.php?showtopic=184462
usage ()
{
echo ${errmsg}
echo ""
echo "tmMakeImage"
echo
echo " usage: tmMakeImage size [directory] [GO]"
echo " Create a sparsebundle disk image with a max storage size of <size>"
echo " (defaults to GB but you can specify MB)"
echo " and copy it to your backup volume (if specified)."
echo
echo The GO parameter requires sudo.
echo It will mount the new image and tell Time Machine to use it for backups.
echo
}
# test if we have two arguments on the command line
if [ $# -lt 1 ]
then
usage
exit
fi
# see if there are two arguments and we can write to the directory
if [ $# -gt 1 ]
then
if [ ! -d "$2" ]
then
errmsg=${2}": No such directory"
usage
exit
fi
if [ ! -w "$2" ]
then
errmsg="Cannot write to "${2}
usage
exit
fi
fi
SIZE=$1
DIR=$2
NAME=`scutil --get ComputerName`;
UUID=`ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }'`
# get busy
echo -n "Generating disk image ${NAME}.sparsebundle with size ${SIZE} ... "
hdiutil create -size ${SIZE}G -fs HFS+J -type SPARSEBUNDLE \
-volname 'Time Machine Backups' "${NAME}.sparsebundle" >> /dev/null 2>&1
echo "done!"
echo -n "Generating property list file with uuid $UUID ... "
PLIST=$(cat <<EOFPLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.backupd.HostUUID</key>
<string>$UUID</string>
</dict>
</plist>
EOFPLIST)
echo $PLIST > "${NAME}.sparsebundle"/com.apple.TimeMachine.MachineID.plist
echo "done!"
if [ $# -gt 1 ]
then
echo "Copying ${NAME}.sparsebundle to $DIR ... "
cp -pfr "${NAME}.sparsebundle" "$DIR/${NAME}.sparsebundle"
fi
if [ $# -gt 2 ]
then
echo Using new image for backups ...
echo
sudo hdiutil attach "$DIR/${NAME}.sparsebundle"
#The above line needs root to turn off "ignore ownership permissions" I think. An alternative might
#be sudo vsdbutil -a "/Volumes/Time Machine Backups"
#
sudo tmutil setdestination "/Volumes/Time Machine Backups"
echo
echo Done. To start backups immediately:
echo
echo tmutil startbackup
echo
else
echo "Finished! Happy backups!"
echo
echo Next Steps.
echo
echo Mount the new backup drive by double-clicking in the finder.
echo
echo It might be that if you then leave it and wait an hour, Time Machine will pick it up.
echo
echo Other options:
echo
echo From the command line:
echo sudo mkdir /Volumes/Time\ Machine\ Backups/Backups.backupdb
echo sudo tmutil setdestination /Volumes/Time\ Machine\ Backups/
echo [optional tmutil startbackup]
echo
echo Or, you can try copying existing backups onto your new disk image:
echo https://support.apple.com/en-us/HT202380
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment