Introduction
BorgBackup (short for Borg) is a deduplicating backup program that offers efficient, secure, and space-saving backups. With features like compression, encryption, and data deduplication, Borg is an excellent choice for protecting your VPS data against loss or corruption.
Space Efficient
Deduplication stores only unique data chunks
Encrypted
AES-256 encryption protects your data at rest
Compression
Multiple algorithms (lz4, zstd, zlib) reduce storage
Verifiable
Built-in consistency checking ensures integrity
Install BorgBackup
Ubuntu/Debian
# Update package list
sudo apt update
# Install BorgBackup
sudo apt install -y borgbackup
# Verify installation
borg --versionRocky Linux/AlmaLinux/CentOS
# Enable EPEL repository
sudo dnf install -y epel-release
# Install BorgBackup
sudo dnf install -y borgbackup
borg --versionInstall Latest Version (All Distributions)
# Install dependencies
# Ubuntu/Debian:
sudo apt install -y python3-pip python3-dev libssl-dev libacl1-dev libfuse-dev pkg-config
# Rocky/RHEL:
sudo dnf install -y python3-pip python3-devel openssl-devel libacl-devel fuse-devel pkgconfig
# Install BorgBackup
sudo pip3 install borgbackup
borg --versionCreate a Backup Repository
Local Repository
sudo mkdir -p /backup/borg-repo
sudo borg init --encryption=repokey /backup/borg-repo
# You'll be prompted to create a passphrase - SAVE THIS SECURELY!Remote Repository (SSH)
borg init --encryption=repokey ssh://user@backup-server.com/~/backups/myserver
# Or with specific port
borg init --encryption=repokey ssh://user@backup-server.com:2222/~/backups/myserverEncryption Modes
- repokey: Encryption key stored in repository (requires passphrase)
- keyfile: Encryption key stored locally in ~/.config/borg/keys/
- repokey-blake2: Same as repokey but with BLAKE2b hashing (faster)
- none: No encryption (not recommended)
For most use cases, repokey or repokey-blake2 is recommended.
Export Your Encryption Key!
Without the key and passphrase, your backups are unrecoverable.
borg key export /backup/borg-repo ~/borg-key-backup.txt
# Securely copy this file to multiple safe locations!Create Your First Backup
Basic Backup Command
sudo borg create \
--stats \
--progress \
--compression lz4 \
/backup/borg-repo::backup-$(date +%Y%m%d-%H%M%S) \
/home \
/etc \
/var/www \
/root
# Enter your repository passphrase when promptedAdvanced Backup with Exclusions
sudo borg create \
--stats \
--progress \
--compression lz4 \
--exclude '/home/*/.cache' \
--exclude '*.tmp' \
--exclude '/var/cache' \
--exclude '/var/tmp' \
/backup/borg-repo::backup-$(date +%Y%m%d-%H%M%S) \
/home \
/etc \
/var/www \
/rootCompression Options
- lz4: Fast compression, moderate size reduction (recommended for most cases)
- zstd,3: Balanced compression (good speed and size)
- zlib,6: Better compression, slower
- none: No compression
Managing Backups
List All Backups
borg list /backup/borg-repoView Backup Contents
# List files in a specific backup
borg list /backup/borg-repo::backup-20250113-140000
# Search for specific files
borg list /backup/borg-repo::backup-20250113-140000 | grep nginx.confCheck Repository Integrity
# Quick check
borg check /backup/borg-repo
# Thorough verification
borg check --verify-data /backup/borg-repoGet Backup Statistics
# Show space usage
borg info /backup/borg-repo
# Show stats for specific backup
borg info /backup/borg-repo::backup-20250113-140000Restoring Data
Extract Entire Backup
# Create restore directory
mkdir -p /restore
# Extract entire backup
cd /restore
sudo borg extract /backup/borg-repo::backup-20250113-140000Restore Specific Files
# Restore single file
cd /restore
sudo borg extract /backup/borg-repo::backup-20250113-140000 etc/nginx/nginx.conf
# Restore directory
sudo borg extract /backup/borg-repo::backup-20250113-140000 var/www/htmlMount Backup for Browsing
# Create mount point
mkdir -p /mnt/borg
# Mount backup
borg mount /backup/borg-repo::backup-20250113-140000 /mnt/borg
# Browse files
ls -la /mnt/borg
# Unmount when done
borg umount /mnt/borgPruning Old Backups
Regularly prune old backups to save space while maintaining backup history.
Standard Retention Policy
# Keep: 7 daily, 4 weekly, 6 monthly backups
sudo borg prune --list --keep-daily=7 --keep-weekly=4 --keep-monthly=6 /backup/borg-repoRetention Strategy Examples
borg prune --keep-daily=14 --keep-weekly=8 --keep-monthly=12 --keep-yearly=2borg prune --keep-daily=3 --keep-weekly=2 --keep-monthly=3Automation with Systemd
Create Backup Script
#!/bin/bash
# BorgBackup script for automated backups
# Repository location
BORG_REPO="/backup/borg-repo"
# Passphrase (consider using a more secure method in production)
export BORG_PASSPHRASE='your-secure-passphrase'
# Directories to backup
BACKUP_PATHS="/home /etc /var/www /root"
# Exclusions
EXCLUDE_PATTERNS="--exclude /home/*/.cache --exclude '*.tmp' --exclude /var/cache --exclude /var/tmp"
# Create backup
borg create --verbose --stats --compression lz4 ${EXCLUDE_PATTERNS} ${BORG_REPO}::backup-$(date +%Y%m%d-%H%M%S) ${BACKUP_PATHS}
# Prune old backups
borg prune --verbose --list --keep-daily=7 --keep-weekly=4 --keep-monthly=6 ${BORG_REPO}
# Check repository
borg check ${BORG_REPO}
# Unset passphrase
unset BORG_PASSPHRASEsudo chmod +x /usr/local/bin/borg-backup.shCreate Systemd Service
[Unit]
Description=BorgBackup Service
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/borg-backup.sh
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
[Install]
WantedBy=multi-user.targetCreate Systemd Timer
[Unit]
Description=BorgBackup Timer
Requires=borg-backup.service
[Timer]
# Run daily at 2 AM
OnCalendar=daily
OnCalendar=02:00
Persistent=true
[Install]
WantedBy=timers.target# Reload systemd
sudo systemctl daemon-reload
# Enable timer to start on boot
sudo systemctl enable borg-backup.timer
# Start timer
sudo systemctl start borg-backup.timer
# Check timer status
sudo systemctl status borg-backup.timer
# List all timers
systemctl list-timers --all | grep borgRemote Backup Configuration
Setting Up SSH Keys
# Generate SSH key (if not already done)
ssh-keygen -t ed25519 -C "borg-backup"
# Copy public key to remote server
ssh-copy-id user@backup-server.com
# Test connection
ssh user@backup-server.comSSH Config for Simplified Access
Host borgbackup
HostName backup-server.com
User backupuser
Port 22
IdentityFile ~/.ssh/id_ed25519
Compression yes
ServerAliveInterval 60Now you can use: borg create borgbackup:repo::backup-$(date +%Y%m%d)
Monitoring and Notifications
Email Notifications
sudo apt install -y mailutils# At the end of script
if [ $? -eq 0 ]; then
echo "Backup completed successfully" | mail -s "Borg Backup Success" admin@example.com
else
echo "Backup failed! Check logs." | mail -s "Borg Backup FAILED" admin@example.com
fiHealthchecks.io Integration
# Add at start of script
curl -fsS -m 10 --retry 5 https://hc-ping.com/YOUR-UUID/start
# Add at end of script
curl -fsS -m 10 --retry 5 https://hc-ping.com/YOUR-UUIDTroubleshooting
Repository is Locked
# Check for stale locks
borg break-lock /backup/borg-repo
# Verify no other borg processes are running
ps aux | grep borgInsufficient Space
# Check space usage
df -h /backup
# Prune more aggressively
borg prune --keep-daily=3 /backup/borg-repo
# Compact repository (Borg 1.2+)
borg compact /backup/borg-repoSlow Backups
# Use faster compression
--compression lz4
# Exclude cache directories
--exclude-caches
# Check disk I/O
iostat -x 1Verify Backup Integrity
# Quick check
borg check /backup/borg-repo
# Full verification
borg check --verify-data /backup/borg-repo
# Repair if issues found
borg check --repair /backup/borg-repoBest Practices
Test Restores Regularly
Schedule monthly test restores to verify backup integrity.
Store Keys Securely
Keep backup encryption keys in multiple secure locations.
Monitor Disk Space
Set up alerts for backup destination capacity.
Use Remote Storage
Always maintain off-site backups for disaster recovery.
You're All Set!
BorgBackup provides a robust, efficient, and secure backup solution for your RamNode VPS.
Key takeaways:
- Always test your backups with regular restore drills
- Store encryption keys securely in multiple locations
- Monitor backup jobs and disk space usage
- Maintain off-site backups for true disaster recovery
