Short notes on Restic
I'm always fashionably late to the party. So forgive me for being the last to get all excited about Restic. Encryption by default, snapshots, deduplication, support for various storage types—Restic checks all the boxes. Not only that, it is also a supremely easy to use backup tool.
While learning how to use Restic, I took notes for future reference. I'm sharing my trivial findings here on the off-chance that someone might find them useful. The notes cover a simple scenario, where Restic backs up data to an external USB storage device.
Install Restic on Debian, Ubuntu, Linux Mint:
sudo apt install restic
sudo restic self-update
Install Restic on openSUSE:
sudo zypper in restic
sudo restic self-update
Initialize a new backup repository (where /the/backup/place is the mount point of the storage device):
restic init --repo "/the/backup/place"
Perform a dry run (where /data/to/save is the path to the directory you want to back up):
restic -r "/the/backup/place" backup "/data/to/save" --dry-run -vv | grep "added"
Perform backup:
restic -r "/the/backup/place" --verbose backup "/data/to/save"
Check the integrity of the backup data:
restic -r "/the/backup/place" check
View backup stats:
restic -r "/the/backup/place" stats
List all snapshots:
restic -r "/the/backup/place" snapshots
The command above returns a list of all snapshots and their IDs.
Check the differences between two snapshots (where ID1 and ID1 are the IDs of the snapshots you want to compare):
restic -r "/the/backup/place" diff ID1 ID2
List all files in a snapshot (where ID is the ID of the desired snapshot):
restic -r "/the/backup/place" ls ID
Find a specific file in a snapshot (where ID is the ID of the desired snapshot and find this is the actual search string):
restic -r "/the/backup/place" ls ID | grep "find this"
Restore from a snapshot (where /where/to/restore is the path to the directory for saving restored data, and ID is the ID of the desired snapshot):
restic -r "/the/backup/place" restore ID --target "/where/to/restore"
Restore from the latest snapshot:
restic -r "/the/backup/place" restore latest --target "/where/to/restore"
Restore a specific file from a snapshot (where /file/to/restore is the path to the desired file, ID is the snapshot ID, and /where/to/restore is the path to the directory for saving restored data):
restic restore ID --include="/file/to/restore" --target="/where/to/restore"
Delete a snapshot (where ID is the hash of the desired snapshot):
restic -r "/the/backup/place" forget ID
restic -r "/the/backup/place" prune
Alternatively you can delete all snapshots except the last n ones:
restic forget --keep-last 3 --prune
The command above deletes all snapshots except the three last ones.
Using environment variables
Create the ~/.restic.env file, and add the desired environment variables. For example, you can specify the password and the backup repository as follows:
export RESTIC_PASSWORD="secret-password"
export RESTIC_REPOSITORY="/the/backup/place"
Make the file read-only:
chmod 400 ~/.restic.env
Before running any Restic commands, activate the file for the current Bash session:
source ~/.restic.env
Now, you can run Restic commands without specifying the backup repository and password:
restic check
Using Autorestic for easy backup configuration
Autorestic makes it possible to create backup profiles in the YAML format. Create an ~/.autorestic.yml file with the following configuration:
version: 2
locations:
data:
from: /data/to/save
to: ssd
backends:
ssd:
type: local
path: /the/backup/place
key: secret-password
Save the changes and use the autorestic check
to validate the configuration. You can then perform a backup by running the following command:
autorestic backup
To restore from a backup, use the command below:
autorestic restore -l data --from ssd --to "/where/to/restore"
This restores the location data from the ssd backend to the /where/to/restore location.