Personal Privacy

Login Advanced Search
     General TopicsSelf Hosted ServicesServer Setup

Backing up Data Using Borg Backup

Installation

First, log in as root, and install

sudo -i
apt -y install borgbackup
apt -y install python3 python3-dev python3-pip \
libssl-dev openssl \
libacl1-dev libacl1 \
liblz4-dev liblz4-1 \
build-essential

Create the following directories:

mkdir -p ~/.config/borg/keys

Next, create the backup structure

borg init --encryption=keyfile /backupfolderpath
For encyption can choose:

The key will be stored at ~./config/borg/keys

Next, create the bash file to run the backup

mkdir backup-script.sh

paste and edit:

#!/bin/sh
# Setting this, so the repo does not need to be given on the command line:
export BORG_REPO=/path/to/backup/folder
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
# Setting this, so you won't be asked for your repository passphrase:
export BORG_PASSPHRASE='PassPhrase'
# or this to ask an external program to supply the passphrase:
#export BORG_PASSCOMMAND='pass show backup'

# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM

info "Starting backup"

# Backup the most important directories into an archive named after
# the machine this script is currently running on:

borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
\
::'Server_{now}' \
/path/to/data1 \
/path/to/data2

backup_exit=$?

info "Pruning repository"

# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:

borg prune \
--list \
--prefix 'server-' \
--show-rc \
--keep-daily 1 \
--keep-weekly 0 \
--keep-monthly 0 \
--save-space

prune_exit=$?

# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))

if [ ${global_exit} -eq 1 ];
then
info "Backup and/or Prune finished with a warning"
fi

if [ ${global_exit} -gt 1 ];
then
info "Backup and/or Prune finished with an error"
fi

exit ${global_exit}

Make the file executable

chomd 700 backup-script.sh

Run the backup to ensure it is working. Depending on the size of the filder you are backing up, it might take a long time to finish the first time!

Add it to cron to run it every day:

crontab -e

and add

00 2 * * * /path/to/backup-script.sh

Extracting an Archive

If you chose keyfile encryption, make sure the encryption keyfile is copied to /root/.config/borg/keys
Then, go to the location of you want to mount your backup and run

borg list /path/to/backup/folder

to see the list of all archives available. Then run

borg extract /path/to/backup/folder::'archive_name'

Buy me a coffe?!


Comments

No comments yet!
Add a new comment:

6