Borg Backup Notes

by Ramses Soto-Navarro, ramses@sotosystems.com


Overview
Install
Quick Start
Daily Script
List Archives
Extract Restore
Delete Archive
Prune
Mount Archive
Export Tarball
Install SSHFS
Remote Restore
Prune


Overview

Brief notes about Borg; an executable for backups with many cool features: deduplication, high compression, encryption, mountable archives, authentication security, offsite backups via SSH, BSD license. Follow the logic; for experienced Linux administrators.

Install

Download latest from: https://github.com/borgbackup/borg/releases

# cd /usr/local/bin
# wget https://github.com/borgbackup/borg/releases/download/1.1.16/borg-linux64
# chmod 0755 borg-linux64
# ln -s borg-linux64 borg
# borg -h

Quick Start

Create backup repository; leave password blank for testing. Backup /u1/ directory into the /backup/ archive called Monday. Next day will be a lot quicker, since only new data is stored (deduplication). Show the stats. Create the cronjob to run daily.

$ borg init -e repokey /backup
$ borg create --compression zlib /backup::Monday /u1
$ borg create --compression zlib --stats /backup::Tuesday /u1
$ crontab -e

# m h  dom mon dow   command
# Borg daily 11PM.

* 23 * * * 1-5 /root/bin/myborg-backup.sh

Daily Script

Create a backup script to backup remote nodes via pull. Initialize the archive. Create configuration script for each node to backup. Last, a backup plan script to backup all the nodes one after the other via a cronjob. NOTE: first fuse-sshfs needs to be installed, as shown in sections at the bottom.

# borg init -e repokey /backup/node1
# vi ~/bin/myborg.sh

#!/bin/bash
#
# Borg backup script.
# by Ramses Soto-Navarro <ramses@sotosystems.com>, 04/13/2021

SYNTAX="Syntax: $0 <configfile.conf>"

if [ $# -lt 1 ]; then
  echo $SYNTAX
  exit 1
fi

. $1

if [ $? -gt 0 ]; then
  echo $SYNTAX
  exit 1
fi

sshfs -o ro $BORG_HOST:/ $D2
cd $BORG_REPO
logger myborg.sh "Starting Borg backup for $BORG_HOST."
borg create --exclude-from=$BORG_EXCLUDE_LIST --compression zlib $BORG_MOUNT::$BORG_JOB $BORG_BASE
cd
umount $BORG_REPO
rm $BORG_EXCLUDE_LIST
logger myborg.sh "End of Borg backup for $BORG_HOST."
# vi ~/bin/myborgall.sh 
#!/bin/bash
#
# Run all the borg jobs one after the other

LIST="
node1
node2
"

for EACH in $LIST ; do
  /root/bin/myborg.sh /backup/$EACH.conf
  echo $EACH
done
# vi /backup/node1.conf 

#!/bin/bash
#
# borg backup config file.

BORG_HOST=node1
BORG_BASE="."

BORG_DATE=`date +%Y%m%d%H%M-$RANDOM`
BORG_JOB=$BORG_HOST-$BORG_DATE
BORG_MOUNT=/backup/$BORG_HOST
BORG_REPO=/mnt/$BORG_HOST 
BORG_EXCLUDE="dev lost+found media mnt proc run srv sys tmp opt/tmp"
BORG_EXCLUDE_LIST=/tmp/$BORG_EXCLUDE_LIST-$BORG_JOB.txt

for a in $BORG_EXCLUDE ; do echo $a ; done > $BORG_EXCLUDE_LIST

mkdir -p $BORG_MOUNT
mkdir -p $BORG_REPO

CHECK1=`ssh $BORG_HOST uptime | grep -i "load average"`
if [ "$CHECK1" == "" ]; then
  echo "$BORG_HOST not reachable."
  exit 1
fi

List Archives

$ borg list /backup
$ borg list /backup::Monday

Extract Restore

Extract (restore) files. Verify first to default cd to the directory where you want to restore.

$ borg extract /backup::Monday
$ borg extract /backup::Monday home/rasoto/Documents

Delete Archive

Delete archive:

$ borg delete /backup::Monday

Prune

Prune archives, last 7 daily, last 4 weekly and end of month:

$ borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=-1 /backup

Mount Archive

# borg mount /backup/node1 /mnt/tmp/

# ls /mnt/tmp/
20210401a  20210401b  20210401c

# pwd
/mnt/tmp/20210401a

# find | head
.
./u1
./u1/clients
./u1/clients/broward.edu
./u1/clients/broward.edu/.ssh
./u1/clients/broward.edu/.ssh/testftp
./u1/clients/broward.edu/.ssh/id_rsa
./u1/clients/broward.edu/.ssh/id_rsa.ppk
./u1/clients/broward.edu/.ssh/id_rsa.pub
./u1/clients/broward.edu/.ssh/id_rsa.pub.ppk

Export Tarball

To export a backup to a tar ball:

# borg export-tar --tar-fiflter="gzip -9" /backup::Monday Monday.tar.gz

Install SSHFS

SSHFS mounts systems remotely via SSH. Install sshfs on backup host. If your distro does not have SSHFS then find the best available RPM via rpm.pbone.net.

rpm -ivh fuse-sshfs-2.8.5.el8.x86_64.rpm

Verify that SSH keys are set (beyond scope of this document).

Mount remote system read-only:

# sshfs -o ro server1:/ /mnt/server1

Refer to Backup Quick Start above to continue.

Remote Restore

Simply mount the backup, as described in sections above, then transfer them over to the restore node via rsync, SFTP or an SSHFS mount.

Prune

TODO: Create a cron script to prune old backup jobs that are more than 2 weeks old, via the “borg delete” function.


The End.