Skip to content

Instantly share code, notes, and snippets.

@QuantumGhost
Created September 9, 2019 06:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save QuantumGhost/1aae8eb8527c9d522fe2a57f214f6ee5 to your computer and use it in GitHub Desktop.
Save QuantumGhost/1aae8eb8527c9d522fe2a57f214f6ee5 to your computer and use it in GitHub Desktop.
TL; DR: time machine sucks. Use borg (https://borgbackup.readthedocs.io) to backup your machine.
#!/usr/bin/env bash
# Put this file on your disk (E.G `/etc/borg/borg.sh`), and add a crontab entry for it.
# your password should be put in env alongside borg.sh, and
# contains `export BORG_PASSPHRASE="YOUR_PASS_PHRASE"
SCRIPT_DIR=$(realpath $(dirname $0))
set -o errexit
export BORG_REPO="YOUR_BORG_REPO"
tmutil localsnapshot
SNAPSHOT=$(tmutil listlocalsnapshots / | tail -n 1)
SNAPSHOT_VERSION=$(echo "${SNAPSHOT}" | awk -F . '{print $4}')
# Please note this should be a fixed path, or the deduplication mechanism of borg will
# NOT work correctly
SNAPSHOT_PATH="/tmp/snapshot"
mkdir -p "${SNAPSHOT_PATH}"
mount -t apfs -r -o -s=$SNAPSHOT / "${SNAPSHOT_PATH}"
# Construct borg args, you may change it according to your need.
BORG_CREATE_ARGS="--stats --one-file-system --compression auto,zstd --progress"
BORG_CREATE_ARGS="${BORG_CREATE_ARGS} --exclude-from ${SCRIPT_DIR}/exclude"
BORG_CREATE_ARGS="${BORG_CREATE_ARGS} --exclude-caches"
# load passphrase
source "${SCRIPT_DIR}/env"
borg create "::${SNAPSHOT_VERSION}" "${SNAPSHOT_PATH}" "${BORG_CREATE_ARGS}"
# clear passphrase as fast as possible
unset BORG_PASSPHRASE
# cleanup
diskutil unmount "${SNAPSHOT_PATH}"
tmutil deletelocalsnapshots $(echo ${SNAPSHOT} | sed 's/com\.apple\.TimeMachine\.//g')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment