restic commands cheat sheet

This cheat sheet brings together the commands of restic, a backup tool that saves each backup as an encrypted, deduplicated snapshot (a point-in-time image of your files): setting up a repository, backups and excludes, database backups, browsing backups, restores, retention policies, repository checks, password management, and automatic scheduling. You can search for a command, filter by topic, and copy it with one click.

Installation and repository setup

  • sudo apt install restic

    Installs restic on Debian and Ubuntu from the distribution's repository.

  • sudo dnf install epel-release && sudo dnf install restic

    Installs restic on AlmaLinux, Rocky Linux, and similar RHEL-family distributions from the EPEL repository. On Fedora, sudo dnf install restic is enough.

  • restic version

    Shows the installed version. When several servers back up to the same repository, it's best to run a recent version on all of them.

  • restic -r /srv/restic-repo init

    Creates a new backup repository in a local directory or on an additional disk and asks for a password. All the data in the repository is encrypted with this password.

    Example: created restic repository 3a6f9e1c2d at /srv/restic-repo

  • restic -r sftp:backup@backup.example.com:/srv/restic-repo init

    Creates a repository on another server over SFTP, on top of a regular SSH connection. Automatic backups need an SSH key login that doesn't prompt for a password. A path without a leading slash is relative to the user's home directory on the remote server.

  • restic -r sftp:backup-server:/srv/restic-repo init

    When the server has a host alias in ~/.ssh/config, with its address, user, port, and key, restic uses it. This way, an SSH port other than 22 is also set in one place.

  • restic -r rest:https://user:pass@backup.example.com:8000/repo/ init

    Creates a repository on restic's REST server (rest-server), which accepts backups over HTTPS. You can also pass the user name and password in the RESTIC_REST_USERNAME and RESTIC_REST_PASSWORD environment variables, so they don't appear in the command.

  • restic -r s3:https://s3.example.com/bucket_name init

    Creates a repository in S3-compatible object storage. The credentials are read from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, and you set the region in AWS_DEFAULT_REGION when the service requires one.

  • restic -r rclone:remote:restic-repo init

    Creates a repository through rclone, on any destination rclone can connect to. Set up the remote destination first with rclone config.

  • openssl rand -base64 32 > /root/.restic-pass && chmod 600 /root/.restic-pass

    Writes a strong random password to a file and limits access to the file to its owner. Keep a copy of the password outside the server too, because without it the repository can't be opened.

  • restic -r /srv/restic-repo --password-file /root/.restic-pass snapshots

    Reads the password from a file instead of prompting for it. The --password-file option, or the RESTIC_PASSWORD_FILE environment variable, is suited to scripts that run unattended.

  • restic cat config

    Checks that the repository exists and that the password is correct. Useful as a check at the start of a script: when the repository doesn't exist, the exit code is 10.

Backing up files

  • restic backup /var/www /etc

    Backs up the directories and creates a snapshot, an image of the files at the moment of the backup. On the next backup, only files that changed are read again and only new data is stored in the repository, so even a daily backup takes up little space. From here on, the examples set the repository and password in environment variables.

  • restic backup --exclude='*.log' --exclude='/var/www/*/cache' /var/www

    Excludes files and directories from the backup by pattern. You can repeat --exclude as many times as you need, and the --iexclude option works the same way but ignores case.

  • restic backup --exclude-file=/etc/restic/excludes.txt /var/www /home

    Reads the exclude patterns from a file, one pattern per line. Handy when the list is long or shared by several servers.

  • restic backup --exclude-caches --exclude-larger-than 2G /home

    Skips cache directories marked with a CACHEDIR.TAG file and files larger than 2 GB, such as video files or old archives.

  • restic backup --one-file-system /

    Backs up only the root file system and doesn't cross into other disks and file systems mounted below it, such as /proc, /sys, or a network drive.

  • restic backup --tag daily --tag web /var/www

    Adds tags to the snapshot. Tags let you filter backups, restore from the right one, and set a different retention policy for each type of backup.

  • restic backup --host web1 /var/www

    Sets the host name saved in the snapshot. The default is the computer's name; change it when the name has changed, for example after moving to a new server, so the backups stay in the same group.

  • restic backup --dry-run -vv /var/www

    A dry run: shows which files would be backed up, without writing to the repository. Useful for testing new exclude patterns.

  • restic backup --skip-if-unchanged /etc

    Doesn't create a new snapshot when nothing has changed since the previous backup. Suited to directories that rarely change, such as system configuration.

  • restic backup --limit-upload 5000 /var/www

    Limits the upload rate to about 5,000 KiB per second, so a backup to a remote server doesn't overload the connection of a live server. The --limit-download option limits downloads the same way during a restore.

Backing up databases

  • restic backup --tag mysql --stdin-filename example_db.sql --stdin-from-command -- mysqldump --single-transaction example_db

    Runs mysqldump and saves its output directly in the repository as the file example_db.sql, without a temporary file on disk. If mysqldump fails, restic doesn't create a snapshot and exits with an error. Keep the login details in the ~/.my.cnf file, not in the command.

  • mysqldump --single-transaction example_db | restic backup --stdin --stdin-filename example_db.sql

    The same backup through a pipe. Caution: this way, restic can't tell whether mysqldump succeeded, and a failure may produce a partial or empty backup with no error. That's why the --stdin-from-command option is preferred. If you do use a pipe, enable set -o pipefail in the script and check the exit code.

  • restic backup --tag postgres --stdin-filename example_db.dump --stdin-from-command -- pg_dump -U postgres -Fc example_db

    Backs up a PostgreSQL database in pg_dump's custom format, which you restore with pg_restore. Here too, a pg_dump failure cancels the snapshot.

  • restic snapshots --tag mysql

    Shows only the database backups, by the tag set during the backup.

  • restic dump --tag mysql latest example_db.sql | mysql example_db

    Caution: extracts the backup file from the latest snapshot tagged mysql and loads it straight into the database, replacing its current contents. For a test only, load it into a separate empty database first.

  • restic dump --tag postgres latest example_db.dump > example_db.dump

    Extracts the PostgreSQL backup file to a local file, which you then restore with pg_restore.

Browsing and searching backups

  • restic snapshots

    Lists all the backups in the repository: ID, date and time, host name, tags, and the paths backed up.

    Example: ID Time Host Tags Paths 4f2a9c1e 2026-11-29 02:30:05 web1 daily /var/www

  • restic snapshots --host web1 --tag daily

    Filters the list by host name and tag. You can also add --path=/var/www to see only backups of a specific path.

  • restic snapshots --latest 1

    Shows only the latest backup for each host and path. A quick way to confirm that last night's backup was actually created.

  • restic ls latest /var/www/site

    Lists the files in the latest snapshot, only under the given path. Adding --long also shows size, permissions, and date.

  • restic find --snapshot latest 'nginx.conf'

    Searches for a file by name or pattern in the latest snapshot. Without --snapshot, the search covers all the backups, which shows you from which date the file exists.

  • restic diff 4f2a9c1e 7b3d0a52

    Compares two backups: + a file that was added, - a file that was deleted, M content that changed. Useful for checking what changed on a site between two dates.

    Example: M /var/www/site/index.php + /var/www/site/new-page.php

  • restic stats --mode raw-data

    Shows how much space the repository actually takes up, after deduplication and compression. Without --mode, it shows the size you'd get from a restore.

Restoring

  • restic restore latest --target /tmp/restore

    Restores the latest snapshot to a separate directory. This way, you can compare and copy what you need without overwriting existing files.

  • restic restore latest --host web1 --path /var/www --target /tmp/restore

    Restores the latest backup of a specific host and path, when the repository holds backups from several servers or directories.

  • restic restore 4f2a9c1e --target /tmp/restore --include /var/www/site/uploads

    Restores only part of the snapshot, by path. The --exclude option excludes paths the same way.

  • restic restore 4f2a9c1e:/var/www/site --target /var/www/site-restored

    Restores a directory from the snapshot directly as a directory in the target, without the full original path above it.

  • restic restore latest --target / --include /etc/nginx --dry-run -vv

    A dry run of a restore to the original location: shows which files would be written, without changing anything. Run it before every restore directly onto the system.

  • restic restore latest --target / --include /etc/nginx --overwrite if-changed

    Caution: restores to the original location and overwrites existing files whose content differs from the backup. The default is always, and never leaves every existing file as it is.

  • restic dump latest /etc/nginx/nginx.conf

    Prints a single file from the backup to the screen, without restoring. With a redirect to a file, such as > nginx.conf.bak, the file is saved aside.

  • restic dump -a zip latest /var/www/site > site.zip

    Saves a directory from the backup as a zip file. Without -a, a tar file is created. Handy for moving it to another computer.

  • mkdir -p /mnt/restic && restic mount /mnt/restic

    Presents the repository as a regular directory, with a directory for each snapshot by date, so you can browse the backups and copy files with the usual tools. Requires FUSE and works on Linux, macOS, and FreeBSD. The command keeps running until Ctrl+C or umount /mnt/restic.

Retention policy and cleanup

  • restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --dry-run

    Shows which backups would be kept and which would be removed under the policy: the last backup of each day for the last 7 days that have a backup, and likewise for 4 weeks and 12 months. Nothing is deleted in a dry run.

  • restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune

    Removes the backups the policy doesn't cover, and right after that frees the data that's no longer in use. The policy applies separately to each group of host and paths, so backups of other servers aren't deleted by mistake.

  • restic forget --keep-within 30d --prune

    Keeps all the backups from the 30 days before the latest backup. You can combine units, such as 1y6m.

  • restic forget --tag mysql --keep-daily 14 --prune

    Applies a policy only to the backups tagged mysql, so database backups can have a different retention period from file backups.

  • restic forget --keep-daily 7 --keep-tag keep --prune

    Always keeps backups tagged keep, such as a backup taken before an upgrade, even when they fall outside the policy.

  • restic tag --add keep 4f2a9c1e

    Adds a tag to an existing snapshot. Together with --keep-tag, this is how you mark an important backup so it isn't removed.

  • restic forget 4f2a9c1e

    Removes a specific snapshot by its ID. The data itself is deleted from the repository only after prune.

  • restic prune

    Deletes data that no snapshot uses anymore from the repository and frees space. The operation locks the repository and may take a long time on a large one, so run it at a time when no backups are running.

  • restic prune --max-unused 10%

    Allows up to 10% unused data to remain, to save on repacking files in the repository. The default is 5%, and 0 frees as much space as possible.

Repository checks and maintenance

  • restic check

    Checks the structure and integrity of the repository without reading all the data. Run it regularly, for example once a week.

    Example: no errors were found

  • restic check --read-data-subset=10%

    Also reads and verifies a random 10% of the data in the repository. This way, each run checks a different part, without downloading the whole repository.

  • restic check --read-data-subset=1/5

    Checks the first of five parts. When you run a different part each time, 1/5 through 5/5, all the data is checked in one cycle.

  • restic check --read-data

    Reads and verifies all the data in the repository. This is the most thorough check, but it downloads the whole repository, so it suits a small or local one.

  • restic unlock

    Removes stale locks left in the repository, for example after a backup was interrupted. Use it when you get the error repository is already locked and no other backup is running at the same time.

  • restic unlock --remove-all

    Caution: removes all locks, including those of processes that are still running. Run it only when you know for certain that no backup, prune, or check is running right now.

  • restic backup --retry-lock 10m /var/www

    When the repository is locked, waits up to ten minutes and retries instead of failing immediately. Useful when several servers back up to the same repository.

  • restic cache --cleanup

    Deletes old cache directories of repositories that are no longer in use. The cache is usually in the ~/.cache/restic directory.

  • restic -r /srv/restic-copy copy --from-repo /srv/restic-repo

    Copies backups from one repository to a second one, for example to keep an extra copy in another location. Create the destination repository first with init --from-repo /srv/restic-repo --copy-chunker-params, so deduplication works in the copy too. Pass the source repository's password with the --from-password-file option.

  • restic repair index

    Rebuilds the repository index. Run it only when the check command reports a problem and tells you to, not as routine maintenance.

Keys and passwords

  • restic key list

    Lists the repository's keys. Each key has its own password, and any of them opens the same data.

  • restic key add --new-password-file /root/.restic-pass-2

    Adds another password to the repository. This way, you can keep a backup password in a safe place, or give another server a separate password that you can revoke later.

  • restic key passwd

    Changes the password of the current key. Afterward, update the password file your scripts use.

  • restic key remove 9e3f12ab

    Removes a key by its ID. You can't remove the key currently in use, so the repository is never left without an active password.

Automation, exit codes, and scheduling

  • RESTIC_REPOSITORY=sftp:backup@backup.example.com:/srv/restic-repo

    A line in the /root/.restic.env configuration file: the repository address. When this variable is set, you don't need the -r option in every command.

  • RESTIC_PASSWORD_FILE=/root/.restic-pass

    A second line in the same file: the path to the password file. S3 or REST server credentials go here the same way.

  • chmod 600 /root/.restic.env /root/.restic-pass

    Limits access to the configuration file and the password file to their owner, so other users on the server can't read them.

  • set -a; . /root/.restic.env; set +a

    In a script: loads the variables from the configuration file and passes them to the restic commands that follow.

  • restic backup --tag daily /var/www /etc; rc=$?

    In a script: saves the backup's exit code in a variable, so you can write it to the log and decide whether to go on to forget.

  • echo "$(date '+%F %T') backup exit code $rc" >> /var/log/restic-backup.log

    Writes the date and the exit code of each run to the log, so it's easy to see when a backup failed.

    Example: 2026-11-29 02:41:17 backup exit code 0

  • echo $?

    Shows the exit code of the last command: 0 success; 1 failure, and for a backup no snapshot was created; 3 partial backup, where some files couldn't be read and the snapshot was created without them, or forget couldn't remove some of the snapshots; 10 the repository doesn't exist; 11 the repository couldn't be locked; 12 wrong password; 130 the command was canceled. Any other code counts as a failure.

    Example: 0

  • 30 2 * * * /usr/local/bin/restic-backup.sh >> /var/log/restic-backup.log 2>&1

    A crontab line that runs the backup script every night at 02:30 and saves the output to the log. Edit the file with crontab -e.

  • EnvironmentFile=/root/.restic.env

    In systemd, in the /etc/systemd/system/restic-backup.service unit file, in the [Service] section: loads the same configuration file. Next to it, set Type=oneshot and ExecStart=/usr/local/bin/restic-backup.sh.

  • OnCalendar=*-*-* 02:30:00

    In the restic-backup.timer file, in the [Timer] section: runs the service every day at 02:30. The Persistent=true line runs a backup that was missed while the server was off as soon as it boots.

  • sudo systemctl enable --now restic-backup.timer

    Starts the timer now and on every server boot.

  • systemctl list-timers restic-backup.timer

    Shows when the backup last ran and when it will run next.

  • journalctl -u restic-backup.service -n 50

    Shows the last 50 lines of the backup service's log, including restic errors.

Names and IDs in the examples

The examples on this page use the example.com domain, sample snapshot IDs, and the database name example_db, so replace them with your own names and IDs. Most examples set the repository and password in the RESTIC_REPOSITORY and RESTIC_PASSWORD_FILE environment variables; without them, add -r and --password-file to every command.

The repository password

Everything stored in the repository is encrypted, so even someone who holds the repository files can't read them without the password. For the same reason, the password is the only key to your data: keep a copy of it outside the server, for example in a password manager, and you can add another password to the repository with restic key add.

A template for a daily backup script

  • At the start of the script, load the configuration file with the repository address and the path to the password file, with 600 permissions.
  • Back up the files with restic backup and the databases with --stdin-from-command, each with its own tag.
  • Write the exit code of each command to the log: 0 success, 3 partial backup, and any other code a failure.
  • Only after a successful backup, run restic forget with the retention policy and --prune, and once a week restic check.
  • Schedule the script in cron, where you can build the line with the Cron expression generator, or with a systemd timer, as shown on the systemctl commands page.
  • Make sure the backup actually runs: check restic snapshots --latest 1 from time to time, or have each successful backup send a request to a monitoring service that alerts you when the request doesn't arrive.

Practice restoring

A backup is only worth something if you can restore from it. Every few months, restore files from a random date and a test database to a separate directory, and check that the site runs from them. This way, you find out early about a missing exclude or a database that wasn't backed up, and you know exactly which commands to run on the day a restore is really needed.

Backing up databases

Copying the files of a running database doesn't give a consistent copy, so back up the output of mysqldump or pg_dump instead. The full export and restore options are on the MySQL commands and PostgreSQL commands pages.

restic or rsync

rsync copies and syncs files, and the destination is a mirror of the source. restic keeps a history of encrypted backups, each one a snapshot you can restore from, with no duplicate data between backups. A backup to a repository on another server over SFTP relies on a regular SSH connection, and you'll find its settings on the SSH commands page.

Did you know?

On a Linux VPS, you get full root access over SSH, so you can install restic, back up your sites and databases, and schedule the backups directly from the server. Alongside the backups of our cloud backup service, restic is a good fit for keeping your own extra copy, encrypted and in a location you choose, following the 3-2-1 rule.

Linux VPS plans

restic commands FAQ

What's the difference between restic and rsync?

rsync copies and syncs files, and the destination is a copy of the current state. restic saves each backup as a separate, encrypted snapshot, so you can go back to any date that was saved, and data that didn't change isn't stored twice. rsync is a good fit for moving sites and for mirrors, and restic is a good fit for backups with history.

How much space do daily backups take up in restic?

The first backup stores all the data, compressed. Each backup after that stores only new data, and identical chunks of files are stored only once, even when they appear in several files or on several servers. That's why daily backups over months take up much less space than a full copy every day, and the size depends mainly on how much data changes. The restic stats --mode raw-data command shows the actual size.

How do I back up a database with restic?

Run mysqldump or pg_dump through the --stdin-from-command option, and the output is saved directly in the repository as a file, without a temporary file on disk. If the export fails, no backup is created and you get an error. To restore, extract the file with restic dump and load it into the database.

How do I keep my restic repository password safe?

Keep the password in a file on the server, with 600 permissions, and another copy outside the server, for example in a password manager. You can add another password to the repository with restic key add and keep it somewhere else. Encryption is what lets you store your backups on any destination with confidence, so the password is the most important thing to keep.

How do I make sure I can actually restore from my backups?

The restic check command checks the repository, and with --read-data-subset it also reads and verifies part of the data on each run. Beyond that, restore files and a database to a separate directory from time to time, and check that the site runs from them. This way, you know the backup is complete and you're familiar with the restore process in advance.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement