How to Back Up a VPS to S3-Compatible Storage with Restic

How to Back Up a VPS to S3-Compatible Storage with Restic

Restic is a backup tool that encrypts, deduplicates, and snapshots your data, then stores it in a repository that can live on S3-compatible storage. To back up vps to s3 with Restic, you create a repository in a bucket, set a strong repository password, and run scheduled snapshots of your site files and database dumps. This guide sets that up on Ubuntu, with the commands to restore a file, since a backup you have never restored is only a theory.

Restic differs from a plain tar archive in three ways that matter on a server. Snapshots are incremental, so each nightly run only sends changed data and finishes fast. Every snapshot is a full view of the data at that time, so restoring last Tuesday does not require replaying a chain of incremental files. And the repository is encrypted with a password you hold, so the storage provider cannot read your backups. Lose that password and the backups are unreadable, so store it somewhere safe and separate from the server.

Back Up VPS to S3 Storage: Install Restic and Create the Repository

Install Restic from your distribution or the official binary. The package version on Ubuntu is usually fine for a first setup, but check the version, since very old builds lack newer repository features. Set the environment variables for the repository location and the S3 credentials, and keep them in a file readable only by root rather than in shell history.

sudo apt install restic
export AWS_ACCESS_KEY_ID="your-key"
export AWS_SECRET_ACCESS_KEY="your-secret"
export RESTIC_REPOSITORY="s3:https://s3.eu-central-1.example.com/my-vps-backups"
export RESTIC_PASSWORD="a-long-random-passphrase"

The endpoint in the example is a placeholder for your provider’s S3 endpoint, which you will find in the provider docs. Providers with S3-compatible APIs include Backblaze B2, Wasabi, and Hetzner Storage Box, and our comparison of Backblaze B2, Hetzner Storage Box, and Wasabi weighs their trade-offs for this exact use. Create the repository once with the init command, and confirm it reports success before any data goes in.

restic init
restic snapshots

Any s3-compatible storage bucket works as the target, as long as the provider supports the S3 API and the bucket already exists. A fresh repository with zero snapshots is the expected output at this stage. If init fails with an authentication error, check the endpoint URL and the bucket name first, since a wrong region in the endpoint is the most common cause. Create the bucket in the provider panel before running init, and give the credentials access to that bucket only, not to the whole storage account.

Run the First Snapshot and Automate Nightly Backups

Take a first snapshot of the paths that matter: the web root, a database dump directory, and a config backup directory. Exclude caches, logs, and session directories, since they add size and never need restoring. The exclude flags below keep the snapshot lean without touching anything you would miss.

mysqldump -u root exampledb > /backup/db.sql
restic backup /var/www/example.com /backup 
  --exclude "*/cache/*" --exclude "*/logs/*" 
  --tag nightly

The restic snapshot command is the core of the routine, and the tag makes later cleanup and restores easier to target. After the first run, list snapshots and check the size reported, then run the same backup a second time and confirm it finishes much faster, since only changed files get sent. That speed difference is deduplication working, and it is what makes nightly Restic runs cheap on bandwidth.

Automate the job with a cron entry that sources a credentials file, so keys never sit in the crontab itself. The credentials file should be mode 600 and owned by root. Log the output so a failed night is visible in the morning rather than discovered during an emergency.

0 3 * * * /usr/local/bin/restic-backup.sh >> /var/log/restic.log 2>&1

The script itself should dump the database first, run the backup, then run a forget and prune pass with a retention policy, such as keeping seven daily, four weekly, and six monthly snapshots. Without pruning, the repository grows forever. With pruning, old snapshots leave on a schedule you control. Our guide on backing up Docker volumes automatically shows the same pattern for container data, where the paths differ but the snapshot logic is identical.

Restore a File and Verify the Backup

Restoring a single file is the test every admin should run in the first week. The restic restore file workflow below targets a temporary directory on purpose, so a bad restore never overwrites live data. List snapshots, pick one, and restore one file to a temporary directory, not over the live file. Compare the restored file with the live one, then delete the test copy. If this test fails, the backup job has a problem worth fixing now.

restic snapshots
restic restore latest --target /tmp/restore-test --include /var/www/example.com/index.php
diff /tmp/restore-test/var/www/example.com/index.php /var/www/example.com/index.php

A full restore works the same way, with a target directory and no include filter, followed by fixing ownership and restarting services. Practice it on a test VPS if you can, since a full restore also needs the database import and the Nginx config, and those steps are easy to fumble under pressure. Keep a short restore note in the same place as the repository password, listing the endpoint, bucket, and commands, because the person restoring may not be the person who set the job up.

Check repository health on a schedule too. The restic check command verifies the repository structure, and a monthly check catches corruption early. A repository that fails its check needs attention before it is the only copy left. Also keep an eye on bucket size and cost in the provider panel, since a prune job that stops running shows up first as a growing bill. For a tool comparison before you commit, see our Duplicati vs Restic vs Kopia comparison and our roundup of the best paid backup tools for Docker volumes in 2026.

Limits: What Restic Does Not Do for You

Restic does not back up a running database correctly from raw files. Dumping the database first, as this guide does, is required, because copying live database files can produce a corrupt restore. Restic also does not protect the repository password or the S3 credentials. If an attacker gains root on the server, they can read the credentials file and delete snapshots, unless the bucket has versioning or object lock that blocks deletion. Turn those on when the provider offers them.

Finally, Restic does not tell you when a backup job silently stops unless you watch the logs. Pair the cron job with a monitor that alerts on a missing snapshot, such as a check that lists snapshots and warns when the newest one is older than a day. A backup system with no failure alert is a backup system that fails quietly. Set the job up, test a restore, watch one week of logs, and only then call the server backed up.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.