Automatic backups of your Linux VPS with restic

Make an encrypted backup of your Debian or Ubuntu VPS to another location every night, with a database dump, retention rules and a restore test.

EliteStarHosting Team8 November 2026Updated 26 November 20264 min read
Ready to start? View the VPS Hosting plans

A backup on the same VPS doesn't help when that VPS breaks, gets hacked or has to be reinstalled: you lose the copy too. So keep your backups somewhere else, such as a second server, a NAS at home or storage with another provider. This guide uses restic. It encrypts everything on your VPS before it leaves and after the first run only stores changes.

Don't have a VPS yet? You can rent a VPS in the Netherlands from us from €3 a month.

What do you back up?

  • /etc: your server's settings.
  • /home and /root: files and scripts.
  • Your own folders, such as /var/www for websites or /srv and /opt for apps and game servers.
  • A dump of your databases.

Programs can stay out: you reinstall them with apt.

Preparing

Work as root (sudo -i). On the backup server you need a user that may log in over SSH, here backup on 192.0.2.50.

apt install restic
ssh-keygen -t ed25519
ssh-copy-id [email protected]

At ssh-keygen, press Enter three times, without a passphrase: an automatic backup can't type one. If ssh-copy-id doesn't work, add the public key by hand as in Secure your new Linux VPS.

Create a password and a file for your settings:

head -c 32 /dev/urandom | base64 > /root/.restic-password
nano /root/restic.env

restic.env gets these lines:

export RESTIC_REPOSITORY=sftp:[email protected]:restic-vps
export RESTIC_PASSWORD_FILE=/root/.restic-password

Then create the repository, the backup folder on the other server:

chmod 600 /root/.restic-password /root/restic.env
. /root/restic.env
restic init

Keep the password outside your VPS too. Show it with cat /root/.restic-password, put it in your password manager and share it with no one. Without this password nobody can restore your backup, not even you.

Using S3 storage? Then the repository becomes s3:https://server/bucket and you put your keys as AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in the same file.

The script

Create /usr/local/sbin/vps-backup with nano:

#!/bin/sh
set -eu
. /root/restic.env
umask 077
mkdir -p /var/backups/db
mariadb-dump --all-databases --single-transaction > /var/backups/db/all.sql
restic backup --exclude-caches /etc /home /root /var/backups/db
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

Make it executable with chmod 700 /usr/local/sbin/vps-backup.

  • The dump comes first. If you copy the files of a running database, that copy can be caught in the middle of a write and be unusable. --single-transaction gives a consistent snapshot of InnoDB tables without stopping your site. With the standard Debian and Ubuntu packages, root needs no database password for this. With MySQL you write mysqldump, without a database you remove the line.
  • Your own folders go after restic backup. Only folders that exist: if one is missing, restic calls the backup incomplete and the script stops.
  • Retention: forget keeps 7 daily, 4 weekly and 6 monthly backups and --prune cleans up the rest.

Automatically every night

Create /etc/systemd/system/vps-backup.service:

[Service]
Type=oneshot
User=root
ExecStart=/usr/local/sbin/vps-backup

And /etc/systemd/system/vps-backup.timer:

[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=true

[Install]
WantedBy=timers.target

Turn on the timer and run a first backup now:

systemctl daemon-reload
systemctl enable --now vps-backup.timer
systemctl start vps-backup.service
journalctl -u vps-backup.service

The backup now runs every night at 3:30, in your VPS's time zone. If your VPS was off at 3:30, Persistent=true catches up. If a backup is still running, systemd doesn't start a second one.

Prefer cron? Add the line 30 3 * * * /usr/local/sbin/vps-backup to root's crontab with crontab -e.

Test a restore

A backup you have never restored is a gamble. Test after the first run and then monthly:

. /root/restic.env
restic snapshots
restic restore latest --target /var/tmp/restore-test --include /var/backups/db
ls /var/tmp/restore-test/var/backups/db
restic check
rm -r /var/tmp/restore-test

snapshots shows your backups, restore latest puts the dump from the newest backup in a test folder and check verifies that the repository is intact. Without --include you restore everything: make sure you have enough free disk space.

If your VPS is reinstalled through support, create a new SSH key, put restic.env and the password back and fetch your files with restic restore.