Golden Codes - armanexplorer planet

Practical code snippets for Django, Python, Bash, Git and All!

View on GitHub

Docs Hetzner Docs

install latest version of borg

install

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
sudo apt-get install libfuse3-dev fuse3  # needed for pyfuse3

pipx install borgbackup # needs pkg-config (installed globally with above installs)
pipx inject borgbackup llfuse
pipx inject borgbackup pyfuse3

config repo

configure the git config

nano ~/.ssh/config

Host ht-storage
    HostName uXXXXXX.example.com
    User uXXXXXX-subY
    Port YOUR_PORT
    # uncomment this after setting the ssh key
    # IdentityFile ~/.ssh/id_ed25519

config other things

cat ~/.ssh/id_ed25519.pub | ssh ht-storage install-ssh-key

export BORG_PASSPHRASE='XXXXXXXXXXXXX'
export BORG_RSH='ssh -i /home/user/.ssh/id_ed25519'

borg init --encryption=repokey ssh://ht-storage/./backups
borg create ssh://ht-storage/./backups::test-{now:%Y-%m-%d} ~/olds
borg list ssh://storage-host/./backups

# set default repo to be used in following commands (then we can use :: instead of ssh://ht-storage/./backups::)
export BORG_REPO=ssh://ht-storage/./backups

mount

# create empty directory to mount borg repo or its archives
mkdir /tmp/borg_mount

# mount an archive of current repo
borg mount ::2024_6_21_initial /tmp/borg_mount

# mount the whole repo
borg mount :: /tmp/borg_mount

compression

LZMA (or xz) compression is a type of data compression algorithm used in the provided borg command. Here's a breakdown of its role:

Alternative Compression Options in borg:

The script uses lzma as an example, but borg supports other compression algorithms:

Choosing the Right Algorithm:

The best compression algorithm depends on your priorities. If storage space is limited, LZMA is a good choice. If speed is crucial, consider zstd or even lz4. Experiment and find the best balance between compression and speed for your needs.

If you don't specify the --compression option in borg, it will default to using the lz4 algorithm.

Here's why:

Specifying Compression:

To override the default and use a different compression algorithm, use the --compression flag followed by the desired algorithm name. Here are some examples:

Choosing Wisely:

The optimal compression choice depends on your needs. Here's a quick guideline:

By understanding the default behavior and available compression algorithms, you can configure borg to best suit your backup requirements.

prune

borg prune --dry-run --list --stats --glob-archives "gitlab*" --keep-minutely 1

borg prune --dry-run --list --stats --glob-archives "gitlab*" --keep-last 1

A good procedure is to thin out more and more the older your backups get. As an example, --keep-daily 7 means to keep the latest backup on each day, up to 7 most recent days with backups (days without backups do not count).

The rules are applied from secondly to yearly, and backups selected by previous rules do not count towards those of later rules. The time that each backup starts is used for pruning purposes. Dates and times are interpreted in the local timezone, and weeks go from Monday to Sunday. Specifying a negative number of archives to keep means that there is no limit. As of borg 1.2.0, borg will retain the oldest archive if any of the secondly, minutely, hourly, daily, weekly, monthly, or yearly rules was not otherwise able to meet its retention target. This enables the first chronological archive to continue aging until it is replaced by a newer archive that meets the retention criteria.

The --keep-last N option is doing the same as --keep-secondly N (and it will keep the last N archives under the assumption that you do not create more than one backup archive in the same second).

delete

 borg delete --dry-run --list --stats ::gitlab_backup_XXX-XX-XX