Most efficient and secure backup of your self-hosted databases, Postgres or SQL Server
Most small teams back up their servers the same way. Veeam, Proxmox Backup Server or something similar takes a snapshot of each VM once a day or once a week, and that's the whole plan. It's a good plan for rebuilding a machine. For databases it isn't good enough: if the disk dies on Friday and the last snapshot ran on Sunday, you've lost five days of orders, sign-ups and edits.
You don't need a second server or replication to fix this. You need three things: frequent database-level backups, an encrypted off-site copy, and an alert when either one stops. With the tools below, all three cost less than a coffee a month.
Replication is not a backup
The first idea is usually to rent a small VPS and replicate the databases to it. Replication protects you against a dead server. It doesn't protect you against a bad DELETE, a broken migration or ransomware, because the replica copies the mistake within milliseconds.
What you want is point-in-time recovery: the ability to say "restore to 14:32, just before it went wrong." That comes from a regular full backup plus a continuous stream of transaction-log backups, stored somewhere the server can't erase.
The setup
One Linux host runs both SQL Server and PostgreSQL:
SQL Server writes a full backup every night and a log backup every 5 minutes.
PostgreSQL dumps every database once an hour.
rclone encrypts the files and copies them to Cloudflare R2 every 5 minutes.
R2 won't let anyone delete or overwrite a file for 14 days, and removes it after 30.
A watchdog posts to Discord when any backup is more than an hour behind.
Your existing VM snapshots stay in place. They're still the fastest way to rebuild a whole machine.
1. SQL Server: full recovery and 5-minute log backups
A database in SIMPLE recovery can't have log backups, so you can only restore to the last full backup. Switch the databases you care about to FULL:
ALTER DATABASE [AppDb] SET RECOVERY FULL;Then install Ola Hallengren's Maintenance Solution. It's free, widely used and creates SQL Agent jobs for you. Give the jobs schedules and add compression:
EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = N'/var/opt/mssql/backups/ola',
@BackupType = 'LOG', -- 'FULL' for the nightly job
@Compress = 'Y',
@Checksum = 'Y',
@Verify = 'Y',
@CleanupTime = 72; -- keep 72 hours locallyCompression makes a real difference: about 2 GB of full backups shrank to under 500 MB. Run the first full backup by hand straight after switching to FULL, because log backups need a full backup to start from.
Development databases can stay in SIMPLE recovery with only the nightly full.
2. PostgreSQL: hourly dumps, minus what you can rebuild
For small databases, an hourly pg_dump is simple and good enough:
pg_dumpall --globals-only | gzip > "$OUT/globals.sql.gz"
for db in $(psql -Atc "select datname from pg_database
where datname not in ('bigdb','template0','template1','postgres')"); do
pg_dump -Fc -Z 6 -f "$OUT/$db.dump" "$db"
doneSkip anything you can rebuild. If one database is 90 GB of data you can download again from its sources, leave it out and let the weekly VM snapshot cover it. It shouldn't drive the cost of everything else.
If you need minute-level recovery for Postgres as well, look at pgBackRest or WAL-G. Both archive the write-ahead log continuously and work with R2.
3. Off-site: encrypted, immutable, almost free
Cloudflare R2 speaks the S3 API, includes 10 GB of storage free each month and charges nothing for downloads. That last part matters on the day you need to restore.
Set up the bucket in this order:
Bucket lock, 14 days. Nobody can delete or overwrite a file for 14 days, including someone holding your API token.
Lifecycle rule: delete files after 30 days.
API token: object read/write on this one bucket only, restricted to your server's IP address.
On the server, rclone's crypt remote encrypts file contents and names before upload. The provider only stores unreadable data:
[r2]
type = s3
provider = Cloudflare
endpoint = https://<account-id>.r2.cloudflarestorage.com
region = auto
no_check_bucket = true
[r2crypt]
type = crypt
remote = r2:db-backups
password = <obscured>
password2 = <obscured>Store the two crypt passwords in a password manager, not only on the server. If the server is gone and the passwords went with it, the backups can't be decrypted.
Upload with copy, never sync, so a deleted local file never deletes the off-site copy. To keep R2 request costs at zero, the 5-minute run looks only at recent files, and one nightly run checks everything:
# every 5 min: only files from the last 2 hours
rclone copy /var/opt/mssql/backups/ola r2crypt:mssql --min-age 2m --max-age 2h --no-traverse
# nightly: full comparison
rclone copy /var/opt/mssql/backups/ola r2crypt:mssql --min-age 2m4. Alerts that don't spam you
A backup that fails without telling anyone is worse than no backup, because you believe you have one. But an alert on every 5-minute run would get muted within a week.
So the watchdog doesn't watch jobs. It watches results: every 10 minutes it checks the age of the newest file of each kind.
Check | Alert when the newest is older than |
|---|---|
SQL log backup | 60 min |
Upload to R2 | 60 min |
Postgres dump | 75 min |
SQL full backup | 26 h |
Disk usage | 92% full |
It posts to a Discord/Telegram webhook. A Slack or Teams webhook works the same way. It repeats at most once an hour per problem and sends one message when things recover. The core of it:
if (( age >= limit )); then
last=$(cat "$STATE/alert_$key" 2>/dev/null || echo 0)
if (( NOW - last >= 3600 )); then
problems+=("$desc: last success ${age} min ago")
echo "$NOW" > "$STATE/alert_$key"
fi
elif [[ -f $STATE/alert_$key ]]; then
recovered+=("$desc: OK again")
rm -f "$STATE/alert_$key"
fiTest it before you trust it. Backdate a timestamp, run the watchdog twice and put the timestamp back. You should get one alert, then silence, then one recovery message.
5. Restore once, on purpose
A backup you haven't restored is a hope, not a backup. Pull the files back from R2 and restore into throwaway databases:
RESTORE DATABASE [AppDb_test] FROM DISK = N'.../FULL.bak' WITH NORECOVERY, MOVE ...;
RESTORE LOG [AppDb_test] FROM DISK = N'.../LOG_0600.trn' WITH NORECOVERY;
RESTORE LOG [AppDb_test] FROM DISK = N'.../LOG_0605.trn' WITH NORECOVERY;
RESTORE DATABASE [AppDb_test] WITH RECOVERY;createdb app_test && pg_restore -d app_test --no-owner app.dumpCompare table counts against the live databases, then drop the test copies. Write the steps down on the server, because you won't remember them during an outage.
What it costs
Item | Cost |
|---|---|
Ola Hallengren scripts, rclone, pg_dump | free |
R2 storage (about 0.5 GB a day, kept 30 days) | 10 GB free, then $0.015/GB, so about $0.10 a month |
R2 requests (about 100k a month) | free tier is 1M |
R2 downloads | free |
Discord webhook | free |
The worst-case loss went from a week to about 5 minutes for SQL Server and an hour for Postgres. Setup took one morning.
What this doesn't cover
The whole host going down. The watchdog runs on the machine it checks, so it can't report its own death. Add an outside dead-man's switch, such as a free healthchecks.io ping that alerts when it stops hearing from the server.
Rebuilding the server itself. Keep your VM snapshots for that. This setup protects the data, not the operating system.
Instant failover. Restoring from R2 takes minutes to an hour. If you need to be back online in seconds, then add a replica, on top of these backups rather than instead of them.
Most of the protection comes from three steps: back up the transaction log, keep a copy where the server can't delete it, and get an alert when something stops. All three are free or close to it.