Dec 14, 2024 3 min read

BorgBackup: A Beginner-Friendly Guide to Self-Hosting

BorgBackup: A Beginner-Friendly Guide to Self-Hosting
Table of Contents

BorgBackup is a powerful, deduplication-based backup tool designed for efficiency, security, and flexibility. Itโ€™s an excellent choice for self-hosting, giving users control over data retention and storage while enabling advanced configuration options. In this guide, weโ€™ll cover installing BorgBackup, configuring it with Nginx for secure access, and using it for backups and restores. Additionally, weโ€™ll explore logging, troubleshooting, updates, and leveraging BorgBackupโ€™s unique features.

Installing BorgBackup

๐Ÿ“ฆ Docker/Docker Compose Setup

To deploy BorgBackup using Docker, create a docker-compose.yml file and launch the container. This setup ensures portability and isolates BorgBackup from the host system.


version: '3.8'

services:

borgbackup:

image: borgbackup/borg:latest

container_name: borgbackup

volumes:

- /path/to/local/repository:/mnt/backup

- /path/to/config:/config

restart: unless-stopped

Run the following commands to deploy BorgBackup:


nano docker-compose.yml

## Launch the container

docker-compose up -d

## Verify the container is running

docker ps

๐Ÿš€ Manual Installation

For a manual installation on Linux, use the following commands to install BorgBackup and its dependencies:


## Install dependencies

sudo apt update && sudo apt install -y borgbackup

## Verify the BorgBackup version

borg --version

This method is ideal for users who prefer not to use containers or need deep integration with the host system.

Configuring Nginx as a Reverse Proxy

๐ŸŒ Nginx Configuration

Set up Nginx to serve as a reverse proxy for BorgBackup. Create a server block configuration file:


sudo nano /etc/nginx/sites-available/borgbackup

Add the following content to route traffic to your BorgBackup container or service:


server {

listen 80;

server_name borgbackup.example.com;

location / {

proxy_pass http://127.0.0.1:8000; # Replace with the BorgBackup service port

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

Enable the configuration and restart Nginx:


sudo ln -s /etc/nginx/sites-available/borgbackup /etc/nginx/sites-enabled/

sudo nginx -t

sudo systemctl reload nginx

๐Ÿ”’ SSL/TLS Setup

Secure BorgBackup with an SSL certificate using Letโ€™s Encrypt:


sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx -d borgbackup.example.com

## Automate certificate renewal

sudo systemctl enable certbot.timer

๐Ÿ› ๏ธ Testing and Reloading Nginx

Validate your configuration and reload Nginx to apply changes:


sudo nginx -t

sudo systemctl reload nginx

Visit https://borgbackup.example.com to ensure everything is working securely.

Logging and Debugging BorgBackup

๐Ÿ—ƒ๏ธ Enabling Debug Logs

To enable debug-level logging in BorgBackup, use the --debug flag:


borg create --debug /mnt/backup::my-backup /path/to/data

๐Ÿ“„ Viewing Logs

If youโ€™re running BorgBackup in Docker, view the logs with:


docker logs borgbackup

For manual installations, check the output of the commands directly or use a log file.

๐Ÿ› ๏ธ Troubleshooting Common Issues

Use the logs to diagnose common errors, such as file permission issues or failed backups. For example:


borg check /mnt/backup

๐Ÿ“ค Exporting Logs

To export logs for advanced analysis, redirect them to a file or forward them to a logging stack like ELK:


borg create /mnt/backup::my-backup /path/to/data 2>&1 | tee /var/log/borgbackup.log

Backup and Restore

๐Ÿ—‚๏ธ File-Based Backups

Create a snapshot of a directory with the following command:


borg init --encryption=repokey /mnt/backup

borg create /mnt/backup::my-backup /path/to/data

๐Ÿ”„ Database Backups

Dump a database for backup, then include it in a BorgBackup snapshot:


## Dump the database

mysqldump -u root -p my_database > /path/to/dump.sql

## Include the dump in the backup

borg create /mnt/backup::db-backup /path/to/dump.sql

๐Ÿ“… Automated Backup Scripts

Automate backups using a cron job. Create a script:


#!/bin/bash

borg create /mnt/backup::$(date +%Y-%m-%d) /path/to/data

Make it executable and schedule it:


chmod +x /path/to/backup.sh

crontab -e

## Add the following line

0 2 * * * /path/to/backup.sh

Updating and Upgrading BorgBackup

โฌ†๏ธ Updating Docker Images

Pull and update the Docker container to the latest version:


docker-compose pull

docker-compose up -d

๐Ÿ› ๏ธ Manual Updates

For manual installations, update BorgBackup using your package manager:


sudo apt update && sudo apt upgrade -y borgbackup

๐Ÿ” Checking for Updates

Verify the latest version available from the official repository:


borg --version

Leveraging BorgBackupโ€™s Unique Features

๐Ÿ”ง Enabling APIs

BorgBackup doesnโ€™t expose APIs natively, but you can script operations using Python or shell commands. Example:


## Python script to check the backup repository

from borg import Repository

repo = Repository('/mnt/backup')

print(repo.list())

๐ŸŒŸ Advanced Configurations

Enable BorgBackupโ€™s prune feature to manage old backups dynamically:


borg prune --list --prefix '{hostname}-' --keep-daily=7 --keep-weekly=4 --keep-monthly=6 /mnt/backup

This ensures you retain only the most relevant backups while keeping storage usage optimal.

Wrapping Up

In this guide, weโ€™ve covered how to install, configure, and manage BorgBackup for efficient and secure backups. By self-hosting BorgBackup, you gain complete control over your data, storage, and backup processes. Use the examples provided to customize and optimize your setup, ensuring reliable and scalable backups tailored to your needs. Start implementing these steps today to secure your data with confidence!

Great! Youโ€™ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.