Skip to content

Instantly share code, notes, and snippets.

@xenithorb
Last active February 23, 2018 23:40
Show Gist options
  • Save xenithorb/b8209f233e86380bdd4a338ce17ce484 to your computer and use it in GitHub Desktop.
Save xenithorb/b8209f233e86380bdd4a338ce17ce484 to your computer and use it in GitHub Desktop.
Simple generic backup script for borg
#!/bin/bash
#
# Simple cron-intended script for Borg Backup
#
# Author: Michael Goodwin Date: 2018-02-23
# Exit on error immediately
# This is important if you want to make sure that each step
# only runs if the previous step exited with a 0 status.
# Like a universal && everywhere.
set -e
# Vars and Configuration
#BORG_PASSPHRASE=
BORG_PASSCOMMAND="cat $HOME/.borg_password"
BORG_REPO='/root/borg/mongodb'
REPO_TYPE='repokey-blake2'
COMPRESSION="auto,zstd"
VERBOSE="-vs"
PREFIX='{hostname}-cron'
KEEP_WITHIN="7d"
DAILY=60
WEEKLY=
MONTHLY=6
YEARLY=
export ${!BORG_*}
do_borg_init() {
if [[ ! -d "${BORG_REPO}" ]]; then
mkdir -p "${BORG_REPO%/*}" \
&& eval borg init ${REPO_TYPE:+-e ${REPO_TYPE}} \
"${BORG_REPO}"
fi
}
do_borg_create() {
eval borg create ${COMPRESSION:+-C ${COMPRESSION}} $VERBOSE \
::"${PREFIX}-"'{now:%Y-%m-%dT%H:%M:%S}' "${PATHS[@]}"
}
do_borg_prune() {
eval borg prune $VERBOSE ${PREFIX:+--prefix ${PREFIX}} \
${KEEP_WITHIN:+--keep-within=${KEEP_WITHIN}} \
${DAILY:+-d ${DAILY}} ${WEEKLY:+-w ${WEEKLY}} \
${MONTHLY:+-m ${MONTHLY}} ${YEARLY:+-y ${YEARLY}}
}
#####################################################
##### PRE-BORG VARS - Define vars here that you need
TEMPDIR="/tmp/mongodb_backup"
##### BACKUP PATHS - Dirs/files to backup - recursive
PATHS=(
"$TEMPDIR"
"/var/lib/rocket.chat"
)
##### PRE-BORG - Put stuff here to run before borg
mkdir -p -m 0700 "${TEMPDIR}"
find "${TEMPDIR:?ERROR UNSET VARIABLE}" -mindepth 1 -delete
/bin/mongodump -o "$TEMPDIR" &&
##### END PRE-BORG ###############################
##################################################
do_borg_init && do_borg_create && do_borg_prune
##################################################
##### POST-BORG - Put stuff here to run after borg
aws s3 sync --delete /root/borg/ s3://borg-chat.opsech.io
##### END POST-BORG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment