IT/Software/Backup Programs/Borg Backup

From msgwiki
Revision as of 17:51, 2 February 2024 by Itvte (talk | contribs)
Jump to navigation Jump to search

About

Borg Backup is a backup program that features compression deduplication, data compression and runs nicely over SSH.

https://www.borgbackup.org/

This page is incomplete.

Setup

BorgBackup must be installed on both a client and server machine to perform remote backups. This is a good thing because it requires much much less bandwidth to perform backups and is much less latency dependent.

To setup the client, simply install borgbackup. It is in the ubuntu repositories. It's a little bit out of date in 22.04 (version 1.2.0, latest stable at time of writing is 1.2.7 and 23.04 on all have development version 2.0), but not an issue.

To setup the server, there are a few more steps.

It is recommended to create a dedicated user for borg for security. Then setup passwordless ssh login for that user using a key file with the client machine. Not necessary but makes things much easier.

For security again, add the following to the beginning of the authorized_keys entry that contains the client public key.

command="borg serve --restrict-to-repository /path/to/repo",restrict

It should look like this:

command="borg serve --restrict-to-repository /path/to/repo",restrict ssh-rsa AbCgnbiuorgurigt743GREG4r43d...B3= username@clienthostname

This forces any login using that private key to run the command borg serve, which disallows any other commands.

See the borg serve docs and the borg Hosting repositories docs for more possible configurations.

Finally, run one of the following to initialize the repository.

# On the server
borg init -e=none /path/to/repo 

# On the client
borg init -e=none ssh://username@serverhostname/path/to/repo 

The repo is setup and connection is established between the server and client. Now you just need to setup a cronjob to perform a backup.

TODO: cronjob to backup

Usage

There are several important commands to understand borg. Note that all options (arguments with a - like -s or --progress) MUST come before or after positional arguments such as a repo URL, and not between.

init

This command is used to create a new borg repository. This repository can be created anywhere you have access, such as on a local mounted disk, or on a remote borg instance over ssh. The syntax is simple, but requires the -e flag for encryption settings. Choose none for no encryption, or repokey for standard SHA-256 encryption. The examples will all be no encryption as we don't require it.

# At-a-glance syntax
borg init -e=none <repository-location>

# To create a repository in a directory on the local machine. 
# The directory should already exist, or supply --make-parent-dirs to borg
borg init -e=none /path/to/repo

# To create a repository on a remote machine
borg init -e=none ssh://username@hostname/path/to/repo 

# To create a repository on a remote machine relative to the user's home directory
borg init -e=none ssh://user@hostname/~/Documents/repository

Creating a local repository from machine A is equivalent to creating a remote repository on machine A from machine B over ssh.

create

extract

list

prune

compact

mount

serve

Creating a backup

When creating a backup we need to specify what kind of compression we want to use, where the repo we want to backup to is, and where the source files we want to backup are located.

borg create -C auto,lzma --progress repo/location/::name-of-backup location/to/be/backed/up

LZMA compression uses more CPU and less storage space.

Name of backup must be unique so using the date command instead of a static name is desirable when automating backups.

... repo/location/::`date '+%Y-%m-%d-%H.%M.%S'` location/to/be/backed/up

Backing up over SSH

In all Borg commands we can use ssh://ip.of.server/repo/location/on/server.

borg create -C auto,lzma --progress ssh://my.backup.server/repo/location/::name-of-backup location/to/be/backed/up


Viewing a repos backups

To list all the backups in a repo we can run the following:

borg list /path/to/repo


Restoring from a backup

We can mount a Borg backup as if it was a regular drive anywhere in the filesystem.

borg mount /path/to/repo/::backupName mountPoint/

We can pull files from the backup as if it were a regular drive.

To unmount the backup we can run:

umount mountpoint/


Pruning old backups

By default Borg will keep backups forever.

We can prune backups by running borg prune.

borg prune -v --list --keep-hourly=48 --keep-daily=30 --keep-monthly=12 /path/to/repo/

In this example we will assume a backup job is running hourly.

In this example we will keep 1 backup per hour for the past 48 hours, 1 backup per day for the past 30 days, and 1 backup per month for the past 12 months.

Borg will keep the most recent backup from the time period it is pruning.

In the example we would keep the backup ran at 23:00 for the past 30 days and the last backup of the month for the monthly.

Backup Scripting

Example:

#!/bin/bash
cd /location/to/be/backed/up
borg create -C auto,lzma --progress /path/to/repo/::`date '+%Y-%m-%d-%H.%M.%S'` .
borg prune -v --list --keep-hourly=48 --keep-daily=30 --keep-monthly=12 /path/to/repo/