How to use the command 'restic' (with examples)
Restic is a backup program that aims to be fast, secure, and efficient. It provides several use cases for managing backup repositories. In this article, we will explore different use cases of the ‘restic’ command with examples.
Use case 1: Initialize a backup repository in the specified local directory
Code:
restic init --repo path/to/repository
Motivation: This use case is useful for initializing a backup repository in a specific local directory. It creates the necessary data structures and files required by Restic to store and manage backups.
Explanation:
restic init
is the command to initialize a backup repository.--repo
specifies the path to the repository where the backups will be stored.path/to/repository
is the desired path to the backup repository.
Example output:
created restic repository b7cb1974b6 at /path/to/repository
Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.
Use case 2: Backup a directory to the repository
Code:
restic --repo path/to/repository backup path/to/directory
Motivation: Backing up important directories is essential to prevent data loss. With this use case, you can easily create backups of a specific directory and store them in the repository.
Explanation:
restic
is the command used for backup operations.--repo
specifies the path to the repository where the backups will be stored.backup
is the subcommand used to perform a backup.path/to/directory
is the directory that will be backed up.
Example output:
scanned 1234 directories, 5678 files in 3.42s
Files: 5678 new, 0 changed, 0 removed
Dirs: 1234 new, 0 changed, 0 removed
Added: 123.45 MiB
processed files: 5678, size: 123.45 MiB
Use case 3: Show backup snapshots currently stored in the repository
Code:
restic --repo path/to/repository snapshots
Motivation: It’s important to keep track of the available snapshots in a backup repository. This use case allows you to list all the snapshots stored in the specified repository.
Explanation:
restic
is the command used for backup operations.--repo
specifies the path to the repository where the backups are stored.snapshots
is the subcommand used to display the existing snapshots.
Example output:
ID Time Host Tags Paths
------------------------------------------------------------------------
abcd1234 2021-01-01 12:00:00 example.com tag1, tag2 /path/to/backup/dir
efgh5678 2021-01-02 08:00:00 example.com tag2, tag3 /path/to/backup/dir
Use case 4: Restore a specific backup snapshot to a target directory
Code:
restic --repo path/to/repository restore latest|snapshot_id --target path/to/target
Motivation: Restoring a specific backup snapshot is useful when you want to retrieve a specific version of your data. With this use case, you can restore a snapshot to a specified target directory.
Explanation:
restic
is the command used for backup operations.--repo
specifies the path to the repository where the backups are stored.restore
is the subcommand used for restoring backups.latest
orsnapshot_id
indicates the snapshot to be restored. Iflatest
is used, Restic will restore the most recent snapshot.--target
specifies the directory where the restored files will be written.
Example output:
restored /path/to/target/file1.txt
restored /path/to/target/file2.txt
Use case 5: Restore a specific path from a specific backup to a target directory
Code:
restic --repo path/to/repository restore snapshot_id --target path/to/target --include path/to/restore
Motivation: Being able to restore specific paths from a backup snapshot is valuable when you only need to retrieve a subset of your data. This use case allows you to restore a specific path from a backup to a target directory.
Explanation:
restic
is the command used for backup operations.--repo
specifies the path to the repository where the backups are stored.restore
is the subcommand used for restoring backups.snapshot_id
is the ID of the specific snapshot to be restored.--target
specifies the directory where the restored files will be written.--include
specifies the path within the backup to be restored.
Example output:
restored /path/to/target/restored_file1.txt
restored /path/to/target/restored_file2.txt
Use case 6: Clean up the repository and keep only the most recent snapshot of each unique backup
Code:
restic forget --keep-last 1 --prune
Motivation: As the backup repository grows, it’s necessary to clean up old backups and free up storage space. This use case allows you to remove older snapshots, keeping only the most recent snapshot of each unique backup.
Explanation:
restic
is the command used for backup operations.forget
is the subcommand used to remove old backups.--keep-last 1
ensures that only the most recent snapshot of each unique backup is kept.--prune
removes unreferenced data and packs the repository to reclaim storage space.
Example output:
keeping 1 snapshots; removing 2 snapshots
successfully removed 500 MB of data
Conclusion:
Restic provides a comprehensive set of features to manage backup repositories. From initializing a repository to restoring specific snapshots, Restic offers powerful functionality for efficient and secure backups. By understanding and utilizing the different use cases of the ‘restic’ command, users can effectively manage their backups and ensure the safety of their data.