Restic Backup for a Dedicated Server Script, Repository and Restore

Restic Backup for a Dedicated Server: Script, Repository and Restore

A Restic backup is only as safe as the place it writes to and the script that runs it. Restic encrypts and compresses your data before it leaves the server, but it cannot pick a safe destination for you. This guide covers what a first setup leaves out. It shows where the repository lives, how to script unattended runs and how to stop an attacker wiping the backups.

We tested the commands on Restic 0.16.4, the version in Ubuntu 24.04. Where we could not run something, the text says so. The facts come from the Restic documentation, the rest-server project, Sophos and CISA.

📖 New to the 3-2-1 rule?

Read 3-2-1 Backup for a Dedicated Server: How to Set It Up for the three-copy rule and a first Restic setup.


Why Run a Restic Backup From a Dedicated Server?

Run it there because one program encrypts, compresses and deduplicates your data before it leaves the server. The Restic design document says every file in the repository, except the key files, is encrypted with AES-256 and checked with Poly1305-AES. Cryptographer Filippo Valsorda reviewed the design in 2017 and found it sound. He called it an informal look, not an audit.

What Restic does before data leaves the server

FeatureWhat it means on a server
EncryptionThe storage host only ever sees ciphertext
DeduplicationUnchanged data is stored once, so repeat runs stay small
CompressionRepository format version 2 compresses data, and auto is the default
SnapshotsEach run restores like a full backup
BackendsLocal folders, SFTP, REST server, S3-compatible storage and more

Sources: Restic design document, repository guide, tuning guide.

Table of what Restic does before data leaves the server: encryption, deduplication, compression, snapshots and supported storage backends.

Two limits matter. Restic has no scheduler, so a timer is part of the job. And in 2017 Valsorda noted that file and backup sizes are probably not hidden from whoever controls the storage.

How Much Does a Restic Backup Shrink Your Data?

In our test, a 1.21 GB sample became a 426 MB repository, and seven nightly runs added only 36 MB. The sample was 6,120 Ubuntu system files, not a real web application, so your ratio will differ.

First backup of the 1.21 GB sample, by compression mode

ModeRepository sizeShare of sourceFirst run
Off1.19 GB99%7.6 s
Auto (default)0.43 GB35%12.7 s
Max0.39 GB33%42.2 s

Source: our test on a 2 vCPU machine with Restic 0.16.4. Times depend on your hardware, and the ratio depends on your data.

Table of a first Restic backup of a 1.21 GB sample by compression mode: off 1.19 GB in 7.6 seconds, auto 0.43 GB in 12.7 seconds and max 0.39 GB in 42.2 seconds.

The default mode costs five seconds more than no compression and saves about two thirds of the space. Max saves a little more for over three times the time, so keep auto. Nightly runs took about one second, because Restic does not rescan a file when its path, timestamps, size and inode match the previous snapshot.


Where Should a Restic Repository Live?

On a second server you control, reached over SFTP or a REST server. The repository must not share a failure with the data it protects. The Restic documentation lists local folders, SFTP, a REST server and S3-compatible storage. Delete rights decide how safe each one is.

Restic backends and who can delete the backups

BackendNeeds on the other sideCan the production server delete backups?
Local or mounted diskA second drive or mountYes
SFTPA server with SSHYes, with full file rights
REST serverrest-server runningNo, in append-only mode
S3-compatible storageA bucket and keysOnly if the keys allow it

Sources: Restic repository guide, rest-server README.

Table of Restic backends and whether the production server can delete backups: local disk and SFTP yes, REST server no in append-only mode, S3-compatible storage only if the keys allow it.

With SFTP, an attacker who takes the production key can delete everything the key can write. The rest-server project offers an append-only mode that stops that. For object storage, use credentials without delete permission.

How Much Space Does a Restic Backup Need?

Reserve about 1.25 times the data you protect as a starting point, then measure with restic stats. Restic stores only new chunks, so eight daily snapshots of our sample took 0.46 GB. Eight full copies took 9.77 GB, and eight compressed copies would still take about 3.4 GB.

Eight daily backups: full copies against one Restic repository

Gigabytes stored for 8 backups of the 1.21 GB sample, with 0.5 to 1% of the data changing nightly.

Eight full copies9.77 GB
Eight compressed copies (estimate)3.41 GB
Restic repository, 8 snapshots0.46 GB

Source: our test with Restic 0.16.4. The compressed estimate is eight times the 0.43 GB first backup.

Bar chart of storage for eight daily backups of a 1.21 GB sample: eight full copies take 9.77 GB, eight compressed copies about 3.41 GB and one Restic repository with eight snapshots 0.46 GB.

What each nightly run added to the repository

MiB stored after compression, for seven nightly runs after the first backup.

1st night3.9 MiB
2nd night4.0 MiB
3rd night5.0 MiB
4th night6.7 MiB
5th night5.2 MiB
6th night3.9 MiB
7th night5.3 MiB

Source: our test with Restic 0.16.4. Each run changed or added about 40 files.

Bar chart of the data each of seven nightly Restic runs added to the repository, between 3.9 and 6.7 MiB.

How Do Database Changes Affect Repository Growth?

Nightly growth ranges from under 1 MiB to nearly a full copy, depending on where the rows change. We tested a 47.5 MB database dump (a 260,000-row table) across six backups.

How database changes affect nightly growth

Where the rows changeStored per nightly run
New rows and updates to recent rows0.6 to 0.9 MiB
Updates spread over the whole tableAbout 18 MiB, nearly a full copy

Source: our test on a SQLite stand-in with Restic 0.16.4. Plan for the second row if your updates touch old data.

Table of nightly Restic repository growth for a database dump: 0.6 to 0.9 MiB when new and recent rows change, and about 18 MiB when updates are spread over the whole table.

📖 Mirroring the backup drives?

Read RAID0 vs RAID1: Data Loss Odds, Restore Time and Cost per TB before you choose how to protect the backup server’s own drives.


How Do You Build a Restic Backup Server on a Dedicated Server?

Order a server with the drives you need, install Debian or Ubuntu and add a user that can only use SFTP. The Swify builder offers drives up to 26 TB and RAID 1. Every plan has full root access and an unmetered 1 Gbps port.

The commands below create the user, restrict its key and initialise an SFTP repository. We tested these steps on Ubuntu 24.04. SFTP is the simple start, and the REST server in the attackers section adds delete protection. Pick one.

# On the backup server, as root
adduser --disabled-password --gecos "" bk
install -d -m 700 -o bk -g bk /home/bk/.ssh
install -d -o bk -g bk /srv/restic
# Paste the production server's public key after the options: SFTP only, no shell
echo 'restrict,command="internal-sftp" ssh-ed25519 AAAA...paste-the-key production' > /home/bk/.ssh/authorized_keys
chown bk:bk /home/bk/.ssh/authorized_keys && chmod 600 /home/bk/.ssh/authorized_keys

# On the production server, as root
ssh-keygen -t ed25519 -N "" -f /root/.ssh/id_backup
cat /root/.ssh/id_backup.pub
cat >> /root/.ssh/config <<'EOF'
Host backup
  HostName backup.example.net
  User bk
  IdentityFile /root/.ssh/id_backup
  StrictHostKeyChecking accept-new
EOF
openssl rand -base64 32 > /root/.restic-pass && chmod 600 /root/.restic-pass
export RESTIC_PASSWORD_FILE=/root/.restic-pass
restic -r sftp:backup:/srv/restic/prod init

Keep a copy of the repository password away from both servers. The Restic documentation states that losing it means the data is irrecoverably lost.

How Do You Lock Down the Backup Server?

Give the backup key only the access it needs, which is the principle of least privilege. In our test, a shell request with the restricted key was refused, and Restic still worked. Add SSH key authentication, disable password logins and follow our SSH security guide.

A restricted key can still delete files over SFTP. For delete protection, use the REST server setup in the section on attackers below.

Build your backup server

Pick an 8 TB HDD from €10 a month, install Debian or Ubuntu and keep full root access. Every plan includes an unmetered 1 Gbps port.

Build your backup server

How Do You Script a Restic Backup That Runs Unattended?

Use one script that makes a database dump, backs up and checks the exit code. It pings a monitor only when everything worked. Ours takes a lock, so a slow run never overlaps the next one.

#!/bin/bash
# /usr/local/bin/restic-backup.sh
set -u -o pipefail

# One run at a time: skip this run if the previous one is still active
exec 9>/run/restic-backup.lock
flock -n 9 || { echo "Previous run still active, skipping"; exit 0; }

# For the REST server, use the rest: URL and export RESTIC_REST_USERNAME and RESTIC_REST_PASSWORD here
export RESTIC_REPOSITORY="sftp:backup:/srv/restic/prod"
export RESTIC_PASSWORD_FILE=/root/.restic-pass
FAIL=0

# 1. Dump to a temporary file, then rename, so a failed dump never replaces a good one
mkdir -p /srv/dumps
if sudo -u postgres pg_dump appdb > /srv/dumps/appdb.sql.new; then
  mv /srv/dumps/appdb.sql.new /srv/dumps/appdb.sql
else
  echo "Database dump failed"; FAIL=1
fi

# 2. Back up. Exit code 3 means some files were unreadable (a snapshot still exists)
restic backup --exclude-caches --exclude-file=/etc/restic-excludes.txt /etc /var/www /srv/dumps
RC=$?
[ "$RC" -eq 0 ] || { echo "restic backup exit code $RC"; FAIL=1; }

# 3. Tell the monitor only when everything worked
[ "$FAIL" -eq 0 ] && curl -fsS -m 10 --retry 3 https://monitor.example.net/ping/restic-prod > /dev/null
exit "$FAIL"

We ran this script with SQLite standing in for pg_dump, and a failing dump left the last good dump untouched. We did not run pg_dump itself. Heartbeat monitoring alerts you when the ping goes missing, which catches a dead server, a broken timer and a failed run.

What Should a Restic Backup Exclude?

Exclude anything the server can rebuild or that changes on every run. Our test used a two-line exclude file, and the Restic backup guide documents the other flags.

What to leave out of a Restic backup

Pattern or flagWhy leave it out
/var/www/cacheThe application rebuilds it, so it only adds churn
*.tmpTemporary files that change on every run
--exclude-cachesSkips folders marked with a CACHEDIR.TAG file
--one-file-systemStays on one filesystem, so mounts and virtual folders are skipped

Source: our test exclude file and the Restic backup guide. We did not test --one-file-system.

Table of what to leave out of a Restic backup: application cache, temporary files, folders marked with CACHEDIR.TAG and mounted or virtual filesystems.

📖 Backing up Nextcloud?

Read Nextcloud Server: What It Needs and What to Run It On to see which data, database and config a Nextcloud backup must contain.

What Do Restic Exit Codes Mean?

Zero means success, one means a fatal error, and three means a snapshot exists but some files could not be read. The backup manual lists the codes. The scripting guide warns that new codes will be added, so treat any unknown code as a failure.

Restic backup exit codes

CodeMeaningNotes
0SuccessSnapshot saved
1Fatal errorNo snapshot created
3Some files unreadableSnapshot saved, confirmed in our test
10, 11, 12Repository missing, locked, wrong passwordRestic 0.17 and later

Sources: Restic backup manual, Restic scripting guide, and our test on Restic 0.16.4.

Table of Restic backup exit codes: 0 success, 1 fatal error, 3 some files unreadable with a snapshot saved, and 10 to 12 for a missing or locked repository or a wrong password in Restic 0.17 and later.

Ubuntu 24.04 ships 0.16.4, which returned 1 for a missing repository in our test. The script treats every non-zero code as a failure, so a code 3 run sends no ping and you can inspect the snapshot.

Should You Stream a Database Dump Into Restic?

Only if you check the dump’s exit status, because a failed dump still becomes a snapshot. In our test, a dump that died after two lines was saved and restored as a normal file. The pipeline returned status 1 under pipefail.

The script above avoids that by writing a file first. Skip gzip too, because Restic compresses for you. In our test, the plain dump stayed at 0.6 to 0.9 MiB a night, while the gzip dump grew from 0.57 to 1.23 MiB.

Cron or a Systemd Timer for a Restic Backup?

Use a systemd timer, because Persistent=true runs a missed backup after downtime, which plain cron skips. The systemd manual documents that catch-up, and the Arch wiki compares timers with cron. RandomizedDelaySec spreads start times when several servers share one target.

# /etc/systemd/system/restic-backup.service
[Unit]
Description=Restic backup
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/restic-backup.sh
Nice=10
IOSchedulingClass=idle

# /etc/systemd/system/restic-backup.timer
[Unit]
Description=Nightly Restic backup

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

[Install]
WantedBy=timers.target

# Then enable it
systemctl daemon-reload
systemctl enable --now restic-backup.timer
systemctl list-timers restic-backup.timer

The systemd-analyze tool accepted both unit files, but we did not run the service itself.


How Do You Stop an Attacker Deleting Your Backups?

Give the production server a way to add backups but not delete them. Run retention only from the backup side. Ransomware goes after backups. In the Sophos State of Ransomware 2024, 94% of hit organisations said attackers tried to compromise their backups, and 57% of those attempts worked. Where backups were compromised, 67% paid the ransom, against 36% where they were not.

The REST server has an append-only mode that lets clients create new backups but blocks deleting or modifying existing ones. That is a practical form of immutable backup. We could not install rest-server in our sandbox, so we did not run this block.

# On the backup server (apache2-utils provides htpasswd)
htpasswd -B -c /srv/restic/.htpasswd prod
rest-server --path /srv/restic --htpasswd-file /srv/restic/.htpasswd \
  --append-only --private-repos --tls \
  --tls-cert /etc/restic/tls.crt --tls-key /etc/restic/tls.key

# On the production server
export RESTIC_REPOSITORY=rest:https://backup.example.net:8000/prod/
export RESTIC_REST_USERNAME=prod
export RESTIC_REST_PASSWORD=your-rest-password
restic init

The README warns that without TLS, basic authentication sends usernames and passwords in cleartext. Always pass --tls and use a signed certificate. Append-only is not offline, though. The CISA #StopRansomware Guide advises offline, encrypted backups that you test regularly, so keep one air-gapped copy that no online credential can reach.

How Do You Run Retention on an Append-Only Repository?

Run forget and prune from a separate, well-secured machine, as the Restic documentation advises, because they need delete access. An attacker who adds fake snapshots could push real ones out of a keep policy. Add --keep-within to protect everything inside the chosen window.

Who can do what in the repository

RoleAdd snapshotsForget and prune
Production serverYesNo
Backup server adminYesYes, on the local folder
Attacker with the production keyOnly fake onesNo

Based on the rest-server README and the Restic forget guide.

Table of who can do what in the Restic repository: the production server can add snapshots but not forget or prune, the backup server admin can do both on the local folder, and an attacker with the production key can only add fake snapshots.

# Weekly, on the backup server, against the local repository folder
export RESTIC_PASSWORD_FILE=/root/.restic-pass
restic -r /srv/restic/prod forget --keep-within 30d --keep-daily 14 --keep-weekly 8 --keep-monthly 12 --dry-run
restic -r /srv/restic/prod forget --keep-within 30d --keep-daily 14 --keep-weekly 8 --keep-monthly 12 --prune

We tested the dry run on a local repository. It reuses the retention policy from our 3-2-1 guide and adds the window. Append-only is enforced by the REST server, so a command run on the repository folder is not blocked. That machine needs the repository password, so protect it most.

📖 Hardening the production server?

Read Dedicated Server Security Checklist: How to Harden Your Server After Setup for the steps that keep an attacker away from the backup key.

How Do You Prove a Backup Works Before You Need It?

Restore into an empty folder with --verify and write down how long it took. That time is your recovery time objective. Nightly runs also mean you can lose up to a day of data. Our test repository held eight snapshots on local disk.

What we measured on the test repository

CheckResult
check --read-data-subset=5%No errors, 1.0 s
restore latest --verify (1.15 GiB)4.7 s
copy one snapshot to a second repository13 s

Source: our test with Restic 0.16.4 on local disks. Over a network, the link sets the pace.

Table of Restic checks on the test repository: a 5% data check in 1.0 seconds, a verified restore of 1.15 GiB in 4.7 seconds and copying one snapshot to a second repository in 13 seconds.

Those times are local, so your numbers will be slower across a network. A second repository also gives you the third copy of the 3-2-1 backup rule. Run the check routine from our 3-2-1 guide too.

# Restore a sample with a content check
restic -r sftp:backup:/srv/restic/prod restore latest --target /restore-test --include /etc --verify

# Copy the latest snapshot to a second repository (the third copy)
export RESTIC_FROM_PASSWORD_FILE=/root/.restic-pass
restic -r /srv/second/repo init --from-repo /srv/restic/prod --copy-chunker-params
restic -r /srv/second/repo copy --from-repo /srv/restic/prod latest

What Does a Restic Backup Server Cost?

The CPU is the biggest line on a small backup server on the Swify builder. The smallest one costs €90 a month, against €10 for 32 GB of RAM and €10 for an 8 TB drive. These prices were correct on 25 November 2026, and the builder shows the total before you order.

Parts of a small backup server

PartOptionMonthly price
CPU1x Intel Xeon Gold 5215€90
RAM32 GB€10
Drive8 TB HDD€10
Network1 Gbps, unmetered€0
IP address1 included€0

Source: Swify builder, 25 November 2026, before VAT.

Table of the parts of a small backup server on the Swify builder: a Xeon Gold 5215 CPU at 90 euros, 32 GB of RAM at 10 euros, an 8 TB HDD at 10 euros, a 1 Gbps unmetered port and one IP address included.

Our 3-2-1 guide compares drive prices per TB. Here the question is how much server one repository needs.

Which Drive Fits Your Data Size?

Pick the smallest HDD that holds 1.25 times your data. That multiplier is our planning assumption, not a Restic figure. Two drives cost twice as much, so a RAID 1 pair doubles the drive price and survives one drive failing.

Smallest Swify HDD for a Restic repository

Data you protectPlanning sizeSmallest HDD that fitsDrive per month
1 TB1.25 TB8 TB€10
4 TB5 TB8 TB€10
8 TB10 TB14 TB€30
12 TB15 TB18 TB€35
20 TB25 TB26 TB€50

Source: Swify builder drive prices, 25 November 2026. Planning size is data multiplied by 1.25.

Table of the smallest Swify HDD for a Restic repository by data size, with a 25 percent margin: 8 TB at 10 euros for up to 5 TB, 14 TB at 30 euros, 18 TB at 35 euros and 26 TB at 50 euros.

Swify has no egress fees, so restoring a large repository adds no transfer bill. For a few gigabytes, a small object storage bucket may still cost less. A second server pays off when you protect terabytes.


Restic Backup: The Short Version

Put the repository on a separate server and give the production key append-only access. Script and monitor the run, restore a sample every month and keep one copy offline. Our test shows the first run costs the most, and later runs add only what changed.

Put production and backups on separate servers

Compare Swify plans for the production workload, then add a second server with the drives your backups need.

Compare Swify plans


Frequently Asked Questions

Is a Restic backup good enough for a production server?

Yes, when the repository sits on separate storage and you test restores. Restic encrypts, compresses and deduplicates data, and Filippo Valsorda’s 2017 review found the design sound enough that he would use it for his own backups. On an unmanaged server, you own the schedule and the monitoring.

Read Managed vs Unmanaged Hosting Explained to see what an unmanaged server leaves to you.


Does a Restic backup encrypt your data?

Yes. Restic encrypts everything in the repository except the key files, using AES-256 in counter mode with Poly1305-AES. If you lose the repository password, the data cannot be recovered, so keep a copy away from the server.

Read GDPR Data Protection Rules: What They Mean for Your Server to see why encrypted backups matter for personal data.


How do you copy a Restic backup to a second location?

Create a second repository with restic init –from-repo and –copy-chunker-params, then run restic copy. In our test, copying the latest snapshot of 1.15 GiB between two local repositories took 13 seconds. Use a different server or provider for the second copy.

Read HDD vs SSD for Servers to pick cheap drives for the second copy.


Does a Restic backup slow down a server?

It can on the first run, because Restic reads and compresses every file. Later runs are light: our first run of 1.21 GB took 12.7 seconds, and nightly runs took about one second. Run it at night with Nice and idle I/O priority.

Read What Is Disk I/O and Why It Becomes a Bottleneck to spot disk pressure.


How do you know a Restic backup failed?

Alert on a missing success ping, and treat any non-zero exit code as a failure. Exit code 3 means a snapshot exists but some files were unreadable, so you have a backup with gaps, not no backup.

Read Best Tools to Monitor Dedicated Server Performance to set up the monitor.


How do you restore a server from a Restic backup?

Install Restic on the new server, point it at the repository with the same password and run restic restore latest into a staging folder. Check the files, load the database dump, then copy the rest into place. For a few files, restic mount lets you browse snapshots first.

Read Dedicated Server Migration Checklist: How to Move Without Downtime to plan the move.