Skip to content

Instantly share code, notes, and snippets.

@nocnokneo
Forked from bitboxer/makeImage.sh
Last active February 27, 2021 15:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nocnokneo/f27e1754ef5692acfe48 to your computer and use it in GitHub Desktop.
Save nocnokneo/f27e1754ef5692acfe48 to your computer and use it in GitHub Desktop.
#!/bin/bash
# A bash script to create a time machine disk image suitable for
# backups with OS X 10.6 (Snow Leopard)
# This script probably only works for 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.
#
# sh ./makeImage.sh 600 /Volumes/backup
# cp -pfr <computer name>.sparsebundle /Volumes/backup/<computer name>.sparsebundle
#
#
#
# Based on a script found here :
# http://www.insanelymac.com/forum/index.php?showtopic=184462
usage ()
{
echo ${errmsg}"\n"
echo "usage: makeImage.sh SIZE [DIRECTORY]"
echo " Create a Time Machine disk image with a max storage SIZE GiB"
echo " at DIRECTORY (current directory if not specified)"
}
# 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 [ $# == 2 ]
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`;
# Note: `head -n 1` added so that the pipeline chain does not wait for
# system_profiler to run to completion (which can take a while)
UUID=`system_profiler | grep 'Hardware UUID' | head -n 1 | awk '{print $3}'`
if [ -n "$DIR" ]
then
cd "$DIR"
fi
echo "Generating ${SIZE} GB disk image: ${NAME}.sparsebundle ..."
hdiutil create -size ${SIZE}G -fs HFS+J -type SPARSEBUNDLE \
-volname 'Time Machine Backups' "${NAME}.sparsebundle" >> /dev/null 2>&1
echo " Done."
echo "Generating property list file with uuid $UUID ..."
cat >"${NAME}.sparsebundle"/com.apple.TimeMachine.MachineID.plist <<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 " Done."
echo "Configuring Time Machine to backup to \"$DIR/${NAME}.sparsebundle\" ..."
sudo tmutil setdestination "`pwd`/${NAME}.sparsebundle"
echo " Done."
echo "Finished! Happy backups!"
@shen-xianpeng
Copy link

hh

@C-Duv
Copy link

C-Duv commented Feb 27, 2021

system_profiler command takes a while to end so the system_profiler | grep 'Hardware UUID' | ... also takes ages.

Use instead:
system_profiler SPHardwareDataType | awk '/UUID/ { print $3; }'

(source: https://www.commandlinefu.com/commands/view/24201/get-hardware-uuid-in-mac-os-x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment