Skip to content

Instantly share code, notes, and snippets.

@aaronzirbes
Last active August 25, 2016 19:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aaronzirbes/aed14f54cd02cb99b4b9 to your computer and use it in GitHub Desktop.
Save aaronzirbes/aed14f54cd02cb99b4b9 to your computer and use it in GitHub Desktop.
Script to setup SMB share for time machine backup
#!/bin/bash
###
### This is the script I use to setup time machine to work with my LinkSys attched NAS (USB 3.0 Hard Drive)
###
#### Configurable Parameters #########
SIZE=300g
NAME="Z.org Time Machine Backup"
#NAS=192.168.1.1
NAS=`netstat -rn |grep default |awk '{print $2}'`
SHARE=
DEFAULT_SHARE=timemachine
NAS_USER=timelord
NAS_PASSWORD=tardis
######################################
SIZE_PARAM="-size $SIZE"
MAC_ADDRESS=`ifconfig en0 | grep ether | sed s/://g | sed -e 's/ether//' | xargs`
HOST=`hostname -s`
if [ "$SIZE}" == "0" ]; then
SIZE_PARAM=""
fi
DISK_IMAGE=${HOST}_${MAC_ADDRESS}
SPARSE_BUNDLE="${DISK_IMAGE}.sparsebundle"
GROUP_ID=`id -g`
if [ "$SHARE" == "" ]; then
SHARE=$DEFAULT_SHARE
echo "INFO: Finding shares on ${NAS}..."
SHARES=`smbutil view //${NAS} |grep Disk |awk '{print $1}'`
for s in $SHARES; do
if (echo $s |grep -q time); then
echo "INFO: Found potential share: '${s}'"
SHARE=$s
elif (echo $s |grep -q backup); then
echo "INFO: Found potential share: '${s}'"
SHARE=$s
fi
done
echo "INFO: Using share: '${SHARE}'"
fi
SMB_URI="smb://${NAS_USER}@${NAS}/${SHARE}"
MOUNT=/Volumes/${SHARE}
echo "INFO: Writing sparse bundle: ./${HOST}_${MAC_ADDRESS} of size $SIZE"
defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1
if [ -d ./${SPARSE_BUNDLE} ]; then
echo "WARN: ${SPARSE_BUNDLE} already found. Not creating."
else
sudo hdiutil create \
$SIZE_PARAM \
-type SPARSEBUNDLE \
-nospotlight \
-volname "${NAME}" \
-fs "Case-sensitive Journaled HFS+" \
-verbose ./${DISK_IMAGE}
fi
sudo chown -R $USERNAME:$GROUP_ID ${SPARSE_BUNDLE}
# This will create a 70GB sparse-bundle as a case-sensitive, journaled HFS+
# without spotlight indexing. Substitute variables in red for values you
# need. Computername_MACaddress may be in the form of “DansComputer_001ec4b8f9b3
echo "INFO: Now connect to ${SMB_URI}"
echo "INFO: To view available shares, run: "
echo ""
echo " smbutil view //${NAS}"
echo ""
echo ""
echo "WARN: Please type your password, and save it to your keychain."
echo ""
echo " open ${SMB_URI}"
open ${SMB_URI}
while [ ! -r ${MOUNT} ]; do
echo "INFO: Waiting for $SMB_URI to become available..."
sleep 2
done
if [ -r ${MOUNT} ]; then
echo " cp -pvr ${SPARSE_BUNDLE} ${MOUNT}/"
if [ -d ${MOUNT}/${SPARSE_BUNDLE} ]; then
echo "WARN: Sparse Bundle already found. Skipping copy."
else
cp -pvr ${SPARSE_BUNDLE} ${MOUNT}/
fi
open ${MOUNT}/${SPARSE_BUNDLE}
TIME_MACHINE_MOUNT=`tmutil destinationinfo |grep 'Mount Point' |sed -e 's/.* : //'`
TIME_MACHINE_VOLUME="/Volumes/${NAME}"
if [ "${TIME_MACHINE_MOUNT}" != "${TIME_MACHINE_VOLUME}" ]; then
echo "INFO: Setting time machine mount."
echo " sudo tmutil setdestination \"${TIME_MACHINE_VOLUME}\"/"
sudo tmutil setdestination "${TIME_MACHINE_VOLUME}"/
else
echo "INFO: Time machine mount already set to ${TIME_MACHINE_VOLUME}."
fi
else
echo "ERROR: UNABLE TO READ $MOUNT"
echo " You must manually run:"
echo " cp -pvr ${SPARSE_BUNDLE} ${MOUNT}/"
fi
@cmvelo
Copy link

cmvelo commented Jun 7, 2015

@aaronzirbes - nice gist 8-)

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