restic makes encrypted backups and keeps older versions, so you can go back to yesterday's or last month's state of a file. After the first backup it uploads only what changed, so a daily backup is fast and small. This page sets up a daily backup of your dedicated server to your App Hosting service over SSH; any other server that you can reach over SSH works the same way. Below, we call the server that holds the backups the backup server. The commands were tested on Ubuntu 26.04 LTS with restic 0.18; run them as root.
You need the address, user name and password of the service that will hold the backups: the same details as for SFTP (Download files from server via FTP – FileZilla).
1. Install restic
apt install restic restic version
2. A Key for the Backups
The automatic backup cannot type a password, so it logs in to the backup server with an SSH key. Create a key without a passphrase and copy it to the backup server; ssh-copy-id asks for your password on the backup server once. Use your own server name and user name:
ssh-keygen -t ed25519 -f /root/.ssh/backup_key -N "" -C backup ssh-copy-id -i /root/.ssh/backup_key.pub username@ds12345.seedhost.eu
Then give the connection a short name. Create the file /root/.ssh/config (for example with nano /root/.ssh/config) with these lines:
Host backupserver
HostName ds12345.seedhost.eu
User username
IdentityFile /root/.ssh/backup_key
Test it: ssh backupserver pwd must print your home folder on the backup server without asking for a password.
Anyone who becomes root on your server can use this key to log in to the backup server. Keep your server secure (SSH keys, firewall), and remove the key from ~/.ssh/authorized_keys on the backup server if you stop using it.
3. The Password of the Backups
restic encrypts everything with a password. Let it be a long random one, stored in a file that only root can read:
umask 077 openssl rand -base64 32 > /root/.restic-password cat /root/.restic-password
Without this password nobody can read the backups, not even our support. If the server is lost or reinstalled, the file on it is gone too. Copy the line that cat printed into your password manager now.
4. Create the Backup Store
The backups go into the folder backups/server01 in your home folder on the backup server. These two lines tell restic where the store is and where the password is; type them again in every new SSH session before you use restic by hand:
export RESTIC_REPOSITORY=sftp:backupserver:backups/server01 export RESTIC_PASSWORD_FILE=/root/.restic-password restic init
restic answers created restic repository … at sftp:backupserver:backups/server01.
5. The First Backup
Choose the folders to back up. A good start is the settings in /etc, the home folders and the home folder of root; add the folders of your own data, for example /srv or /var/www:
restic backup /etc /root /home --exclude /root/.cache
The first backup ends like this:
Files: 991 new, 0 changed, 0 unmodified Added to the repository: 12.041 MiB (5.993 MiB stored) snapshot 7c0364ad saved
Each backup is a snapshot. A second backup a few seconds later, with one new file, adds only 70.699 KiB: restic stores what is already there only once.
Do not back up the files of a running database such as MySQL or MariaDB directly: the copy may be broken. Save a dump of the database into a backed-up folder first, for example with mysqldump before the backup.
6. Back Up Automatically Every Day
A systemd timer starts the backup every night. Create the file /etc/systemd/system/restic-backup.service:
[Unit] Description=Backup to the backup server with restic Wants=network-online.target After=network-online.target [Service] Type=oneshot Environment=RESTIC_REPOSITORY=sftp:backupserver:backups/server01 Environment=RESTIC_PASSWORD_FILE=/root/.restic-password Environment=RESTIC_CACHE_DIR=/var/cache/restic CacheDirectory=restic ExecStart=/usr/bin/restic backup /etc /root /home --exclude /root/.cache ExecStart=/usr/bin/restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
The first ExecStart line makes the backup; use the same folders as in step 5. The second removes old snapshots: it keeps one per day for the last 7 days, one per week for 4 weeks and one per month for 6 months. Then create /etc/systemd/system/restic-backup.timer:
[Unit] Description=Daily backup to the backup server [Timer] OnCalendar=*-*-* 03:00 RandomizedDelaySec=30min Persistent=true [Install] WantedBy=timers.target
The backup starts between 03:00 and 03:30 in the time zone of the server (timedatectl shows it). If the server was off at that time, Persistent=true starts the backup soon after it comes back. Switch the timer on, and start one backup now to check that everything works:
systemctl daemon-reload systemctl enable --now restic-backup.timer systemctl start restic-backup.service journalctl -u restic-backup.service -n 30
The log must show snapshot … saved and the list of kept snapshots. systemctl list-timers restic-backup.timer shows when the next backup runs.
7. Find and Restore Files
First type the two export lines from step 4. Then:
restic snapshotslists the snapshots with their date and time;restic find sshd_configshows which snapshots contain a file with this name;restic ls latest /etc/sshlists the files of a folder in the newest snapshot.
Restore into a separate folder, look at the files, and copy back only what you need:
restic restore latest --target /root/restore-test --include /etc/hostname ls -l /root/restore-test/etc/
Instead of latest, use the ID of a snapshot from restic snapshots to get an older version.
8. Check the Backups From Time to Time
restic check
It reads the store on the backup server and ends with no errors were found. To see how much space the backups take on the backup server: ssh backupserver du -sh backups. The backups count toward the disk space of the App Hosting service.
After a Reinstall or on a New Server
Install restic, put the saved password back into /root/.restic-password, and set up the key and /root/.ssh/config again (step 2). Then the commands of step 7 show and restore your snapshots. On a new server, check the restore before you delete anything else.
A one-off copy before a reinstall is described in Back Up Your Linux Server Before a Reinstall.