borgbackup_script/README.md

4.8 KiB

Borgbackup Script

Once set up, this script helps create backups with borg in a very convenient way.

In fact, it's just a slightly more elaborated version of the example in the official borg documentation. It mainly focuses on the use of environment variables and also enables the distinction between manual and automated backups.

Preparation

Install Borg

The borg packages provided by package managers are usually not up to date. It is better to install borg via pip in a virtual environment.

Download dependencies.

sudo apt-get install python3 python3-dev python3-pip python3-virtualenv \
libacl1-dev libacl1 \
libssl-dev \
liblz4-dev libzstd-dev libxxhash-dev \
build-essential \
pkg-config python3-pkgconfig

sudo apt-get install libfuse-dev fuse    # needed for llfuse
# or
sudo apt-get install libfuse3-dev fuse3  # needed for pyfuse311

Prepare virutal environment.

sudo mkdir /opt/borg && cd /opt/borg
sudo python3 -m venv borg-venv

Install borg via pip.

source borg-venv/bin/activate
pip install -U pip setuptools wheel
pip install pkgconfig

pip install borgbackup
# or
pip install borgbackup[llfuse]  # to use llfuse
# or
pip install borgbackup[pyfuse3]  # to use pyfuse3

Link borg binary to $PATH.

cd /usr/local/bin
sudo ln -s /opt/borg/borg-venv/bin/borg borg

[ If the borg repository should be located on a remote server ]

IMPORTANT: borg must be installed also on the remote server!

Generate a dedicated ssh key for every user which will create backups.

ssh-keygen -t ed25519  # Don't set a password!

Print public key.

cat $HOME/.ssh/id_ed25519.pub
# Example output:
# ssh-ed25519 AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx someuser@somehost

Add the public key to your authorized_keys file on the server.

# ON REMOTE SERVER
echo 'command="borg serve --restrict-to-path ~/backups/<user>/<host>",restrict ssh-ed25519 AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx someuser@somehost' >> $HOME/.ssh/authorized_keys

Create the backup directory for the host.

mkdir -p ~/backups/<user>/<host>

Set up Borgbackup Script

Download the repository.

cd ~/Downloads
wget https://gitea.extrasolar.space/marc/borgbackup_script/archive/main.tar.gz
tar xzf main.tar.gz
mv borgbackup_script /opt/borgbackup

Copy the example environment file to the home directory(s) of the user(s) who will create backups. Then configure the .borg-env file.

cp /opt/borgbackup/.borg-env.example $HOME/.borg-env
sudo chown $USER. $HOME/.borg-env
sudo chmod 700 $HOME/.borg-env
nano $HOME/.borg-env
  • [ If the borg repository is located on a remote server: set the right path to the dedicated ssh key ]
  • Define the path to your borg repository
  • Set a strong passphrase
  • Define everything you want to include in your backup as an absolute path in the BORG_TARGETS array

Make the script executable.

sudo chmod +x /opt/borgbackup/borgbackup.sh

Link the script to the $PATH.

cd /usr/local/bin
ln -s /opt/borgbackup/borgbackup.sh borgbackup

Logs

Create a logfile with read and write permissions for every user.

sudo touch /var/log/backup.log
sudo chmod 666 /var/log/backup.log

Later, each backup procedure gets logged to that file.

Initialize borg repository

source $HOME/.borg-env
borg init --encryption=repokey

Don't forget to export your repository key and store it in a very safe place. The key can be exported in three different formats:

borg key export > encrypted-key-backup
borg key export --paper > encrypted-key-backup.txt
borg key export --qr-html > encrypted-key-backup.html

Usage

The backup script can be used to create backups either manually or scheduled.

Manual backup

Manual backups will not be pruned automatically. They are prefixed with man.

To create a manual backup just type:

borgbackup

Scheduled backups

Scheduled backups will be pruned automatically. There will be kept:

  • 7 daily backups
  • 4 weekly backups
  • 6 monthly backups
  • 2 yearly backups

Scheduled backups are prefixed with auto.

The scheduled backups can be automated by using cron. To create scheduled backups, place this line in your crontab (edit with crontab -e):

0 3 * * * export BORG_RSH='ssh -oBatchMode=yes' && /usr/local/bin/borgbackup auto /dev/null 2>&1

Using borg

Please refer to the official documentation to learn how to use borg.

Tip: Before you use a borg command, source the .borg-env file. This way, you do not have to type the path to the repository or the password.

source $HOME/.borg-env
borg info