Your data on a VPS is your responsibility: keep a copy somewhere else and make sure you can restore it. Restic makes encrypted, deduplicated backups to another server, an S3-compatible storage or a local disk.
Run everything on this page as root: sudo -i.
apt update
apt install -y restic
umask 077
openssl rand -base64 32 > /root/.restic-password
export RESTIC_REPOSITORY=sftp:[email protected]:/srv/restic/myvps
export RESTIC_PASSWORD_FILE=/root/.restic-password
restic init
The sftp address needs SSH key login from root on this server to the backup server, without a passphrase prompt. Store a second copy of the password from /root/.restic-password somewhere else: without the password the backups cannot be read. The repository can also be an S3-compatible bucket; see the restic documentation for the exact address format.
restic backup /etc /home /var/www /opt
List only folders that exist on your server. For a database, dump it first so you copy a consistent file, for example mysqldump --single-transaction --all-databases > /var/backups/db.sql or sudo -u postgres pg_dumpall > /var/backups/db.sql, then chmod 600 /var/backups/db.sql, and include /var/backups in the backup.
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
Put the backup and forget commands in a small script and run it daily from cron or a systemd timer, with the two environment variables set in the script.
restic snapshots
restic restore latest --target /tmp/restore-test
A backup you have never restored is only a hope. Do a test restore after you set it up and from time to time afterwards.
Why do I need my own backups on a VPS?
Your data on a server is your responsibility; a copy somewhere else protects you from mistakes, failures and deleted files.
What happens if I lose the restic password?
The backups cannot be read, so keep a second copy of the password in a safe place.
How do I back up a database?
Dump it first with mysqldump or pg_dumpall and include the dump file in the backup.
How do I know the backup works?
Test a restore into a temporary folder after setting it up and regularly afterwards.