Back up your Linux VPS with rsync, restic and cron

Decide what to back up, dump your databases, copy files off the server with rsync or encrypted restic snapshots, schedule it with cron and prove restores work.

  • Intermediate
  • Linux VPS
  • 3 min read

By RunRacks Team · Published

A backup only counts if it lives somewhere other than the server and you have restored from it at least once. This guide builds a simple, dependable routine.

1. Decide what to back up

  • Website and app files, for example /var/www, /srv and /home.
  • Configuration in /etc (Nginx, SSH, cron files).
  • Database dumps, not the live database files.
  • Anything else you could not quickly recreate: TLS keys, .env files, uploads.

2. Dump databases first

Copying /var/lib/mysql while the database runs gives an inconsistent copy. Dump to a file instead:

Terminal
sudo mkdir -p /var/backups/db
sudo mariadb-dump --single-transaction --all-databases | gzip | sudo tee /var/backups/db/mariadb.sql.gz > /dev/null
sudo -u postgres pg_dumpall | gzip | sudo tee /var/backups/db/postgres.sql.gz > /dev/null

Keep only the line for the database you use.

3. Option A: pull a copy with rsync

Run this on your own computer or a backup server, so the VPS never holds credentials to your backups:

Terminal
rsync -az --delete [email protected]:/var/www/ ~/backups/myvps/www/

4. Option B: encrypted, versioned backups with restic

restic stores deduplicated, encrypted snapshots on SFTP, S3-compatible storage and more. Install it:

Terminal
sudo apt install -y restic                                   # Ubuntu / Debian
sudo dnf install -y epel-release && sudo dnf install -y restic   # AlmaLinux / Rocky

Create a password file and initialise a repository (here over SFTP to another server you control):

Terminal
sudo sh -c 'openssl rand -base64 32 > /root/.restic-pass && chmod 600 /root/.restic-pass'
sudo restic -r sftp:[email protected]:/srv/restic/myvps --password-file /root/.restic-pass init

5. Automate it

Create /usr/local/bin/backup.sh:

Terminal
#!/usr/bin/env bash
set -euo pipefail
export RESTIC_REPOSITORY="sftp:[email protected]:/srv/restic/myvps"
export RESTIC_PASSWORD_FILE="/root/.restic-pass"

mariadb-dump --single-transaction --all-databases | gzip > /var/backups/db/mariadb.sql.gz
restic backup /etc /var/www /home /var/backups/db
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

Make it executable and schedule it nightly in root's crontab (sudo crontab -e):

Terminal
sudo chmod 700 /usr/local/bin/backup.sh
Text
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

6. Test a restore

Terminal
sudo restic -r sftp:[email protected]:/srv/restic/myvps --password-file /root/.restic-pass snapshots
sudo restic -r sftp:[email protected]:/srv/restic/myvps --password-file /root/.restic-pass restore latest --target /tmp/restore-test

Check a few files in /tmp/restore-test, then delete it. Repeat this every few months.

Troubleshooting

  • SFTP repository asks for a password: restic needs key-based SSH from root to the backup host. Add root's public key there.
  • Cron job does nothing: read /var/log/backup.log. Cron's PATH is minimal, so use full paths if a command is not found.
  • Backups use too much space: check the forget rules and run restic stats.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Linux VPS

Deploy a Node.js app with PM2 behind Nginx

Install a Node.js LTS release, keep your app running with PM2 across crashes and reboots, and serve it through Nginx with HTTPS.

  • Intermediate
  • Linux VPS
  • 3 min read

Linux VPS

Create an SFTP-only user limited to one folder

Give someone file access over SFTP without a shell, locked inside one folder. Configure internal-sftp with a Match block, set the chroot ownership right and test with WinSCP.

  • Advanced
  • Linux VPS
  • 5 min read