Back up a Linux VPS
Updated 27 Nov 2026 backup rsync database
Copy files and database dumps somewhere else, and test them.
A backup on the same server is lost with the server. Keep copies elsewhere.
Database dumps
PostgreSQL:
sudo -u postgres pg_dump -Fc mydb > mydb.dump
MySQL or MariaDB:
sudo mysqldump --single-transaction mydb | gzip > mydb.sql.gz
Copy files to your computer
rsync -avz root@203.0.113.10:/var/www/ ./backup/www/
Automate on the server
/usr/local/bin/nightly-backup:
#!/usr/bin/env bash
set -euo pipefail
dir=/var/backups/app
mkdir -p "$dir"
sudo -u postgres pg_dump -Fc mydb > "$dir/mydb-$(date +%F).dump"
find "$dir" -name '*.dump' -mtime +14 -delete
Make it executable, schedule it with sudo crontab -e, then copy that folder off the server on a schedule too.
Test a restore
sudo -u postgres createdb restore_test
sudo -u postgres pg_restore -d restore_test < mydb.dump
Check the data, then drop the test database. A backup you have never restored is unproven.