restic with sops and rypt
restic encrypts a backup repository on your machine, under a key
that the repository's password opens. This page keeps that password and the repository's
location in a sops file protected by a rypt key, and runs restic with
them. Each time sops opens the file costs one rypt decrypt. Your values do not reach
rypt, but the key that opens them does, so this is not zero-knowledge.
What you need
Section titled "What you need"This page builds on the sops setup: age-plugin-rypt on your PATH,
the rypt identity in sops's keys.txt, a recovery key in backup.key, and a
.sops.yaml whose rule, \.enc\.(env|ya?ml|json)$, already matches restic.enc.env.
Run the commands here in the directory that holds .sops.yaml.
You also need restic 0.17.1 or later, which exits with the codes this page lists. The output on this page is from restic 0.18.0 and sops 3.13.3.
The API token it needs
Section titled "The API token it needs"age-plugin-rypt calls decrypt each time sops opens restic.enc.env, and encrypt each
time sops gives the file a new data key. A backup host only opens it, so give its API
token decrypt alone, hold it to the one key, and hold it to a firewall that lists the
host's address: the API token in the examples has this
shape, and
setting it up in the dashboard
shows how. The machine where you create the file, change the password or run
sops rotate -i needs encrypt and decrypt, which the quickstart's API token has.
Each machine reads its API token from RYPT_TOKEN.
Create the secrets file
Section titled "Create the secrets file"This makes a random password and encrypts it, with the repository's location, straight
into restic.enc.env:
printf 'RESTIC_REPOSITORY=%s\nRESTIC_PASSWORD=%s\n' "$HOME/restic-repo" "$(openssl rand -hex 32)" \
| sops encrypt --filename-override restic.enc.env --output restic.enc.env
It prints nothing and costs 1 encrypt. The password goes through a pipe and never
touches the disk. For a remote repository, such as S3 or a REST server, open the file
with sops edit restic.enc.env and set RESTIC_REPOSITORY to its repository string,
with its credentials beside it (for S3, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).
restic's
repository docs
give the string and variables for each backend.
Run restic through sops exec-env
Section titled "Run restic through sops exec-env"sops exec-env decrypts restic.enc.env, adds its values to the environment, and runs
your command with /bin/sh -c. Create the repository:
sops exec-env restic.enc.env 'restic init'
created restic repository 96f0a6495d at /home/you/restic-repo
Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.
Back up a directory:
sops exec-env restic.enc.env 'restic backup ~/Documents'
List the snapshots:
sops exec-env restic.enc.env 'restic snapshots'
ID Time Host Tags Paths Size
----------------------------------------------------------------------------
1b4fd842 2026-11-05 16:40:43 laptop /home/you/Documents 97.662 KiB
----------------------------------------------------------------------------
1 snapshots
Restore the latest snapshot:
sops exec-env restic.enc.env 'restic restore latest --target ~/restore'
restic puts each file at its full path under the target, so ~/Documents comes back
under ~/restore/home/you/Documents:
diff -r ~/Documents ~/restore$HOME/Documents && echo same
same
Each sops exec-env is 1 operation, however many restic commands it runs, so chain them
with &&:
sops exec-env restic.enc.env 'restic backup ~/Documents && restic check'
Keep the command in single quotes, so that the shell sops starts, with the file's values
set, expands ~ and any $ in it, and your own shell does not. sops exits with
restic's exit code, and prints exit status and the code when it is not 0. A script can
tell 3 (some source files could not be read), 10 (there is no repository at that
location), 11 (the repository could not be locked) and 12 (the password is wrong) apart.
The password, any backend credentials in the file and RYPT_TOKEN are in restic's
environment for the whole run. On Linux, any process of the same user, and root, can
read them from /proc/<pid>/environ. To keep the API token out, start restic with
env -u RYPT_TOKEN:
sops exec-env restic.enc.env 'env -u RYPT_TOKEN restic snapshots'
sops exec-env --pristine keeps your whole environment out, RYPT_TOKEN with it, but
restic then has no HOME: it prints unable to open cache and runs without its cache.
Keep the password out of restic's environment
Section titled "Keep the password out of restic's environment"restic can run a command to get its password instead. In the directory that holds
restic.enc.env, set the repository and the command, and run restic itself:
export RESTIC_REPOSITORY="$HOME/restic-repo"
export RESTIC_PASSWORD_COMMAND="sops decrypt --extract '[\"RESTIC_PASSWORD\"]' $PWD/restic.enc.env"
restic snapshots
ID Time Host Tags Paths Size
----------------------------------------------------------------------------
1b4fd842 2026-11-05 16:40:43 laptop /home/you/Documents 97.662 KiB
f3060d42 2026-11-05 16:40:51 laptop /home/you/Documents 97.662 KiB
----------------------------------------------------------------------------
2 snapshots
restic runs the command without a shell, so $VAR, ~, pipes and globs do not work in
it. Your shell puts the absolute path in place of $PWD when it sets the variable, so
restic finds the file from any directory, and because the path is not quoted in the
command, it must not contain a space. restic runs the command once for each restic
command, at 1 operation each, but not for version, help, cache, generate,
options or self-update: restic version costs 0.
restic reads the password from the command's output, so the password is never in its
environment. RYPT_TOKEN is, because restic passes its environment on to sops. The
command gives restic only the password: a remote repository's credentials have to reach
restic another way, and reading them from a sops file costs 1 more operation.
The two exports last until you close the shell. While RESTIC_PASSWORD_COMMAND is
set, restic prefers it to the RESTIC_PASSWORD that sops exec-env gives it, so under
each sops exec-env on the rest of this page restic would also run sops once for every
restic command, at 1 more operation each, and with env -u RYPT_TOKEN it would fail with
Fatal: Resolving password failed: exit status 128. Unset both before you go on, with
unset RESTIC_REPOSITORY RESTIC_PASSWORD_COMMAND.
Which to use
Section titled "Which to use"sops exec-env |
RESTIC_PASSWORD_COMMAND |
|
|---|---|---|
| Operations | 1 per run, however many restic commands it chains | 1 per restic command, 0 for restic version |
| The password | In restic's environment | Read from the command's output, never in restic's environment |
| Backend credentials | From the same file | Need another route, at 1 more operation |
RYPT_TOKEN in restic's environment |
Not with env -u RYPT_TOKEN |
Always, because restic runs sops |
| The command | Run by /bin/sh -c, so ~, $VAR and && work |
Run without a shell: no $VAR, ~, pipes or globs |
Use sops exec-env with env -u RYPT_TOKEN. Use the password command instead when other
processes run on the machine as the same user that you do not trust with the password.
Reading restic's environment then gives them RYPT_TOKEN but not the password. Short of
reading restic's memory, which root can always do and a same-user process can where the
kernel allows it, they have to decrypt the file through rypt, under the same API token as
your backups, which the key's audit log records and
revoking the API token stops.
What each command costs
Section titled "What each command costs"Each time sops opens restic.enc.env through rypt costs 1 decrypt, whatever the size
of the backup: restic encrypts the backup itself. rypt counts an operation only when it
completes (operations).
| Command | Operations |
|---|---|
Creating restic.enc.env with sops encrypt |
1 encrypt |
sops exec-env restic.enc.env '...', however many restic commands it chains |
1 decrypt |
A restic command run with RESTIC_PASSWORD_COMMAND |
1 decrypt, or 0 for version, help, cache, generate, options and self-update |
sops edit or sops set on restic.enc.env |
1 decrypt |
sops rotate -i restic.enc.env |
2: 1 decrypt and 1 encrypt |
| Changing the password, as in shut out a machine | 4: 3 decrypt and 1 encrypt |
backup.key with RYPT_TOKEN unset, as in restore without rypt |
0: no request reaches rypt |
| A request rypt refuses, or that does not complete | 0 |
When something fails
Section titled "When something fails"| Situation | restic through rypt | With backup.key |
|---|---|---|
| rypt cannot be reached | Fails | Works |
RYPT_TOKEN is not set |
Fails | Works |
The API token is wrong or revoked, or lacks decrypt |
Fails | Works |
| The machine is outside the API token's firewall | Fails | Works |
| A Free key is at its monthly cap | Works | Works |
| A paid key's billing is not in good standing | Works | Works |
| The key is pending deletion | Fails | Works |
| The key is destroyed | Fails | Works |
| The file's key version is destroyed | Fails | Works |
In the rows that fail, sops cannot open restic.enc.env, so restic gets no password:
sops exec-env exits 128 without running the command, and with the password command
restic exits 1 before it opens the repository. Here echo restic would run prints
nothing, because sops stops before it:
env -u RYPT_TOKEN sops exec-env restic.enc.env 'echo restic would run; restic snapshots'; echo "exit $?"
Failed to get the data key required to decrypt the SOPS file.
...
age1rypt1pu0z60zttf5h3puk5k6v85hp7qcmk36k: FAILED
- | failed to create reader for decrypting sops data key with
| age: rypt plugin: RYPT_TOKEN is not set.
...
exit 128
At a Free key's monthly cap, rypt refuses to
encrypt until 00:00 UTC on the 1st, and goes on decrypting. Backups and restores keep
working; only sops encrypt and sops rotate -i wait. A paid key whose billing is not
in good standing refuses to encrypt and goes on decrypting too
(billing refusals).
A deleted key refuses every operation for 7 days, during which Restore on its page in
the dashboard brings it back. After those 7 days, or at once with Delete now, the key
is destroyed, and whatever was encrypted only to it can never be decrypted again. See
one Free key per tenant and
errors. restic.enc.env is also encrypted to backup.key,
so it still opens with that.
Restore without rypt
Section titled "Restore without rypt"Copy backup.key into this directory from offline storage, and give it to sops.
env -u RYPT_TOKEN runs sops without the API token, so the file opens with backup.key
alone:
env -u RYPT_TOKEN SOPS_AGE_KEY_FILE="$PWD/backup.key" sops exec-env restic.enc.env 'restic restore latest --target ~/restore2'
restoring snapshot f3060d42 of [/home/you/Documents] at 2026-11-05 16:40:51.550236751 +0000 UTC by you@laptop to /home/you/restore2
Summary: Restored 10 files/dirs (97.662 KiB) in 0:00
This costs 0 operations and leaves no row in the audit log. Then move backup.key off
the machine again.
While rypt still answers, give restic a second password of its own as well, which opens the repository with neither sops nor rypt. Add one, and list the repository's keys:
sops exec-env restic.enc.env 'restic key add && restic key list'
restic asks for the new password with enter new password: and enter password again:,
prints saved new key with ID and the new key's id, and lists both keys, the one in use
marked *. This costs 1 operation. Store the second password offline too: like the
recovery key, it reaches the backups without rypt.
Shut out a machine that has read the password
Section titled "Shut out a machine that has read the password"First revoke its API token, so that it
cannot open restic.enc.env again. Then, on a machine whose API token has encrypt and
decrypt, give the repository a new password and store it in the file:
NEW_PASSWORD=$(openssl rand -hex 32)
printf '%s' "$NEW_PASSWORD" | sops exec-env restic.enc.env 'restic key passwd --new-password-file /dev/stdin'
printf '"%s"' "$NEW_PASSWORD" | sops set --value-stdin restic.enc.env '["RESTIC_PASSWORD"]'
unset NEW_PASSWORD
sops rotate -i restic.enc.env
saved new key as <Key of you@laptop, created on 2026-11-05 16:41:14.610315732 +0000 UTC m=+1.579151198>
restic key passwd opens the repository with the old password from the file and
replaces its key with one for the new password, which it reads from standard input.
sops set writes the new password into the file; --value-stdin takes a JSON value,
hence the double quotes around it. sops rotate -i then gives the file a new data key,
since the machine you shut out may have kept the old one. The new password stays in a
shell variable, not on disk, and printf is built into the shell, so it shows in no
process list. This costs 4 operations.
Run the lines one at a time, and go on only when the line before succeeded. If
restic key passwd fails, stop: sops set would store a password the repository does
not know. Once it has succeeded, the old password no longer opens the repository, and
$NEW_PASSWORD is the only copy of the new one until sops set has stored it, so do not
unset it before then.
Afterwards restic key list shows a new key in place of the old one, and still lists a
second password from restic key add, which keeps working.
A new password does not change the repository's master key, which a machine that has
opened the repository may have kept: with it, the machine can still read whatever
storage it can still reach. So if the file holds backend credentials, change them at the
backend as well, and store the new ones with sops edit. Only restic copy to a new
repository changes the master key.
After a rotation
Section titled "After a rotation"On a Free key every rotation retires the version before it; a retired version keeps decrypting for 7 days and is then destroyed (Free keys). A paid key keeps every version until you retire one (versions per tier).
The repository needs nothing: rypt protects only restic.enc.env. Within those 7 days,
run sops rotate -i restic.enc.env on a machine whose API token has encrypt and
decrypt, which costs 2 operations, as after a rotation
on the sops page describes. If the version is destroyed first, the file opens only with
backup.key: run sops rotate -i restic.enc.env with SOPS_AGE_KEY_FILE naming
backup.key, which puts its new data key under the primary version, and then move
backup.key off the machine again.
Errors
Section titled "Errors"| Message | Cause |
|---|---|
Failed to get the data key required to decrypt the SOPS file. |
sops could not open restic.enc.env. sops exec-env then exits 128 without running restic; with the password command, the next row follows. Under the rypt recipient's FAILED line, the text after rypt plugin: says why, such as RYPT_TOKEN is not set, rypt: connection failed or rypt: unauthorized. age-plugin-rypt's errors give the cause of each. |
Fatal: Resolving password failed: exit status 128 |
The password command failed, for the same reasons; sops's own error comes before this line. restic exits 1. Fix the cause that sops gives under the rypt recipient's FAILED line, as in the row above. |
Fatal: Resolving password failed: Fatal: Password file and command are mutually exclusive options |
RESTIC_PASSWORD_FILE is set as well as RESTIC_PASSWORD_COMMAND. Unset RESTIC_PASSWORD_FILE. restic exits 1. |
unable to open cache: unable to locate cache directory: neither $XDG_CACHE_HOME nor $HOME are defined |
sops exec-env --pristine gave restic no HOME, so it runs without its cache. Use env -u RYPT_TOKEN instead, as in run restic through sops exec-env. |