Skip to content

Instantly share code, notes, and snippets.

@laroo
Created June 29, 2023 20:53
Show Gist options
  • Save laroo/4fbf2e9467794cc1ad168e56a34ce6d4 to your computer and use it in GitHub Desktop.
Save laroo/4fbf2e9467794cc1ad168e56a34ce6d4 to your computer and use it in GitHub Desktop.
Borg backup script using Borgmatic and ntfy.sh
#!/usr/bin/env bash
function _trap_exit () {
ret=$?
if [ $ret -ne 0 ]; then
echo "Backup failed on '${BASH_COMMAND}'"
notify "max" "skull" "Borgmatic backup failed on '${BASH_COMMAND}'"
else
notify "default" "+1" "Borgmatic backup completed successfully"
fi
echo "[exit with status ${ret}]"
exit $ret
}
trap _trap_exit EXIT
function _trap_ctrl_c() {
echo "** Trapped CTRL-C"
exit -1
}
trap _trap_ctrl_c INT
IFS=$'\n\t'
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "${SCRIPT_DIR}"
if [ "$EUID" -ne 0 ] ; then
echo "Please run script as root (use sudo)"
exit 1
fi
echo "[start]"
notify () {
# $1 = priority (eg: default)
# $2 = tags (eg: +1)
# $3 = message (eq "Success")
curl \
-H "Title: My Server Backup" \
-H "Priority: ${1}" \
-H "Tags: borgmatic,${2}" \
-d "${3}" \
https://ntfy.sh/my_unique_ntfy_url
}
notify "min" "clock2" "Borgmatic backup started"
export BORG_CACHE_DIR=/my/custom/cache/dir/dont/want/user/dir
export BORG_PASSPHRASE=Pa55phraseHere!
echo "Create backup..."
borgmatic --config config.yaml --verbosity 1 create
echo "Cleanup old backups..."
borgmatic --config config.yaml prune
echo "Cleanup diskspace"
borgmatic --config config.yaml compact
echo "Backup status"
borgmatic --config config.yaml info
echo "[done]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment