Last active
October 20, 2020 10:39
-
-
Save iancd/aac1799ae0e7028d93e2a525879d56d5 to your computer and use it in GitHub Desktop.
Script to backup macOS home folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Designed to be run inside the user environment to create a sparse bundle image which contains parity and checksums | |
# Band size is 256MB | |
# variables | |
CONSOLEUSER="$(stat -f %Su /dev/console)" || { echo "`date`: Failed to get username correctly. Exiting"; exit 1; } | |
# stash and eject previous image if exists | |
if [ -d "/Volumes/$CONSOLEUSER.sparsebundle" ]; then | |
/usr/bin/hdiutil eject "/Volumes/$CONSOLEUSER.sparsebundle" || { echo "`date`: Failed to unmount previous image. Try again"; exit 1; } | |
sleep 10 | |
fi | |
if [ -d "/tmp/$CONSOLEUSER.sparsebundle" ]; then | |
mv "/tmp/$CONSOLEUSER.sparsebundle" "/tmp/$CONSOLEUSER.previous`date +%s`.sparsebundle" || { echo "`date`: Failed to rename previous image. Exiting"; exit 1; } | |
fi | |
# create sparse image | |
/usr/bin/hdiutil create -o "/tmp/$CONSOLEUSER.sparsebundle" -srcfolder "/Users/$CONSOLEUSER/" -layout SPUD -imagekey sparse-band-size=514288 -size 512g -fs HFS+J -format UDSB -volname "$CONSOLEUSER.sparsebundle" -attach || { echo "`date`: Failed to create image. Exiting"; exit 1; } | |
#Remove things that do not need to be backed up | |
if [ -d "/Volumes/$CONSOLEUSER.sparsebundle" ]; then | |
if [ -d "/Volumes/$CONSOLEUSER.sparsebundle/.Trash" ]; then | |
rm -rf "/Volumes/$CONSOLEUSER.sparsebundle/.Trash" | |
fi | |
if [ -d "/Volumes/$CONSOLEUSER.sparsebundle/Library/Caches" ]; then | |
rm -rf "/Volumes/$CONSOLEUSER.sparsebundle/Library/Caches" | |
fi | |
else | |
echo "`date`: Image failed to mount" | |
exit 1 | |
fi | |
# compact free space | |
/usr/bin/hdiutil eject "/Volumes/$CONSOLEUSER.sparsebundle"&&sleep 10&&/usr/bin/hdiutil compact "/tmp/$CONSOLEUSER.sparsebundle" || { echo "`date`: Disk failure. Please try again..."; exit 1; } | |
# notify of completion | |
/usr/bin/open "/tmp"&&echo -ne '\007'&&echo -ne '\007'&&echo -ne '\007' | |
echo "Completed image /tmp/$CONSOLEUSER.sparsebundle is ready to be copied to IT Team folder > Employees > Ex-Employee > [name]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment