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!