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

# Back up a Linux VPS with restic: files and databases

> Back up your Linux VPS files and MySQL, MariaDB and PostgreSQL databases with restic: encrypted, scheduled with systemd, restores tested. OnetSolutions guide.

*Published on November 16, 2026*

Backing up a Linux VPS with restic gives you an encrypted copy of your files and databases off the server, with months of history, and lets you restore a single item with one command.

The [automatic VPS backup](https://blog.onetsolutions.net/en/free-vps-backup-trial.md) copies the whole disk every night and restores it in one go from the console. restic adds the finer layer: it restores one configuration file, one site directory or one database, on this server or another, without touching anything else.

This guide sets up that second layer on Debian, Ubuntu, AlmaLinux and Rocky Linux: file and database backups, scheduling, retention, verification and a restore test.

## VPS backup, snapshot or restic?

| | VPS backup | Snapshot | restic |
| - | - | - | - |
| What is copied | The whole disk | The VPS state at one point in time | The files and databases you choose |
| Trigger | Every night, or on demand | On demand, before a change | Your systemd timer |
| Storage | Managed by OnetSolutions | Managed by OnetSolutions | The S3-compatible or SFTP repository you choose |
| Restore | The whole VPS, from the console | The whole VPS, from the console | One file, directory or database, wherever you want |
| History | The nightly backups | The snapshot you took | The retention policy you set |

> [!TIP]
>
> All three work together: a [snapshot](https://help.onetsolutions.net/vps/snapshot) before an upgrade, the [nightly backup](https://help.onetsolutions.net/vps/backup) to bring the server back, restic to recover one specific piece of data.

## Install restic

Versions in the official repositories, checked on November 16, 2026. The latest upstream release is 0.19.1 (July 5, 2026).

| Distribution | Package | Version | `--stdin-from-command` |
| - | - | - | - |
| Debian 12 (Bookworm) | `restic` | 0.14.0 | No |
| Debian 13 (Trixie) | `restic` | 0.18.0 | Yes |
| Ubuntu 24.04 LTS | `restic` | 0.16.4 | No |
| Ubuntu 26.04 LTS | `restic` | 0.18.1 | Yes |
| AlmaLinux 9 and 10, Rocky Linux 9 and 10 | `restic` (EPEL) | 0.19.1 | Yes |

`--stdin-from-command` streams the output of a dump command straight into the repository. It arrived in restic 0.17.0. A variant for earlier versions is given below.

```bash theme={null}
# Debian, Ubuntu
sudo apt install restic

# AlmaLinux, Rocky Linux
sudo dnf install epel-release
sudo dnf install restic

restic version
```

## Prepare the backup repository

A restic repository is encrypted and deduplicated: each run only uploads new blocks. It must live off the VPS.

### Credentials in a single file

restic reads its repository and password from environment variables. Keep them in a file only root can read:

```bash theme={null}
sudo install -d -m 700 /etc/restic

# Random repository password
head -c 32 /dev/urandom | base64 | sudo tee /etc/restic/password >/dev/null
sudo chmod 600 /etc/restic/password
```

> [!WARNING]
>
> Copy this password into your password manager, off the server. The restic documentation is explicit: without it, the data in the repository is lost. If the VPS goes away, it is the only key to your backups.

### Option 1: S3-compatible storage

```bash theme={null}
sudo tee /etc/restic/env >/dev/null <<'EOF'
RESTIC_REPOSITORY=s3:https://s3.example.com/my-bucket
RESTIC_PASSWORD_FILE=/etc/restic/password
RESTIC_CACHE_DIR=/var/cache/restic
AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
EOF
sudo chmod 600 /etc/restic/env
```

If your provider requires a region, add `AWS_DEFAULT_REGION=` with its value.

### Option 2: an SFTP server

restic uses the system `ssh` client. The login must use a key, with no password prompt, or the timer cannot run on its own:

```bash theme={null}
# Dedicated key for restic, owned by root
sudo ssh-keygen -t ed25519 -f /root/.ssh/restic -N ''
sudo ssh-copy-id -i /root/.ssh/restic.pub backup@repo.example.com

# An SSH alias for the repository
sudo tee -a /root/.ssh/config >/dev/null <<'EOF'
Host restic-repo
    HostName repo.example.com
    User backup
    IdentityFile /root/.ssh/restic
    ServerAliveInterval 60
    ServerAliveCountMax 240
EOF
sudo chmod 600 /root/.ssh/config

# First connection: accept the host key, then type bye
sudo sftp restic-repo
```

The two `ServerAlive` lines come from the restic documentation: they stop the server from dropping the connection while restic walks through a large set of unchanged files.

```bash theme={null}
sudo tee /etc/restic/env >/dev/null <<'EOF'
# Path relative to the remote user's home directory
RESTIC_REPOSITORY=sftp:restic-repo:backups/my-vps
RESTIC_PASSWORD_FILE=/etc/restic/password
RESTIC_CACHE_DIR=/var/cache/restic
EOF
sudo chmod 600 /etc/restic/env
```

To manage your VPS SSH keys, see [SSH keys](https://help.onetsolutions.net/vps/ssh-keys).

### Initialize the repository

```bash theme={null}
# Root shell with the variables from the file
sudo -i
set -a; . /etc/restic/env; set +a

restic init
```

**Check.** `restic cat config` prints the repository configuration with no error. Keep this shell open for the next steps.

## Back up files

**Problem.** The system can be reinstalled. Your configuration and your sites cannot.

**Solution.** Back up the directories that hold your data, and leave caches out:

```bash theme={null}
tee /etc/restic/excludes >/dev/null <<'EOF'
/var/www/*/cache
/home/*/.cache
*.tmp
EOF

restic backup --tag files \
  --one-file-system \
  --exclude-caches \
  --exclude-file /etc/restic/excludes \
  /etc /var/www /home /root
```

`--one-file-system` stays on the file system of each path. `--exclude-caches` skips directories that contain a `CACHEDIR.TAG` file. Adjust the paths to your server: `/srv`, `/opt`, Docker volumes.

**Check.** `restic snapshots --tag files` lists the new snapshot.

## Consistent MySQL and MariaDB backups

**Problem.** Copying `/var/lib/mysql` while the server runs gives inconsistent files.

**Solution.** Take a logical dump with `--single-transaction`: it reads InnoDB tables exactly as they were when the transaction started, without blocking applications. restic stores the output directly, with no intermediate file on disk:

```bash theme={null}
# One snapshot per database, so you can restore just one
for DB in $(mariadb -N -e 'SHOW DATABASES' | grep -Ev '^(information_schema|performance_schema|sys)$'); do
  restic backup --tag mariadb --stdin-filename "mariadb-$DB.sql" \
    --stdin-from-command -- \
    mariadb-dump --single-transaction --quick --routines --events "$DB"
done
```

On MySQL, replace `mariadb` with `mysql` and `mariadb-dump` with `mysqldump`: the options are the same. `--routines` and `--events` add stored procedures and scheduled events; triggers are included by default.

As root, the login goes through the Unix socket on Debian and Ubuntu. If your server asks for a password, put it in `/root/.my.cnf`:

```ini theme={null}
[client]
user=root
password=YOUR_PASSWORD
```

```bash theme={null}
chmod 600 /root/.my.cnf
```

With `--stdin-from-command`, restic watches the exit code of the dump. If `mariadb-dump` fails, the backup is cancelled and no snapshot is created.

**Check.** `restic snapshots --tag mariadb` shows one line per database, with the path `/mariadb-<database>.sql`.

## Back up PostgreSQL

**Problem.** Same constraint: the files under `/var/lib/postgresql` cannot be copied while the server runs.

**Solution.** `pg_dump` produces a consistent export even while the database is in use, without blocking readers or writers. It exports one database at a time; roles are backed up separately with `pg_dumpall --globals-only`:

```bash theme={null}
# One database per snapshot
for DB in $(runuser -u postgres -- psql -At -c 'SELECT datname FROM pg_database WHERE NOT datistemplate'); do
  restic backup --tag postgresql --stdin-filename "postgresql-$DB.sql" \
    --stdin-from-command -- \
    runuser -u postgres -- pg_dump "$DB"
done

# Roles and tablespaces
restic backup --tag postgresql --stdin-filename postgresql-globals.sql \
  --stdin-from-command -- \
  runuser -u postgres -- pg_dumpall --globals-only
```

Keep the plain SQL format, uncompressed: restic already compresses data (repository format 2, the default since restic 0.14.0), and an uncompressed file deduplicates well from one night to the next.

**Check.** `restic snapshots --tag postgresql` shows one line per database, plus `postgresql-globals.sql`.

> [!NOTE]
>
> On Debian 12 and Ubuntu 24.04, the packaged restic predates `--stdin-from-command`. Write the dump to a file, then back up the directory:
>
> ```bash theme={null}
> install -d -m 700 /var/backups/dumps
> mariadb-dump --single-transaction --quick --routines --events app > /var/backups/dumps/mariadb-app.sql \
>   && restic backup --tag mariadb /var/backups/dumps
> ```
>
> Avoid `mariadb-dump ... | restic backup --stdin`: the restic documentation advises against it, because a failed dump still produces a snapshot.

## Schedule backups with a systemd timer

**Problem.** A backup you run by hand eventually stops being run.

**Solution.** A script holds the steps, a service runs it, a timer schedules it.

```bash theme={null}
tee /usr/local/sbin/restic-backup >/dev/null <<'EOF'
#!/bin/sh
# restic backup: files, MariaDB, PostgreSQL, retention, verification.
# Remove the blocks for database engines this server does not run.
STATUS=0

restic backup --tag files --one-file-system --exclude-caches \
  --exclude-file /etc/restic/excludes \
  /etc /var/www /home /root || STATUS=1

for DB in $(mariadb -N -e 'SHOW DATABASES' | grep -Ev '^(information_schema|performance_schema|sys)$'); do
  restic backup --tag mariadb --stdin-filename "mariadb-$DB.sql" --stdin-from-command -- \
    mariadb-dump --single-transaction --quick --routines --events "$DB" || STATUS=1
done

for DB in $(runuser -u postgres -- psql -At -c 'SELECT datname FROM pg_database WHERE NOT datistemplate'); do
  restic backup --tag postgresql --stdin-filename "postgresql-$DB.sql" --stdin-from-command -- \
    runuser -u postgres -- pg_dump "$DB" || STATUS=1
done
restic backup --tag postgresql --stdin-filename postgresql-globals.sql --stdin-from-command -- \
  runuser -u postgres -- pg_dumpall --globals-only || STATUS=1

restic forget --prune --keep-daily 7 --keep-weekly 5 --keep-monthly 12 || STATUS=1
restic check --read-data-subset=5% || STATUS=1

exit $STATUS
EOF
chmod 700 /usr/local/sbin/restic-backup
```

```ini theme={null}
# /etc/systemd/system/restic-backup.service
[Unit]
Description=restic backup of files and databases
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
EnvironmentFile=/etc/restic/env
ExecStart=/usr/local/sbin/restic-backup
Nice=10
IOSchedulingClass=idle
```

```ini theme={null}
# /etc/systemd/system/restic-backup.timer
[Unit]
Description=Daily restic backup

[Timer]
OnCalendar=*-*-* 03:30:00
RandomizedDelaySec=30min
Persistent=true

[Install]
WantedBy=timers.target
```

`Persistent=true` catches up on a missed run if the VPS was off at the scheduled time. `RandomizedDelaySec` spreads the start, which helps when several servers write to the same storage.

```bash theme={null}
systemctl daemon-reload
systemctl enable --now restic-backup.timer

# First run right away, without waiting for the night
systemctl start restic-backup.service
journalctl -u restic-backup.service -n 50
```

**Check.** `systemctl list-timers restic-backup.timer` shows the next run, and `systemctl status restic-backup.service` shows `status=0/SUCCESS` after the first run.

## Retention: restic forget --prune

**Problem.** Without a retention policy, the repository only grows.

**Solution.** The `forget` line in the script keeps the latest snapshot of each of the last 7 days, 5 weeks and 12 months. restic groups snapshots by host and paths before applying the policy, so each database keeps its own history. `--prune` then frees the space of data nothing refers to any more.

Before changing the policy, simulate it:

```bash theme={null}
restic forget --dry-run --keep-daily 7 --keep-weekly 5 --keep-monthly 12
```

**Check.** After the first few months, `restic snapshots` does not go back further than the policy you chose.

## Verify the repository: restic check

**Problem.** A backup that was never checked is an assumption.

**Solution.** `restic check` verifies the repository structure. `--read-data-subset=5%` also reads back a random sample of the data on every run: over the nights, the whole repository gets read. Once a month, run a full read:

```bash theme={null}
restic check --read-data
```

On S3 storage billed by download volume, `--read-data` downloads the whole repository: a regular sample covers the rest of the time.

**Check.** The output ends with `no errors were found`.

## Test a restore

This is the step that turns a copy into a backup. Do it when you set things up, then regularly.

### Restore a file or a directory

```bash theme={null}
# What the latest file snapshot holds
restic ls latest --tag files /etc/nginx

# Restore one file next to the original
restic restore latest --tag files \
  --target /tmp/restore \
  --include /etc/nginx/nginx.conf

diff /tmp/restore/etc/nginx/nginx.conf /etc/nginx/nginx.conf
```

Files are restored under `--target` with their full path. For a whole site, replace the file with its directory: `--include /var/www/mysite`. For an older version, use the ID shown by `restic snapshots` instead of `latest`.

### Restore a database into a test database

```bash theme={null}
# MariaDB: import into a temporary database
mariadb -e 'CREATE DATABASE restore_test'
restic dump --tag mariadb latest /mariadb-app.sql | mariadb restore_test
mariadb -e 'SHOW TABLES' restore_test
mariadb -e 'DROP DATABASE restore_test'

# PostgreSQL: same idea
runuser -u postgres -- createdb restore_test
restic dump --tag postgresql latest /postgresql-app.sql | runuser -u postgres -- psql -q -d restore_test
runuser -u postgres -- psql -d restore_test -c '\dt'
runuser -u postgres -- dropdb restore_test
```

The dump has no `CREATE DATABASE` statement: it imports into any empty database, on this server or another one. To replace the production database, import it into the original database after emptying it.

`restic dump` combines `latest` and `--tag`: it picks the latest snapshot with that tag, then reads the given file from it.

### Browse backups with restic mount

```bash theme={null}
mkdir -p /mnt/restic
restic mount /mnt/restic
```

In a second terminal, `ls /mnt/restic` shows the `snapshots`, `tags`, `hosts` and `ids` directories. Copy what you need, then stop `restic mount` with Ctrl-C. Mounting goes through FUSE: on Linux, the `fuse` kernel module must be loaded and the `fusermount` command available. For many files, `restic restore` is significantly faster.

**Check.** The restored file matches the original, and the test database holds the expected tables.

## What to restore, and how

| Situation | Action |
| - | - |
| A configuration file changed by mistake | `restic restore latest --tag files --include <file>` |
| A site deleted or overwritten | `restic restore` with `--include /var/www/<site>`, then copy it back |
| A corrupted table or database | `restic dump` into a test database, check, then import |
| The VPS no longer boots | [Rescue mode](https://blog.onetsolutions.net/en/vps-rescue-mode-repair-server-that-wont-boot.md), or restore the [VPS backup](https://help.onetsolutions.net/vps/backup) |
| The server has to be rebuilt elsewhere | Install restic on the new server, copy `/etc/restic`, then `restic restore` |

## Common pitfalls

**The repository password only exists on the server.** If the VPS is lost, so are the backups. Keep a copy of the password and of `/etc/restic/env` somewhere else.

**`latest` without `--tag`.** The latest snapshot in the repository is often a database one. Always add `--tag`, or pass a snapshot ID.

**A dump sent through a pipe.** `mariadb-dump | restic backup --stdin` creates a snapshot even when the dump fails. Use `--stdin-from-command`.

**The SFTP host key was never accepted.** The timer fails on a question nobody answers. Make the first connection by hand, as root.

**`forget` without `--prune`.** Snapshots leave the list, but space is only freed by `prune`.

**MyISAM tables.** `--single-transaction` only guarantees consistency for InnoDB. Convert those tables, or back them up during a quiet window.

**A lock left by an interrupted run.** `restic unlock` removes locks created by other restic processes. Make sure no backup is running first.

## Going further

* [Free VPS backup trial](https://blog.onetsolutions.net/en/free-vps-backup-trial.md): a full copy of the server every night, in one click
* [Backups](https://help.onetsolutions.net/vps/backup) and [snapshots](https://help.onetsolutions.net/vps/snapshot): the two protections you manage from the console
* [SSH keys](https://help.onetsolutions.net/vps/ssh-keys): key-based login, also useful for the SFTP repository
* [VPS won't boot: rescue mode](https://blog.onetsolutions.net/en/vps-rescue-mode-repair-server-that-wont-boot.md), step by step
* [Disk full on a Linux VPS](https://blog.onetsolutions.net/en/no-space-left-on-device-linux-vps.md): the restic cache and dumps take space too
* [Automatic security updates](https://blog.onetsolutions.net/en/automatic-security-updates-linux-vps.md): the other setting that should run on its own

Have a question about a server hosted with us? [Our team is available](https://help.onetsolutions.net/console/support) from your client area.


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