Skip to content

Instantly share code, notes, and snippets.

@strlng
Last active April 15, 2021 18:40
Show Gist options
  • Save strlng/05779ce9a6096c96e2d235254b0025d6 to your computer and use it in GitHub Desktop.
Save strlng/05779ce9a6096c96e2d235254b0025d6 to your computer and use it in GitHub Desktop.
Just as the name implies, a very basic backup script. Uses Rsync to back up a Mac user's home directory to a folder named backup at the location of the script file. Depends on the $HOME environment variable being set.
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DIR="${DIR}/backup"
while :
do
echo -e "\033[39mThis script will back up:\033[0m"
echo ""
echo -e "\033[33m****************************************************************\033[0m"
echo -e "\033[34m ${HOME}\033[0m"
echo -e "\tto"
echo -e "\033[34m $DIR"
echo -e "\033[33m****************************************************************\033[0m"
echo ""
read -s -n1 -p "Is this correct? (press y/n)" key
case $key in
y)
echo ""
echo ""
echo -e "\033[31m****************************************************************\033[0m"
echo "You will receive several prompts asking to give Terminal"
echo "access to files and data, you MUST approve these or"
echo "the backup will not complete properly."
echo -e "\033[31m****************************************************************\033[0m"
sleep 2
echo ""
read -n1 -r -p "Press any key to begin..."
echo ""
echo ""
echo "Starting backup, this may take a while..."
echo ""
mkdir -p "${DIR}"
rsync --archive --verbose --exclude ".Trash" --exclude "Library/Application Support/SyncServices" --exclude "Library/Caches" --exclude "Library/Logs" --exclude "Library/Preferences/com.apple.mail-shared.plist" --exclude "Library/Preferences/com.apple.homed.plist" --exclude "Library/Preferences/com.apple.homed.notbackedup.plist" --exclude "Library/Cookies" --exclude "Library/Metadata" --exclude "Library/PersonalizationPortrait" --exclude "Library/Containers" --exclude "Library/Autosave Information" --exclude "Library/IdentityServices" --exclude "Library/Messages" --exclude "Library/HomeKit" --exclude "Library/Sharing" --exclude "Library/Mail" --exclude "Library/Accounts" --exclude "Library/Safari" --exclude "Library/Suggestions" --exclude "Library/Application Support/CallHistoryTransactions" --exclude "Library/Application Support/CloudDocs" --exclude "Library/Application Support/com.apple.sharedfilelist" --exclude "Library/Application Support/Knowledge" --exclude "Library/Application Support/com.apple.TCC" --exclude "Library/Application Support/FileProvider" --exclude "Library/Application Support/com.apple.avfoundation" --exclude "Library/Application Support/CallHistoryDB" "$HOME" "$DIR"
echo ""
echo "Done! You can run this again to backup any updated files. Only updated files will be backed up so the backup should be considerably quicker."
exit 0
;;
n)
echo ""
echo "Exiting..."
exit 0
;;
*)
echo ""
echo "You need to pick Yes or No"
continue
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment