Automated Encrypted Backups with Restic and Systemd Timers
Implement zero-trust, deduplicated, client-side encrypted server backups using Restic 0.17+, managed reliably with systemd service and timer units.
Netbay Engineering
Netbay Engineering
On this page
Traditional server backups relying on unencrypted tar archives, raw rsync scripts, or basic cron jobs suffer from three fatal operational flaws: they waste expensive remote storage by transferring redundant unchanged files, expose unencrypted sensitive credentials and database dumps on remote storage targets, and lack built-in cryptographic integrity verification to detect bit rot. Restic resolves all three challenges through a modern zero-trust architecture.
Files in Restic are chunked using content-defined chunking with Rabin-Karp fingerprints, deduplicated globally across all historical snapshots, and encrypted on the client using AES-256-CTR and Poly1305 before leaving the host. By pairing Restic 0.17+ with systemd service and timer units on an Ubuntu Linux instance, you eliminate brittle cron dependencies, capture structured journald logs, and ensure daily automated backups with granular retention policies.
Architectural Advantages of Content-Defined Chunking
Traditional block-level backups divide files into fixed-size chunks (e.g., 4MB). If a single byte is inserted at the beginning of a 10GB database backup, every subsequent fixed block boundary shifts, defeating deduplication and forcing a full re-upload.
Restic solves this through content-defined chunking (CDC). A rolling Rabin fingerprint scans the byte stream, splitting chunks only when specific mathematical conditions are met regardless of file offsets. If a byte is inserted, only the immediate chunk changes; all subsequent chunks retain their identical hashes and are instantly deduplicated. This ensures that incremental daily backups of multi-gigabyte application directories complete in seconds, transferring only net-new bytes over the network.
Initializing an Encrypted Restic Repository
Restic supports diverse storage backends including SFTP/SSH targets, local disks, and S3-compatible object stores. First, install the latest Restic binary on Ubuntu 24.04:
# Install Restic from official package archive
sudo apt-get update
sudo apt-get install -y restic
# Verify binary version and supported backends
restic versionCreate a dedicated secure credentials file readable only by the root user to store the repository location and encryption password:
sudo mkdir -p /etc/restic
sudo tee /etc/restic/backup.env << 'EOF'
RESTIC_REPOSITORY="sftp:backupuser@10.0.0.8:/var/backups/restic-repo"
RESTIC_PASSWORD="YourStrongCryptographicPasswordGoesHere"
EOF
sudo chmod 600 /etc/restic/backup.envInitialize the repository. This step writes encrypted master key files and config structures to the target storage:
# Export environment variables for initialization
set -a
. /etc/restic/backup.env
set +a
# Initialize the repository
restic initOnce initialized, no data can be read or modified without the master repository password.
Systemd Service and Timer Units
Systemd timers provide execution logging, timeout guards, and failure notifications that cron lacks. Create the one-shot service unit /etc/systemd/system/restic-backup.service:
# /etc/systemd/system/restic-backup.service
[Unit]
Description=Automated Restic Encrypted Backup
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
EnvironmentFile=/etc/restic/backup.env
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
# Backup critical directories with exclusions
ExecStart=/usr/bin/restic backup --exclude-caches --exclude=/tmp --exclude=/var/tmp --exclude=/var/cache /etc /var/www /srv /home
# Apply retention policy: keep 7 dailies, 4 weeklies, 12 monthlies
ExecStartPost=/usr/bin/restic forget --prune --keep-daily 7 --keep-weekly 4 --keep-monthly 12
[Install]
WantedBy=multi-user.targetNext, create the corresponding timer unit /etc/systemd/system/restic-backup.timer to trigger execution every night at 02:30 UTC:
# /etc/systemd/system/restic-backup.timer
[Unit]
Description=Nightly Restic Encrypted Backup Timer
[Timer]
OnCalendar=*-*-* 02:30:00
RandomizedDelaySec=1800
Persistent=true
[Install]
WantedBy=timers.targetThe Persistent=true directive guarantees that if the server is powered off during the scheduled time, systemd will execute the backup immediately upon the next boot.
sudo systemctl daemon-reload
sudo systemctl enable --now restic-backup.timer
sudo systemctl list-timers --all | grep resticVerifying Integrity and Testing Restores
A backup is only as reliable as your latest verified restore. Schedule weekly integrity audits using restic check to detect underlying disk bit rot:
# Test manual run and view journald output
sudo systemctl start restic-backup.service
journalctl -u restic-backup.service --no-pager -n 50
# List stored snapshots
set -a; . /etc/restic/backup.env; set +a
restic snapshots
# Verify repository integrity
restic check --read-data-subset=10%
# Perform a test restore of a specific configuration file
restic restore latest --target /tmp/test-restore --include /etc/nginx
ls -la /tmp/test-restore/etc/nginxRestic transforms unpredictable backups into an encrypted, deterministic maintenance pipeline. Running automated prune jobs ensures your remote storage footprint remains bounded while preserving complete point-in-time recovery points across months.
Deploy your backup targets and Linux servers on Netbay in under 60 seconds with High-Speed SSD and DDoS protection in Lucknow DC01 — netbayhosts.in.
Keep reading
Follow along on a real VPS
Deploy Linux in under 60 seconds
These guides are written against Ubuntu, Debian, and RHEL-family images — the same ones on NetBay.
Deploy an instance