Restic Basics
Table of Contents
Restic is an easy to use fast an secure way to create and manage your backups. It has support for many different backends, including local backups.
Below I will describe basic restic usage; how to create and manage backups using a local usb/external-drive as a backup repo, as it requires no extra setup.
1 Init Repository
Restic
uses repos to store your backups. The benefit of this is that
you can have many different backups in the same repo. You can also
have backups of the same directories from different points in time
that are stored space efficiently1 in the same repo. Each backup
in a repo is referred to as a snapshot. To initialize a restic repo
run:
restic init --repo "${REPO}"
You'll be prompted for a password because all data in the repo is encrypted and can therefore only be accessed if you have the password. Note that if you lose the password the data will be irrecoverably lost.
2 Backup Data
To create a backup of ~/documents
run:
restic -r "${REPO}" --verbose backup ~/documents
This will add a snapshot to ${REPO}
with all the contents of
~/documents
. Each time you run the backup command restic
will add
a snapshot to the specified repo.
2.1 Exclude Directories/Files from a Backup
You can be more specific with the files and directories you want in a
backup by specifying --exclude
or --exclude-file
. The latter
receives a file similar to a .gitignore
file and contains patterns
to exclude.
restic -r "${REPO}" backup ~/documents --exclude="*.txt"
This will exclude all the file ending with .txt
from the backup.
3 List Snapshots
Before you can restore a backup you first must know which one to use. You can do this by listing the snapshots.
restic -r "${REPO}" snapshots
If you have many snapshots you can filter the list, see the docs for info on how to do that.
4 Restore Backups
Once you have found the snapshots you want to restore copy its ID
found when list snapshots and run:
ID="set you snapshot ID here" restic -r "${REPO}" restore "${ID}" --target /tmp/restore-snapshot
This will restore the snapshot with ${ID}
to the directory
/tmp/restore-snapshot
. Instead of using the ${ID}
you can also use
the latest
key word to restore the latest snapshot. To do this
simply replace ${ID}
with latest
in the above command.
5 Remove Snapshots
If you want to clean up your repo or if you need to make space, you can
use the forget
command to remove single snapshots from a repo with
its ID
.
ID="set you snapshot ID here" restic -r "${REPO}" forget "${ID}" --prune
This removes the snapshot from the repo and deletes all unreferrenced data.
6 Password Management
You may have noticed that every interaction with the repo required
entering of the password. This is very annoying and can be resolved
with the RESTIC_PASSWORD_COMMAND
environment variable. You could use
gnupg or pass to decrypt a file containing the password to the repo.
For pass
add to your .bash_profile
the line:
export RESTIC_PASSWORD_COMMAND="pass <restic/repo1>"
This requires that you have setup pass
which I will probably do a
tutorial on in the future.
Footnotes:
restic
has a notion of de-duplication, where if a file has
the same contents as a file already in the repo than restic
will not
store that file a second time but rather use the data that is already
in the repo.