VPS Backup with restic: Automatic, Encrypted Off-Server Backups
Back up your Linux VPS files and databases off the server with restic, encrypted and automatic: creating a repository, an SFTP or S3 target, database dumps, a retention policy, a systemd timer and a restore test.
There are many ways to lose data on a server: a mistyped rm -rf, a broken update, a compromised account or a disk that quietly fills up. The common cure for all of them is a backup that lives off the server and has been proven to restore. In this guide we set up encrypted, incremental and automatic backups on an Ubuntu 24.04 VPS with restic.
Summary: install restic, choose where backups will live (SFTP on another server, or S3-compatible storage) and create an encrypted repository with
restic init, dump your database and back it up together with your files usingrestic backup, clean up old backups withrestic forget --prune, and tie everything to a systemd timer. Try arestic restoreat least once.
Why restic?
- Encrypted: backups are encrypted with your password before they are written to the repository. Whoever hosts the repository cannot read the contents.
- Incremental with deduplication: after the first backup only changed chunks of data are sent. You can go back to any day as if you had a full backup, while using far less disk and traffic.
- Many targets: it works with a local disk, another server (SFTP), S3-compatible object storage and other cloud storage types.
- A single program: it is available in the Ubuntu repositories; no separate server software is required.
Where should backups go?
A backup kept on the same server is useless once that server is deleted or compromised. The well-known 3-2-1 rule: keep 3 copies of your data, on 2 different media, with 1 copy somewhere else. In practice two options stand out:
| Target | When it fits | Example restic repository |
|---|---|---|
| Another server (SFTP) | You have a second server or a home/office NAS reachable over SSH | sftp:backup@BACKUP_SERVER:/srv/restic/vps1 |
| S3-compatible object storage | You do not want to manage another server | s3:https://S3_ENDPOINT/bucket-name/vps1 |
restic opens the connection outbound from the server being backed up. So the VPS does not need a public IP address of its own; it only has to be able to reach the backup target.
1. Install restic
Connect to your server over SSH and install restic:
sudo apt update
sudo apt install -y restic
restic version
We will run the following commands as root (sudo -i), because reading system folders such as /etc requires it.
2. Create the repository password
The repository password is the key to your backups. If you lose the password, there is no way to open the backups. Generate a strong password and store it in a file only root can read:
sudo -i
openssl rand -base64 32 > /root/.restic-password
chmod 600 /root/.restic-password
cat /root/.restic-password
Also save the password shown on screen in a password manager or another safe place off the server. If the server is lost completely, that copy is the only way back into your backups.
3. Prepare the target and create the repository
Option A: SFTP to another server
On the backup server (the target), create a dedicated user and folder for backups:
sudo adduser --disabled-password --gecos "" backup
sudo mkdir -p /srv/restic && sudo chown backup:backup /srv/restic
On the VPS being backed up, generate an SSH key for root and add the public key to ~/.ssh/authorized_keys of the backup user on the target:
ssh-keygen -t ed25519 -f /root/.ssh/restic_ed25519 -N ""
cat /root/.ssh/restic_ed25519.pub
Add an entry to /root/.ssh/config so restic uses this key:
Host backup-server
HostName BACKUP_SERVER_IP
User backup
IdentityFile /root/.ssh/restic_ed25519
Connect once with ssh backup-server to accept the host key, then log out. Now create the repository:
export RESTIC_REPOSITORY="sftp:backup-server:/srv/restic/vps1"
export RESTIC_PASSWORD_FILE=/root/.restic-password
restic init
Option B: S3-compatible storage
In your storage provider's panel, create a bucket and an access key that is allowed to use only that bucket. Then:
export AWS_ACCESS_KEY_ID="ACCESS_KEY"
export AWS_SECRET_ACCESS_KEY="SECRET_KEY"
export RESTIC_REPOSITORY="s3:https://S3_ENDPOINT/bucket-name/vps1"
export RESTIC_PASSWORD_FILE=/root/.restic-password
restic init
When you see created restic repository, the repository is ready.
4. Dump the database
Copying the files of a running database directly can produce an inconsistent backup. The right way is to dump the database first and back up the dump file:
mkdir -p /var/backups/db
# MySQL / MariaDB
mysqldump --single-transaction --all-databases > /var/backups/db/mysql.sql
# PostgreSQL
sudo -u postgres pg_dumpall > /var/backups/db/postgres.sql
Run the line for the database you use. In newer MariaDB releases the same tool is also available as mariadb-dump. On Ubuntu the MariaDB/MySQL root user authenticates through the socket, so no extra password is needed when you run it as root.
5. Take the first backup
Decide which folders to back up. For most servers this list is a good start: /etc (configuration), /home and /root (user files), /var/www (websites), /var/backups/db (database dumps) and the data folders of your applications (for example /opt).
restic backup /etc /home /root /var/www /var/backups/db /opt \
--exclude-caches \
--exclude "/home/*/.cache" \
--exclude "/root/.cache" \
--exclude "*.log"
restic warns about folders that do not exist; adjust the list to your server. To list backups:
restic snapshots
The first backup sends everything, so it can take a while; later backups only send changes and are much faster.
6. Retention policy: clean up old backups
If daily backups are kept forever, the repository keeps growing. forget decides which backups stay, and --prune deletes data that is no longer referenced:
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
With this setting you keep daily backups for the last 7 days, weekly backups for the last 4 weeks and monthly backups for the last 6 months.
7. Automate it: a script and a systemd timer
Put all the steps into one script:
nano /usr/local/bin/restic-backup.sh
#!/bin/bash
set -euo pipefail
export RESTIC_REPOSITORY="sftp:backup-server:/srv/restic/vps1"
export RESTIC_PASSWORD_FILE=/root/.restic-password
# Using S3? Also add the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY lines
mkdir -p /var/backups/db
# Uncomment the one for your database:
# mysqldump --single-transaction --all-databases > /var/backups/db/mysql.sql
# sudo -u postgres pg_dumpall > /var/backups/db/postgres.sql
restic backup /etc /home /root /var/www /var/backups/db \
--exclude-caches --exclude "/home/*/.cache" --exclude "/root/.cache" --exclude "*.log"
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic check
chmod 700 /usr/local/bin/restic-backup.sh
The service file:
nano /etc/systemd/system/restic-backup.service
[Unit]
Description=restic backup
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/restic-backup.sh
Nice=10
IOSchedulingClass=idle
The timer (every night at 03:30, or at the next boot if the server was off at that time):
nano /etc/systemd/system/restic-backup.timer
[Unit]
Description=Nightly restic backup
[Timer]
OnCalendar=*-*-* 03:30:00
RandomizedDelaySec=15min
Persistent=true
[Install]
WantedBy=timers.target
systemctl daemon-reload
systemctl enable --now restic-backup.timer
systemctl start restic-backup.service
journalctl -u restic-backup.service -n 50
systemctl list-timers restic-backup.timer
Run it once by hand with systemctl start and check the log for errors. For more on scheduled jobs, see the cron section of our server management commands article.
8. Always test a restore
A backup that cannot be restored is not a backup. Test by restoring a single folder to a temporary location:
restic snapshots
restic restore latest --target /tmp/restore-test --include /etc/nginx
ls /tmp/restore-test/etc/nginx
Use restic find nginx.conf to locate a single file and restic ls latest to list the contents of a backup. If the whole server is lost: install restic on the new server, run restic restore latest --target / with the same repository address and password, then import the database dump (mysql < mysql.sql or sudo -u postgres psql -f postgres.sql). Rehearsing this once a month on a test server is a good habit.
Notes for small VPSs
- RAM: restic can use a few hundred MB of memory on large repositories. If the job dies on a server with 512 MB–1 GB of RAM, add swap.
- Disk: database dumps take local space; keep only the latest dump (the script overwrites it each time).
- Security: use the backup user on the target only for backups. To stop an attacker who takes over the server from deleting your backups, consider an extra copy on the target or storage protected against deletion (append-only or with object lock). For other basic measures see our Linux server security guide.
Which TürkiyeVPS plan?
You set up backups yourself on each server, and this guide works the same on our VPS plans and VDS plans. All plans are in Istanbul, with root access, unmetered speed and 3 TB of traffic per month, and are set up automatically with Ubuntu 24.04 or Debian 12. Backups and database dumps use CPU and RAM for short periods: a VPS is enough for a small site or a bot, while a VDS with CPU cores reserved for you is more predictable for databases and applications under constant load. If you are not sure which fits you, read VPS vs VDS.
Frequently Asked Questions
Can I open my backups if I forget the restic password?
No. restic encrypts backups with the password and there is no way to reset it. Always keep the password off the server, for example in a password manager.
What is the difference between rsync and restic?
rsync synchronises files from one place to another; it does not keep older versions and does not encrypt on its own. restic stores every backup as a separate snapshot, encrypts the data and stores repeated chunks only once, so you can go back to, say, a file from three days ago.
Will my website or game server slow down during backups?
Apart from the first run, backups only send changes and are usually short. The service in this guide runs at low priority (Nice=10, IOSchedulingClass=idle) and is scheduled for the quietest hour.
How do I know a backup is intact?
restic check verifies the repository structure; restic check --read-data-subset=5% also reads and verifies part of the data. The most reliable method is still to test restores regularly.
Can't I just back up the database files directly?
The files of a running database can change while they are being copied, which makes the backup inconsistent. A dump taken with mysqldump --single-transaction or pg_dumpall is consistent and easy to move to another server.
You might also like
VPS prices range from under 1 dollar to dozens of dollars a month. We explain what drives the price (CPU, RAM,...
Want your own VPN server instead of a VPN app subscription? What to look at when choosing a VPS for VPN: IP ad...
What the target site (Target / dest) and SNI do in a Reality connection, what makes a good target, how to test...