Skip to content
VDS · Databases

Database backups

How to schedule MySQL and PostgreSQL dumps, move them off the machine, and verify that the backup actually restores.

Updated August 23, 2026

A VPS doesn't come with automatic machine backups or state snapshots: anything you haven't copied yourself exists in a single instance. A database dump is the first thing you should set up, because there's nowhere to restore the data from, while the site can be redeployed from scratch.

Manual dump

PostgreSQL:

pg_dump -Fc -U shop -h 127.0.0.1 shop > /srv/backups/shop-$(date +%F).dump
pg_dumpall -U postgres --roles-only > /srv/backups/roles.sql

MySQL and MariaDB:

mysqldump --single-transaction --quick --routines --events \
  -u root shop | gzip > /srv/backups/shop-$(date +%F).sql.gz

--single-transaction takes a consistent InnoDB dump without locking tables — without it, your site will stall during the export. PostgreSQL's -Fc format is compressed and lets you restore individual tables.

Don't put passwords on the command line: create ~/.pgpass or ~/.my.cnf with 600 permissions.

Scheduled

mkdir -p /srv/backups && chmod 700 /srv/backups
crontab -e
20 3 * * * /usr/local/bin/db-backup.sh >> /var/log/db-backup.log 2>&1

The /usr/local/bin/db-backup.sh script:

#!/bin/bash
set -euo pipefail
DEST=/srv/backups
STAMP=$(date +%F-%H%M)
pg_dump -Fc -U shop -h 127.0.0.1 shop > "$DEST/shop-$STAMP.dump.tmp"
mv "$DEST/shop-$STAMP.dump.tmp" "$DEST/shop-$STAMP.dump"
find "$DEST" -name '*.dump' -mtime +14 -delete

Writing to a temporary file and then renaming it matters: if the dump is interrupted, you won't mistake a truncated file for a complete backup. set -euo pipefail stops the script on the first error instead of silently continuing.

Move backups off the machine

A copy on the same disk won't save you from a migration mistake, ransomware, or reinstalling the OS on the “Access” tab. Send dumps offsite:

rsync -az --delete /srv/backups/ backup@other-host:/store/vds/

Or to any S3-compatible storage via rclone. Keep in mind the bandwidth of up to 500 Mbit/s and the per-tier disk write limit: 60 MB/s per core.

Restore check

A backup you've never restored isn't a backup.

createdb -U postgres shop_check
pg_restore -U postgres -d shop_check /srv/backups/shop-2026-08-23.dump
psql -U postgres -d shop_check -c "SELECT count(*) FROM users;"
dropdb -U postgres shop_check

For MySQL: gunzip -c dump.sql.gz | mysql -u root shop_check.

Do this once a month and record the check date.

Common mistakes

SymptomCause
The dump is only a few kilobytesThe script failed at authentication, and the error went to /dev/null
Disk space ran outNo deletion of old backups by mtime
Restore fails on permissionsRoles weren't dumped — you need --roles-only
Cron is silent, no backupsCron doesn't have your PATH — use absolute paths
The site slows down at nightThe dump competes for disk — shift the time, add --quick

Keep at least two copies in different locations and monitor the dump size: a sharp drop in size means not the entire database was dumped.