Skip to content

Instantly share code, notes, and snippets.

@Freaky
Last active February 20, 2023 20:39
Show Gist options
  • Select an option

  • Save Freaky/81356a2bc7e54e4d7f5e9f2ee006ef1b to your computer and use it in GitHub Desktop.

Select an option

Save Freaky/81356a2bc7e54e4d7f5e9f2ee006ef1b to your computer and use it in GitHub Desktop.

Revisions

  1. Freaky revised this gist May 15, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions borg-backup.sh
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@
    # $ borg-backup.sh create
    # $ borg-backup.sh list
    # $ borg-backup.sh help
    # $ CONFIG=/etc/another-bord-backup.conf borg-backup.sh init
    # $ CONFIG=/etc/another-borg-backup.conf borg-backup.sh init

    set -eu

    @@ -32,7 +32,7 @@ COMPRESSION='zlib'
    PRUNE='-H 24 -d 14 -w 8 -m 6'

    err() {
    echo "$@"
    echo "$@" 1>&2
    exit 78
    }

  2. Freaky revised this gist Mar 8, 2017. 1 changed file with 74 additions and 25 deletions.
    99 changes: 74 additions & 25 deletions borg-backup.sh
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,44 @@
    #!/bin/sh
    #
    # Simple BorgBackup script by Thomas Hurst <[email protected]>
    # borg-backup.sh - BorgBackup shell script
    #
    # No warranty, slippery when wet, etc.

    BORG=/usr/local/bin/borg
    TARGET=backup@somehost:/some/backup/location
    COMPRESSION=zlib
    PRUNE='-H 24 -d 14 -w 8 -m 6'
    # Author: Thomas Hurst <[email protected]>
    #
    # No warranty expressed or implied. Beware of dog. Slippery when wet.
    # This isn't going to be your *only* backup method, right?
    #
    # Synopsis:
    #
    # $ touch /etc/borg-backup.conf && chmod go-rw /etc/borg-backup.conf
    # $ cat >>/etc/borg-backup.conf
    # TARGET=backup@host:/backups
    # PASSPHRASE='incorrect zebra generator clip'
    # BACKUPS='homes etc'
    # BACKUP_homes='/home -e /home/bob/.trash'
    # BACKUP_etc='/etc'
    # # (also available: COMPRESSION, PRUNE
    # ^D
    # $ borg-backup.sh init
    # $ borg-backup.sh create
    # $ borg-backup.sh list
    # $ borg-backup.sh help
    # $ CONFIG=/etc/another-bord-backup.conf borg-backup.sh init

    BACKUPS='etc homes'
    BACKUP_homes='/home/freaky -e /home/freaky/.rbenv'
    BACKUP_etc='/etc /usr/local/etc'
    export BORG_PASSPHRASE='some nice long high entropy passphrase'
    set -eu

    TIMESTAMP=`date +"%Y-%m-%dT%H:%M"`
    : "${BORG:=/usr/local/bin/borg}"
    : "${CONFIG:=/etc/borg-backup.conf}"
    COMPRESSION='zlib'
    PRUNE='-H 24 -d 14 -w 8 -m 6'

    set -eu
    err() {
    echo "$@"
    exit 78
    }

    usage() {
    echo "Borg: $BORG"
    echo "Config: $CONFIG"
    echo "Backups: $BACKUPS"
    echo "Location: $TARGET"
    echo "Compression: $COMPRESSION"
    @@ -36,20 +56,49 @@ usage() {
    echo " $0 borg backup [arbitrary borg command-line]"
    echo
    echo " e.g: $0 borg etc extract ::etc-2017-02-21T20:00 etc/rc.conf --stdout"
    exit $1
    exit "$1"
    }

    [ -z "$CONFIG" ] && err "CONFIG unset"
    [ -r "$CONFIG" ] || err "CONFIG $CONFIG unreadable"
    [ -f "$CONFIG" ] || err "CONFIG $CONFIG not a regular file"

    # shellcheck disable=SC1090
    . "$CONFIG"

    [ -e "$BORG" ] || err "$BORG not executable (see https://borgbackup.readthedocs.io/en/stable/installation.html)"
    [ -z "$PRUNE" ] && err "PRUNE not set (e.g. '-H 24 -d 14 -w 8 -m 6')"
    [ -z "${PASSPHRASE-}" ] && err "PASSPHRASE not set (e.g. 'incorrect zebra generator clip')"
    [ -z "${TARGET-}" ] && err "TARGET not set (e.g. 'backup@host:/backup/path')"
    [ -z "${BACKUPS-}" ] && err "BACKUPS not set (e.g. 'homes etc')"

    for B in $BACKUPS; do
    eval "DIRS=\$BACKUP_${B}"
    [ -z "${DIRS}" ] && err "BACKUP_${B} not set (e.g. '/home/bla -e /home/bla/.foo')"
    done

    export BORG_PASSPHRASE="$PASSPHRASE"

    TIMESTAMP=$(date +"%Y-%m-%dT%H:%M")

    nargs=$#
    cmd=${1-}
    backup=${2-}
    shift || true
    shift || true

    rc=0
    for B in $BACKUPS; do
    if [ $# -eq 1 -o "${2-}" = "$B" ] ; then
    export BORG_REPO=${TARGET}/${B}.borg
    if [ "$nargs" -eq 1 ] || [ "$backup" = "$B" ] ; then
    export BORG_REPO="${TARGET}/${B}.borg"
    eval "DIRS=\$BACKUP_${B}"
    case $1 in
    case $cmd in
    init)
    $BORG init --encryption=repokey || rc=$?
    ;;
    create)
    $BORG create --exclude-caches --compression=${COMPRESSION} -v -s ::${B}-${TIMESTAMP} $DIRS || rc=$?
    # shellcheck disable=SC2086
    $BORG create --exclude-caches --compression=${COMPRESSION} -v -s ::"${B}-${TIMESTAMP}" $DIRS || rc=$?
    ;;
    list)
    $BORG list || rc=$?
    @@ -58,20 +107,20 @@ for B in $BACKUPS; do
    $BORG check || rc=$?
    ;;
    prune)
    # shellcheck disable=SC2086
    $BORG prune -sv $PRUNE || rc=$?
    ;;
    info)
    [ $# -ne 3 ] && usage 64
    $BORG info ::$3 || rc=$?
    [ "$nargs" -ne 3 ] && usage 64
    $BORG info ::"$3" || rc=$?
    ;;
    delete)
    [ $# -ne 3 ] && usage 64
    $BORG delete -s -p ::$3 || rc=$?
    [ "$nargs" -ne 3 ] && usage 64
    $BORG delete -s -p ::"$3" || rc=$?
    ;;
    borg)
    [ $# -lt 2 ] && usage 64
    shift ; shift
    $BORG ${@} || rc=$?
    [ "$nargs" -lt 2 ] && usage 64
    $BORG "${@}" || rc=$?
    ;;
    help)
    usage 0
  3. Freaky revised this gist Mar 7, 2017. No changes.
  4. Freaky created this gist Mar 7, 2017.
    86 changes: 86 additions & 0 deletions borg-backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    #!/bin/sh
    #
    # Simple BorgBackup script by Thomas Hurst <[email protected]>
    #
    # No warranty, slippery when wet, etc.

    BORG=/usr/local/bin/borg
    TARGET=backup@somehost:/some/backup/location
    COMPRESSION=zlib
    PRUNE='-H 24 -d 14 -w 8 -m 6'

    BACKUPS='etc homes'
    BACKUP_homes='/home/freaky -e /home/freaky/.rbenv'
    BACKUP_etc='/etc /usr/local/etc'
    export BORG_PASSPHRASE='some nice long high entropy passphrase'

    TIMESTAMP=`date +"%Y-%m-%dT%H:%M"`

    set -eu

    usage() {
    echo "Backups: $BACKUPS"
    echo "Location: $TARGET"
    echo "Compression: $COMPRESSION"
    echo "Pruning: $PRUNE"
    echo
    echo "Usage:"
    echo " $0 help"
    echo " $0 init [backup]"
    echo " $0 create [backup]"
    echo " $0 list [backup]"
    echo " $0 check [backup]"
    echo " $0 prune [backup]"
    echo " $0 info backup archive"
    echo " $0 delete backup archive"
    echo " $0 borg backup [arbitrary borg command-line]"
    echo
    echo " e.g: $0 borg etc extract ::etc-2017-02-21T20:00 etc/rc.conf --stdout"
    exit $1
    }

    rc=0
    for B in $BACKUPS; do
    if [ $# -eq 1 -o "${2-}" = "$B" ] ; then
    export BORG_REPO=${TARGET}/${B}.borg
    eval "DIRS=\$BACKUP_${B}"
    case $1 in
    init)
    $BORG init --encryption=repokey || rc=$?
    ;;
    create)
    $BORG create --exclude-caches --compression=${COMPRESSION} -v -s ::${B}-${TIMESTAMP} $DIRS || rc=$?
    ;;
    list)
    $BORG list || rc=$?
    ;;
    check)
    $BORG check || rc=$?
    ;;
    prune)
    $BORG prune -sv $PRUNE || rc=$?
    ;;
    info)
    [ $# -ne 3 ] && usage 64
    $BORG info ::$3 || rc=$?
    ;;
    delete)
    [ $# -ne 3 ] && usage 64
    $BORG delete -s -p ::$3 || rc=$?
    ;;
    borg)
    [ $# -lt 2 ] && usage 64
    shift ; shift
    $BORG ${@} || rc=$?
    ;;
    help)
    usage 0
    ;;
    *)
    usage 64

    esac
    fi
    done

    exit $rc