Skip to content

Instantly share code, notes, and snippets.

@maximko
Last active April 6, 2024 15:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximko/c78c43ad5cf79b96ab8f2fa6c6ce1d96 to your computer and use it in GitHub Desktop.
Save maximko/c78c43ad5cf79b96ab8f2fa6c6ce1d96 to your computer and use it in GitHub Desktop.
Backup macOS using borg and apfs snapshots
#!/usr/local/bin/bash
export BORG_REPO="ssh://user@server/borg-repo"
if [[ ! "${USER}" == "root" ]]; then
echo Start this script as root
exit 1
fi
echo === Present snapshots ===
tmutil listlocalsnapshots /
echo === Creating snapshot ===
name=$(tmutil localsnapshot | grep Created | egrep -o '[0-9-]*')
if [ ! -z "${name}" ]; then
echo Created snapshot ${name}, mounting...
else
echo Snapshot creation failed
exit 1
fi
echo === Mounting snapshot ===
mkdir /tmp/borg-snapshot
# Using /Volumes instead of / because otherwise it says resource busy. Can be any existing dir in /
if ! mount_apfs -s com.apple.TimeMachine.${name}.local /Volumes /tmp/borg-snapshot/; then
echo Failed to mount snapshot, exiting
exit 1
fi
echo === BACKUP ===
borg create -C zstd --stats --progress ::$(gdate -Is) /tmp/borg-snapshot
echo === Umount and removing snapshot ===
umount /tmp/borg-snapshot && rm -rf /tmp/borg-snapshot
tmutil deletelocalsnapshots ${name}
echo === Done ===
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment