Automated VPS Backup Strategy: BorgBackup, Restic & Retention Guide

Step-by-step engineering guide to designing an automated Linux VPS backup strategy with BorgBackup, client-side encryption, and systemd scheduling.

Relying solely on local RAID arrays for data protection is an operational anti-pattern. While hardware RAID NVMe arrays protect against physical drive failure, they offer zero protection against accidental file deletions, database corruptions, malicious intrusions, or software misconfigurations. A robust, automated off-host backup strategy is essential for every production Linux VPS.

The Responsibilities of VPS Backup Architecture

Under StreamData Networks' standard service terms, unmanaged VPS instances place complete operational responsibility for application-level data and backup policies on the system administrator. While the underlying hypervisors operate on redundant storage fabrics, host-level redundancy is not a substitute for versioned, point-in-time archives stored on an isolated off-site target.

An effective backup strategy must achieve three technical goals: client-side encryption so credentials never leave the host unencrypted, deduplication to prevent storage bloat across daily runs, and automated execution via reliable system scheduling daemons.

BorgBackup Architecture: Chunking, Deduplication & Encryption

BorgBackup (Borg) is an open-source, deduplicating archiver designed specifically for automated server backups. Unlike traditional tarball scripts that re-copy entire directory trees every night, Borg breaks files into variable-length chunks using rolling hashes.

When files are modified, only the changed chunks are compressed, encrypted, and transmitted to the backup repository. This content-defined chunking delivers massive storage savings, allowing administrators to retain months of daily snapshots in a fraction of the raw disk space.

To initialize an encrypted remote repository over SSH, install Borg via your package manager and run the initialization command:

# Initialize an authenticated, encrypted repository
borg init --encryption=repokey-blake2 ssh://backupuser@storage.example.com/backups/vps-repo

Secure the passphrase in an environment variable or protected configuration file accessible only by root (chmod 600). The Blake2 authenticated encryption mode ensures both high cryptographic security and fast throughput on modern x86-64 processors.

Writing the Automated Backup and Pruning Script

Create a production shell script at /usr/local/bin/vps-borg-backup.sh that executes the backup, prunes obsolete archives according to an explicit retention policy, and compacts the repository:

#!/bin/bash
set -euo pipefail

export BORG_REPO="ssh://backupuser@storage.example.com/backups/vps-repo"
export BORG_PASSPHRASE="YourSecurePassphraseHere"

ARCHIVE_NAME="vps-backup-$(date +%Y-%m-%d_%H%M%S)"

echo "[$(date)] Starting Borg backup: ${ARCHIVE_NAME}..."

# 1. Create the backup archive
borg create     --verbose     --stats     --compression zstd,3     --exclude '/tmp/*'     --exclude '/var/tmp/*'     --exclude '/proc/*'     --exclude '/sys/*'     ::"${ARCHIVE_NAME}"     /etc /home /var/www /root

# 2. Prune old archives based on grandfather-father-son retention
echo "[$(date)] Pruning expired archives..."
borg prune     --list     --keep-daily=7     --keep-weekly=4     --keep-monthly=6     "${BORG_REPO}"

# 3. Compact the repository to release freed storage chunks
echo "[$(date)] Compacting repository..."
borg compact "${BORG_REPO}"

echo "[$(date)] Backup completed successfully."

Make the script executable with sudo chmod +x /usr/local/bin/vps-borg-backup.sh.

Scheduling Backups with systemd Service and Timer Units

While legacy cron jobs can execute backup scripts, modern Linux distributions benefit significantly from systemd timers. Systemd provides centralized journal logging, prevents overlapping execution if a backup run takes longer than expected, and supports Persistent=true to execute missed jobs if the server was rebooted during the scheduled window.

Create the service unit file at /etc/systemd/system/borg-backup.service:

[Unit]
Description=Automated Borg VPS Backup Service
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/vps-borg-backup.sh
ProtectSystem=full
ProtectHome=read-only

Next, create the corresponding timer unit at /etc/systemd/system/borg-backup.timer:

[Unit]
Description=Nightly Timer for Automated Borg Backup

[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true

[Install]
WantedBy=timers.target

Enable and activate the timer unit:

sudo systemctl daemon-reload
sudo systemctl enable --now borg-backup.timer

Failure Notifications and Backup Health Telemetry

Automated backup scripts must alert engineers immediately if an execution step fails. You can add an error trap directly into your bash script to dispatch an HTTP webhook or email alert:

# Dispatch notification on failure
trap 'curl -s -X POST -H "Content-Type: application/json" -d "{"text":"ALERT: Backup failed on $(hostname)"}" https://alerts.yourdomain.com/webhook' ERR

To inspect timer execution status and confirm next scheduled execution times across your fleet, run:

sudo systemctl list-timers --all
sudo journalctl -u borg-backup.service -n 50 --no-pager

Disaster Recovery and Restore Verification Testing

A backup that has never been tested is not a reliable backup. Production disaster recovery protocols require scheduled restore drills. Borg provides efficient tools to inspect and restore files without extracting entire archives:

  • List Archives: borg list $BORG_REPO displays every snapshot timestamp and status.
  • FUSE Mount: borg mount $BORG_REPO /mnt/backup allows you to mount the entire deduplicated repository as a virtual read-only filesystem to browse and retrieve individual files.
  • Direct Extraction: borg extract $BORG_REPO::vps-backup-2026-10-15_023000 path/to/restore extracts specific directory paths directly into your current working directory.

Backup Architecture Best Practice Summary

Component Recommended Tool / Setting Operational Rationale
Archival Engine BorgBackup / Restic Deduplication and client-side encryption minimize bandwidth and storage overhead.
Storage Target Dedicated Off-Site Storage Node Isolation from hypervisor failure domains or compromised credentials.
Scheduling systemd Timers Journald logging, overlap prevention, and catch-up execution via Persistent=true.
Retention Policy 7 Daily, 4 Weekly, 6 Monthly Balanced recovery window covering recent bugs and older historical baselines.

StreamData Networks delivers high-performance KVM cloud virtual private servers equipped with high-speed RAID NVMe arrays, multi-gigabit network interfaces, and full root access for custom backup automation. Review our KVM Cloud VPS specifications, explore bare-metal solutions on Dedicated Servers, or learn about our infrastructure on the Company Overview.


Sources