> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Restic Backup Tutorial: Encrypted Backups on Linux

> Restic backup tutorial for Linux servers: create an encrypted repository on SFTP or S3, run restic backup, keep versions with forget --prune and restore files.

restic makes encrypted, deduplicated, versioned backups. Create a repository with `restic -r sftp:backup@198.51.100.20:/srv/restic init`, back up with `restic backup /etc /home /var/www`, keep a set number of versions with `restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune`, and restore with `restic restore latest --target /tmp/restore`. Only changed data is uploaded.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Intermediate | 30 minutes | Ubuntu, Debian, AlmaLinux, Rocky Linux, Fedora, Arch, Alpine | sudo, storage reachable over SFTP, S3 or rclone |

## Before you start

* [Connect with SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md). Commands use `sudo`; root users can leave it out. Backups run as root so they can read every file.
* Decide where the **repository** (restic's storage folder) lives: another server over SFTP, S3-compatible object storage, or any service rclone supports.
* For SFTP, set up an [SSH key](https://help.freakhosting.com/portals/other/how-to-create-and-use-ssh-keys.md) for root so restic can log in without a password.

## How to install restic

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo apt update && sudo apt install restic
> ```

> [tab] AlmaLinux / Rocky
>
> restic comes from EPEL:
>
> ```bash theme={null}
> sudo dnf install epel-release
> sudo dnf install restic
> ```

> [tab] Fedora
>
> ```bash theme={null}
> sudo dnf install restic
> ```

Check it with `restic version`. Ubuntu 24.04 ships restic 0.16.4. If you need a newer version, use the official binary from the restic project, which you can update with `restic self-update` (this does not work for distribution packages).

## How to create a restic repository

**1. Create a password file**

```bash theme={null}
sudo mkdir -p /root/.restic
sudo sh -c 'openssl rand -base64 32 > /root/.restic/password'
sudo chmod 600 /root/.restic/password
```

Copy this password into a password manager now.

**2. Choose the repository location**

> [tab] SFTP (another server)
>
> ```bash theme={null}
> sudo restic -r sftp:backup@198.51.100.20:/srv/restic \
>   --password-file /root/.restic/password init
> ```
>
> For a custom SSH port use the URL form: `sftp://backup@198.51.100.20:2222//srv/restic`.

> [tab] S3-compatible storage
>
> ```bash theme={null}
> sudo AWS_ACCESS_KEY_ID=EXAMPLEKEYID AWS_SECRET_ACCESS_KEY=EXAMPLESECRET \
>   restic -r s3:https://s3.example.com/my-bucket \
>   --password-file /root/.restic/password init
> ```
>
> restic needs path-style addresses (`s3:https://endpoint/bucket`). Put the two `AWS_` keys in the environment file below as well.

> [tab] Through rclone
>
> After [setting up an rclone remote](https://help.freakhosting.com/portals/other/how-to-back-up-to-cloud-storage-with-rclone.md):
>
> ```bash theme={null}
> sudo restic -r rclone:gdrive:restic --password-file /root/.restic/password init
> ```

You see `created restic repository … at …`.

> [!WARNING]
>
> The restic documentation is clear: if you lose the repository password, the data in it is lost for good. Nobody can recover it. Keep a copy of the password off the server.

## How to back up with restic

Save the settings in an environment file so you don't type them every time:

```bash theme={null}
# /root/.restic/env
export RESTIC_REPOSITORY=sftp:backup@198.51.100.20:/srv/restic
export RESTIC_PASSWORD_FILE=/root/.restic/password
# For S3 storage, also:
# export AWS_ACCESS_KEY_ID=EXAMPLEKEYID
# export AWS_SECRET_ACCESS_KEY=EXAMPLESECRET
```

Make it readable only by root: `sudo chmod 600 /root/.restic/env`.

Then run a backup:

```bash theme={null}
sudo bash -c 'source /root/.restic/env && restic backup /etc /home /root /var/www \
  --exclude /var/www/example.com/cache --tag daily'
```

Example output:

```text theme={null}
Files:          2140 new,     0 changed,     0 unmodified
Dirs:            312 new,     0 changed,     0 unmodified
Added to the repository: 412.311 MiB (180.904 MiB stored)
snapshot 578b9a8f saved
```

The next run uploads only data that changed. Dump your databases to a file first and include that file; see [back up MySQL databases](https://help.freakhosting.com/portals/other/how-to-back-up-and-restore-mysql-databases.md).

## How to list snapshots and delete old ones

```bash theme={null}
sudo bash -c 'source /root/.restic/env && restic snapshots'
```

Keep 7 daily, 4 weekly and 6 monthly snapshots and free the space:

```bash theme={null}
sudo bash -c 'source /root/.restic/env && restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune'
```

`forget` on its own only removes the snapshot list entries; `--prune` deletes the data no snapshot needs any more. Add `--dry-run` to see what would be removed first. Afterwards, check the repository:

```bash theme={null}
sudo bash -c 'source /root/.restic/env && restic check'
```

Success ends with `no errors were found`.

## How to restore files from a restic backup

**1. Find the file**

```bash theme={null}
sudo bash -c 'source /root/.restic/env && restic ls latest /etc/nginx'
```

**2. Restore to a temporary folder**

```bash theme={null}
sudo bash -c 'source /root/.restic/env && restic restore latest --target /tmp/restore --include /etc/nginx'
```

Files appear under their full path, for example `/tmp/restore/etc/nginx/nginx.conf`. Use a snapshot ID such as `578b9a8f` instead of `latest` for an older version.

**3. Copy back what you need**

Compare the restored files with the live ones, then copy them into place.

> [!WARNING]
>
> Restoring with `--target /` writes straight over your live files. Restore to a temporary folder first unless you are rebuilding a server.

## How to run restic backups automatically

Create `/usr/local/bin/restic-backup.sh`:

```bash theme={null}
#!/bin/bash
set -euo pipefail
source /root/.restic/env
restic backup /etc /home /root /var/www --exclude /var/www/example.com/cache --tag daily
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic check
```

Make it executable with `sudo chmod 700 /usr/local/bin/restic-backup.sh` and add it to root's crontab (`sudo crontab -e`):

```bash theme={null}
45 2 * * * /usr/local/bin/restic-backup.sh >> /var/log/restic-backup.log 2>&1
```

See [schedule tasks with cron](https://help.freakhosting.com/portals/other/how-to-schedule-tasks-with-cron.md).

## Frequently asked questions

### Is restic encrypted?

Yes. Everything in the repository is encrypted with your password before it leaves the server, so the storage provider cannot read it.

### What does "repository is already locked" mean?

Another restic command is running or one was interrupted. If you are sure nothing else is running, remove stale locks with `restic unlock`.

### Can I back up several servers to one repository?

Yes. Snapshots record the host name, and `forget` groups snapshots by host and paths by default, so each server keeps its own versions. Every server needs the repository password.

### restic or rsync: which should I use?

Use restic when you want encrypted, versioned backups on storage you don't fully trust. Use [rsync](https://help.freakhosting.com/portals/other/how-to-back-up-files-with-rsync.md) when you want a plain, readable copy on another server.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every guide in this section.

> [card] [Back up a Linux server](https://help.freakhosting.com/portals/other/how-to-back-up-a-linux-server.md)
>
> What to back up and which tool to use.

> [card] [Back up to cloud storage](https://help.freakhosting.com/portals/other/how-to-back-up-to-cloud-storage-with-rclone.md)
>
> Set up rclone remotes.

> [card] [Back up with rsync](https://help.freakhosting.com/portals/other/how-to-back-up-files-with-rsync.md)
>
> Plain copies over SSH.

> [card] [VPS Panel backups](https://help.freakhosting.com/portals/vps-panel/how-to-schedule-and-restore-backups.md)
>
> Full-disk backups in the panel.

***

> [!NOTE]
>
> **Checked against the official [restic documentation](https://restic.readthedocs.io/en/stable/)** on 4 November 2026; `init`, `backup`, `snapshots`, `forget --prune`, `check`, `ls` and `restore` tested on Ubuntu 24.04 LTS (restic 0.16.4) with a local repository. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.