A VPS without a tested backup is a server waiting for a bad day. Disks fail, updates break things, and a wrong command can wipe a directory in a second. Restic fixes the backup side of that problem with one tool: it encrypts everything before it leaves your server, deduplicates data so repeat backups stay small, and stores snapshots in S3-compatible storage you can reach from anywhere. This restic encrypted backups s3 tutorial walks through the full setup on Ubuntu 24.04, from install to a scheduled nightly backup, with a restore test included because a backup you never restored is only a guess.
What Restic Encrypted Backups S3 Give You
Restic is a single static binary with no server component to run. You point it at a repository, give it a password, and it encrypts every file with AES-256 before upload. The storage provider never sees your file names or contents in plain text, only encrypted blobs. That matters when your backups hold database dumps, WordPress uploads, or client files. If you already follow the 3-2-1 backup rule, restic covers the offsite copy: one copy on your server, one encrypted copy in S3-compatible storage, and snapshots that let you roll back to any point in time.
Restic also deduplicates at the chunk level, so repeat backups upload only changed data and stay small.
Install Restic on Ubuntu or Debian
Install restic from your distribution packages or grab the latest release from the project site. The package version is fine for most servers, but the upstream release is newer if you need a recent fix.
sudo apt update
sudo apt install restic -y
restic version
The release archive works too: drop the binary in /usr/local/bin and make it executable.
Set Up Your S3-Compatible Bucket and Credentials
Create a bucket with your storage provider and generate an access key and secret key with write access to that bucket only. Never reuse your provider root credentials for backups. A scoped key limits the damage if the key leaks. Providers that speak the S3 API include Backblaze B2, Wasabi, Hetzner Storage Box with S3 access, and MinIO on your own hardware. If you have not picked one yet, this guide comparing S3-compatible storage providers lays out the trade-offs between them.
Store the credentials in a file only root can read. Restic reads S3 settings from environment variables, so a small env file keeps secrets out of your shell history and out of cron logs.
sudo mkdir -p /root/restic
sudo nano /root/restic/env.sh
Add these lines, replacing the values with your own:
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export RESTIC_REPOSITORY="s3:https://s3.eu-central-1.wasabisys.com/my-vps-backups"
export RESTIC_PASSWORD="a-long-random-password-you-store-in-a-password-manager"
Lock the file down and load it in your shell:
sudo chmod 600 /root/restic/env.sh
source /root/restic/env.sh
The repository URL uses your provider endpoint. Swap the host for your provider, for example s3.eu-central-003.backblazeb2.com for Backblaze B2 or your MinIO server address. The bucket name at the end must already exist.
Initialize the Encrypted Repository
With the env file loaded, initialize the repository. Restic asks for the password from RESTIC_PASSWORD and creates the encrypted structure in the bucket.
restic init
You should see a message that the repository was created. If you see an error about the bucket or credentials, check the endpoint URL first, then the key pair. A wrong region in the endpoint is the most common cause of init failures.
Store the password in a password manager now. There is no reset for a restic repository: lose the password and the backups are unreadable.
Run Your First Backup
Back up the directories that matter and exclude the ones that do not. For a typical WordPress VPS, that means the web root, the database dump directory, and your Nginx and PHP config. Skip caches, logs, and session files.
restic backup /var/www /etc/nginx /etc/php
--exclude /var/www/*/wp-content/cache
--exclude '*.log'
--tag vps-first-backup
Tags help later when you want to list or restore one kind of backup. Add --verbose on the first run so you can watch what gets uploaded and confirm the size matches what you expect. For a fuller walkthrough of the same pattern on a live server, see this guide to back up a VPS to S3-compatible storage with restic.
Databases need a dump before restic can back them up, since restic copies files, not live database state. Dump MySQL or MariaDB first, then include the dump directory:
mkdir -p /root/backups
mysqldump --single-transaction --all-databases > /root/backups/all-databases.sql
restic backup /root/backups --tag db-dump
If you run apps in containers, the same idea applies to volumes. Dump or stop what needs a consistent state first, then back up the volume data. This guide on how to back up Docker volumes covers the container side in detail.
Check Snapshots and Set a Retention Policy
List your snapshots to confirm the backup landed:
restic snapshots
Each snapshot shows an ID, the time it was taken, the host, the tags, and the paths it covers. Run backups daily and the list grows fast, so set a retention policy with forget. This policy keeps 7 daily, 4 weekly, and 6 monthly snapshots:
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
The --prune flag removes data that no kept snapshot references, which frees storage. Run forget without --prune and old data stays in the bucket until you prune. A restic backup retention policy like this one balances history against storage cost: a week of daily rollback points, a month of weekly points, and half a year of monthly points.
Test a Restore Before You Need One
A restore test is the step most people skip, and it is the step that decides whether your backups work. Pick a single file from a snapshot and restore it to a temp directory, without touching the live site.
restic restore latest --target /tmp/restore-test --include /var/www/html/index.php
Open the restored file and compare it with the live one. Then test a full directory restore to a scratch location and check that the file count matches:
restic restore latest --target /tmp/restore-full --path /var/www
diff -r /tmp/restore-full/var/www /var/www --brief | head -20
Differences in cache files and logs are normal. Differences in config files or uploads are not, and they tell you the backup missed something. This comparison of how restic compares with Duplicati and Kopia covers when another tool fits better.
Automate Backups with Cron
Daily backups only happen if they run on their own. Write a small script that loads the env file, dumps the database, backs up, forgets old snapshots, and logs the result.
sudo nano /usr/local/bin/vps-backup.sh
#!/bin/bash
set -e
source /root/restic/env.sh
mkdir -p /root/backups
mysqldump --single-transaction --all-databases > /root/backups/all-databases.sql
restic backup /var/www /etc/nginx /etc/php /root/backups
--exclude /var/www/*/wp-content/cache
--exclude '*.log'
--tag daily
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
echo "Backup finished: $(date)" >> /var/log/restic-backup.log
sudo chmod +x /usr/local/bin/vps-backup.sh
sudo crontab -e
Add a nightly run at 2 AM, when traffic is low:
0 2 * * * /usr/local/bin/vps-backup.sh >> /var/log/restic-backup.log 2>&1
Check the log the next morning. A cron job that fails silently for a month is the same as no backup at all, so add an alert on failure if you can.
Common Mistakes to Avoid
- Storing the password only on the server. If the server dies, the password dies with it. Keep a copy in a password manager.
- Backing up the live database files. Copying MySQL data files while the server runs gives you a corrupt dump. Dump first, back up second.
- Skipping the restore test. Run one before you trust the setup, and repeat it after any big change to paths or excludes.
- Forgetting to prune. Snapshots pile up and storage grows. The forget policy in your script handles this, but only if the script runs.
- Using one repository for everything. Separate repos per server keep a prune or a bad command on one box from touching another box backups.
FAQ
Does restic encrypt backups by default?
Yes. Every restic repository is encrypted with the password you set at init. There is no unencrypted mode, and the storage provider only ever receives encrypted data.
Can I use restic with any S3-compatible storage?
Yes, as long as the provider speaks the S3 API. Set the endpoint in the repository URL and use the access key the provider gives you. Check the provider docs for the exact endpoint host.
How do I restore a single file from an old snapshot?
List snapshots with restic snapshots, copy the snapshot ID, then run restic restore <snapshot-id> --target /tmp/restore --include /path/to/file. The file lands under the target directory with its full path.
What happens if I lose the restic password?
The backups become unreadable. There is no recovery path, so store the password in a password manager separate from the server.
Set this up once, test the restore, and let cron do the rest. The next time a disk fails or an update goes wrong, the fix is one restore command instead of a rebuild from scratch.