Borg bash wrapper

Borg backup is an efficient and secure way to backup data. It is space efficient, fast, uses compression and deduplication as well as authentication and encryption.

I use my simple wrapper with a single repo.

borg list

The bl command lists all archives of the repository and is able to filter them with an argument (optional). It also sorts the list of archives by name instead of date.


function bl() {
  borg list $REPOSITORY | grep ${1:-''} | sort

borg backup

The bb command creates a new archive with the target folder and current timestamp as unique name. It also takes care of any database with the same name and dumps it into an sql file1. It also excludes all the dependencies one could think of such as node_modules, composer’s vendor directory or the virtual environments Python uses.

  function bb() { CWD=$(pwd) DIR=${1:-$CWD} ARCHIVE=$(basename $DIR)
    DATABASES=$(mysql -NB -e 'SHOW DATABASES' | grep -vE 'mysql|test|*_schema')

    cd $(realpath $DIR) echo $DATABASES | grep -q $ARCHIVE && mysqldump $ARCHIVE >
    $DIR/$ARCHIVE.sql borg create $REPOSITORY::$ARCHIVE'@{now:%s}' . -C lz4
    --exclude-from $EXCLUDESFILE rm -f $DIR/$ARCHIVE.sql cd $CWD }

borg delete

The bd command deletes not only one, but all the archives of the respository of a given name. Use an empty set of quotes to delete them all.

function bd() {
  ARCHIVES=$(borg list --short $REPOSITORY | grep $1)

  while read ARCHIVE; do
    borg delete $REPOSITORY::$ARCHIVE
  done <<< $ARCHIVES

borg restore

The br command extracts the first archive found by a given name to the global temporary folder with the unique archive name created as folder and cd’s into it then.

function br() {
  ARCHIVE=$(borg list --short $REPOSITORY | grep ${1%/} | tail -1)

  mkdir -p /tmp/$(basename $REPOSITORY)/$ARCHIVE && cd $_
  borg extract $REPOSITORY::$ARCHIVE --exclude-from $EXCLUDESFILE

  1. This one is an easy convention I prefer to stick to.↩︎