Every server eventually has a bad day: a failed upgrade, a deleted directory, a disk that gives up without warning. The difference between a disaster and a minor inconvenience is a backup you can actually restore from. Restic is a fast, open-source backup tool that encrypts everything on your machine before it ever leaves it, stores it as space-efficient snapshots, and works beautifully with S3-compatible storage. In this guide you will install Restic on Ubuntu 24.04, point it at an S3 bucket, take your first encrypted backup, and automate the whole thing.
Why Restic?
Most backup tools either skip encryption or make it an afterthought. Restic encrypts by design: your repository password derives the encryption keys, so even if someone gets full access to your S3 bucket, they see nothing but scrambled data. It also deduplicates aggressively, so backing up the same 10 GB of files every night only stores what actually changed. Snapshots, a simple retention policy, and a single static binary complete a setup that is genuinely hard to break.
This guide targets a server running Ubuntu 24.04 LTS. The same steps work on Ubuntu 22.04. You will need an S3 bucket (AWS S3, or any S3-compatible provider such as Wasabi, Backblaze B2, or a self-hosted MinIO server) and an access key with permission to read and write that bucket.
Prerequisites
- An Ubuntu 24.04 server with sudo access
- An S3 bucket created in your provider's console, plus an access key ID and secret access key
- A strong repository password you will store safely (losing it means losing the backups)
- About 20 minutes
Step 1: Install Restic on Ubuntu
Restic is in Ubuntu's official repositories, so installation is one command. Update the package index first, then install it.
sudo apt update
sudo apt install restic -y
The first command refreshes the list of available packages; the second installs Restic and its dependencies. Verify the installation and note the version:
restic version
On Ubuntu 24.04 you should see Restic 0.16.x. That version is stable and fully featured, so there is no need to chase the latest release. If you ever do want a newer version, the project publishes static binaries on its GitHub releases page that you can drop into /usr/local/bin, but the apt package is the right choice for a server you plan to maintain.
Step 2: Prepare Your S3 Bucket and Credentials
Create a bucket in your S3 provider's console. The name must be globally unique on AWS S3, so something like myserver-backups-2026 works better than backups. Leave the bucket private, enable default encryption if your provider offers it, and consider turning on versioning so an accidental delete does not wipe your backup history.
Next, create an access key with permission to use that bucket. On AWS, make an IAM user with a policy granting s3:GetObject, s3:PutObject, s3:DeleteObject, s3:ListBucket on your bucket only. Never use your root AWS credentials for backups. Save the access key ID and secret access key somewhere safe; the secret is shown only once.
Restic talks to S3 through the same environment variables as the AWS CLI, so export them in your shell:
export AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
Replace the placeholder values with your real credentials. These variables only live for the current shell session; later in this guide you will put them in a protected file for the automated backup.
Step 3: Set the Repository Location and Password
Restic needs two things to find your backup repository: where it lives and the password that unlocks it. The repository location is an S3 URL in the form s3:https://endpoint/bucket/path. For AWS S3 in the Mumbai region, that looks like this:
export RESTIC_REPOSITORY="s3:https://s3.ap-south-1.amazonaws.com/myserver-backups-2026/restic"
export RESTIC_PASSWORD="a-very-long-random-password-you-store-safely"
The repository URL points at a folder inside your bucket. Restic will create that folder itself. The password encrypts the entire repository, so make it long and random, and store it in a password manager. There is no password reset: if you lose it, the backups are unreadable forever.
For day-to-day use, storing the password in an environment variable is fine. For the automated backup later, you will use a password file instead, so create one now and lock down its permissions:
echo "a-very-long-random-password-you-store-safely" > ~/.restic-password
chmod 600 ~/.restic-password
The chmod 600 makes the file readable only by your user. Anyone who can read this file can read your backups, so treat it like a private key.
Step 4: Initialise the Repository
With the environment variables set, initialise the repository. This creates the encrypted structure inside your S3 bucket:
restic init
You should see output confirming the repository was created, including its ID. The init command only needs to run once per repository. If you run it again on an existing repository, Restic will simply tell you it already exists, which is harmless.
If init fails with a 403 error, your access key lacks permission on the bucket. If it fails with a "no such host" or connection error, double-check the endpoint region in the repository URL: s3.ap-south-1.amazonaws.com is only correct if your bucket is actually in ap-south-1. Match the endpoint to your bucket's region.
Step 5: Take Your First Backup
Now back up the directories that matter. A typical web server backup covers the web root, configuration, and home directories:
restic backup /etc /home /var/www --exclude /home/*/.cache
Restic scans the directories, splits files into chunks, encrypts them, deduplicates against anything already stored, and uploads only new data. The --exclude flag skips cache directories that are not worth backing up. The first backup is the slowest because everything is new; later backups only upload changes and finish in seconds or minutes.
When it finishes, Restic prints a summary showing how many files were processed, how much data was added, and the snapshot ID. Note that ID: every backup creates a snapshot, and you restore from a specific snapshot.
For a database server, back up a database dump rather than the live database files. Dump MySQL first, then include the dump in the backup:
mysqldump --all-databases --single-transaction > /home/backups/mysql-dump.sql
restic backup /home/backups --tag mysql
The --single-transaction flag keeps the dump consistent without locking your tables. The --tag mysql labels the snapshot so you can find database backups easily later.
Step 6: List Snapshots and Restore a File
A backup you have never restored is a backup you have not tested. List your snapshots to confirm the backup landed:
restic snapshots
You will see a table with one row per snapshot: its ID, the date, the host, the tags, and the paths it covers. To restore a single file or a whole directory from the latest snapshot, use:
restic restore latest --target /tmp/restore --path /etc/nginx/nginx.conf
This extracts /etc/nginx/nginx.conf from the most recent snapshot into /tmp/restore, preserving its original path underneath. To restore an entire snapshot, drop the --path flag:
restic restore latest --target /tmp/restore
To restore a specific older snapshot, replace latest with the snapshot ID from the snapshots list. Open the restored files, check they look right, and delete the test directory when you are done. Do this restore test at least once after your first backup, and again after any major change to what you back up.
Step 7: Set a Retention Policy and Prune Old Snapshots
Daily backups pile up fast. Restic's forget command applies a retention policy, and --prune actually deletes the data that is no longer referenced:
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
This keeps the last 7 daily snapshots, the last 4 weekly snapshots, and the last 6 monthly snapshots, then removes everything else and reclaims the storage. Adjust the numbers to fit your storage budget: keeping more history costs more S3 storage, keeping less means fewer restore points.
Run a repository integrity check occasionally to make sure the stored data is intact:
restic check
This verifies that every chunk referenced by your snapshots is present and readable. Run it monthly, or after any storage incident.
Step 8: Automate Nightly Backups with Cron
Manual backups get forgotten. Put the credentials and repository settings in a protected environment file, then schedule Restic with cron.
Create /root/restic-env (or a file in your home directory if you run as a non-root user) with your settings:
RESTIC_REPOSITORY="s3:https://s3.ap-south-1.amazonaws.com/myserver-backups-2026/restic"
RESTIC_PASSWORD_FILE="/root/.restic-password"
AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
Lock it down and add a cron job that runs every night at 2 AM, backs up, applies the retention policy, and logs the result:
chmod 600 /root/restic-env
crontab -e
0 2 * * * . /root/restic-env; restic backup /etc /home /var/www --exclude /home/*/.cache >> /var/log/restic-backup.log 2>&1; restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune >> /var/log/restic-backup.log 2>&1
The leading . /root/restic-env loads your settings into the cron shell. The log file at /var/log/restic-backup.log is where you check that the nightly job actually ran: look for the "snapshot saved" line each morning for the first week, and set up an alert if the log stops growing. A backup job that fails silently is barely better than no backup at all.
Verify It Worked
Run through this checklist after setup:
restic snapshotsshows at least one snapshot with today's date.restic restore latest --target /tmp/restorebrings back a file you can open and read.restic checkreports no errors.- The cron log at
/var/log/restic-backup.logshows a successful run the morning after your first scheduled night. - Your S3 console shows the bucket growing by roughly the size of your data, not ten times it: deduplication should keep the stored size close to the size of one full backup plus changes.
Common Issues and Fixes
1. "Fatal: wrong password" or "unable to open config file"
The repository password does not match the one used at init time. Check for a typo, a trailing space in the password file, or a different RESTIC_PASSWORD_FILE path. If the password is genuinely lost, the repository cannot be recovered: create a new repository with a new password and back up again.
2. S3 403 Forbidden errors
Your access key cannot write to the bucket. Verify the IAM policy grants the four actions (GetObject, PutObject, DeleteObject, ListBucket) on the right bucket, and that the repository URL points at the bucket in the region your endpoint names. A bucket policy that denies access will also produce 403s even with a correct IAM policy.
3. "Repository is already locked" and the backup refuses to run
A previous Restic run was killed or crashed while holding the lock. Check that no Restic process is running with ps aux | grep restic, then release the stale lock:
restic unlock
Never run unlock while another backup is genuinely in progress, or the two runs will corrupt each other's uploads.
4. The first backup is extremely slow
That is normal: the first run uploads everything. Check your server's upload bandwidth, and if the data set is huge, run the first backup from a screen or tmux session so a dropped SSH connection does not kill it. Later backups are incremental and much faster.
5. "No space left on device" in /tmp during backup
Restic uses a local cache directory to speed up later runs. If /tmp is small, point the cache elsewhere with --cache-dir /var/cache/restic or set RESTIC_CACHE_DIR. You can also clear the cache safely at any time: Restic rebuilds it on the next run.
Conclusion
You now have encrypted, deduplicated, snapshot-based backups of your Ubuntu server flowing into S3 every night, with a retention policy that keeps storage costs under control and a restore procedure you have actually tested. The setup takes under half an hour and protects you from accidental deletion, bad upgrades, and dead disks. Set a monthly reminder to run restic check and a test restore, and your backups will be there when you need them.
If you would rather have an expert set up and monitor backups for your business servers, XpertBees handles server setup, hardening, and backup automation for clients across India and beyond. Reach out through the contact page and we will take it from there.



