How to use the command 'restic' (with examples)
Restic is a fast, secure, and efficient backup program that provides users with the ability to automatically back up directories and files to repositories. Designed for flexibility, it supports multiple storage types and ensures that all data is encrypted, minimizing the risk of unauthorized access. With built-in de-duplication, restic helps maintain storage efficiency.
Use case 1: Initialize a backup repository in the specified local directory
Code:
restic init --repo path/to/repository
Motivation:
Initializing a backup repository is the first step in utilizing restic’s powerful features. This repository acts as a storage location for all backups you make, functioning much like a database where your backups will be consistently managed. Before you can back up your data, the repository needs to be created and initialized to prepare it for receiving data. This step is crucial as it lays the groundwork for structuring and accessing your backups.
Explanation:
restic
: The main command to invoke the restic tool.init
: This command initializes a new repository.--repo path/to/repository
: Specifies the path to the local directory where the new backup repository should be created. The path could be a relative path or an absolute directory path, depending on where you want your repository to reside.
Example Output:
created restic repository 1e894e6a
Please note that knowledge of your password is required!
This signifies successful repository creation, and it is now ready to be used for subsequent backup operations.
Use case 2: Backup a directory to the repository
Code:
restic --repo path/to/repository backup path/to/directory
Motivation:
Backing up your directories regularly is a critical part of data management and security. This command enables you to capture the state of specific directories at a point in time, thereby protecting against data loss from unexpected deletions, corruption, or other failures. Storing these backups in a repository ensures they can be restored as needed, providing peace of mind and security for your data.
Explanation:
restic
: Invokes the restic program.--repo path/to/repository
: Specifies the repository where the backup will be stored. This must be the repository previously initialized.backup
: This is the subcommand to start a backup operation.path/to/directory
: Indicates the path of the directory you wish to back up. It can be an absolute or relative path within your system.
Example Output:
building file list ...
Files: 142 new, 0 changed, 0 unmodified
Dirs: 49 new, 0 changed, 0 unmodified
Added to the repo: 9.569 MiB
This indicates successful backup of the specified directory, detailing the number of files and directories backed up and the total size added to the repository.
Use case 3: Show backup snapshots currently stored in the repository
Code:
restic --repo path/to/repository snapshots
Motivation:
Over time, you will create multiple snapshots of data with restic. Viewing these snapshots is critical to manage and make informed decisions about restoring or organizing data. It helps identify when backups were made and the specific data versions stored. This use case is especially beneficial for tracking backup intervals and versions stored.
Explanation:
restic
: The restic command line tool.--repo path/to/repository
: Path to the backup repository.snapshots
: Lists all the backup snapshots currently stored in the specified repository. This command provides a view into the past states of your directories that have been saved.
Example Output:
ID Date Host Tags Directory
----------------------------------------------------------------------------
41e49c53 2023-10-01 12:34:56 localhost /home/user/documents
999e46b3 2023-10-02 12:34:56 localhost /home/user/music
7a854a72 2023-10-03 12:34:56 localhost /home/user/photos
Each line presents a unique snapshot ID along with the date and time of the snapshot, the host from where the backup originated, and the directory backed up.
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:
Data loss or corruption can occur unexpectedly. Restic allows you to restore data from your backups to recover lost files or rollback changes. You can retrieve either the latest snapshot or specify any previous snapshot by its ID, restoring the data to a location of your choice and thereby ensuring continuity and data recovery.
Explanation:
restic
: Calling the restic binary.--repo path/to/repository
: Designates the source repository.restore
: The command to initiate the restore process.latest|snapshot_id
: Specifies which snapshot to restore. Uselatest
to restore from the most recent snapshot, or use a specificsnapshot_id
to restore a particular version.--target path/to/target
: Path to the directory where the restored files will be placed.
Example Output:
restoring <snapshot_id> to /home/user/documents_restore
The system indicates it has begun restoring the contents from the specified snapshot into the targeted directory.
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:
In certain cases, you may need to recover a specific file or sub-directory without restoring an entire backup. This command provides a selective restoration solution. By defining a specific path within a snapshot, you can efficiently restore only what is necessary, saving time and storage space in the process.
Explanation:
restic
: Start the restic command.--repo path/to/repository
: Points to the repository containing the backups.restore
: Commands the restore action.snapshot_id
: Indicates the specific backup version to be used.--target path/to/target
: Specifies where to save the restored content.--include path/to/restore
: Identifies the specific file or directory path to restore from within the snapshot.
Example Output:
restoring <snapshot_id> to /home/user/partials_restore
The operation notifies that it is restoring the specified path from the snapshot to the target directory.
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:
Over time repositories grow with each snapshot, possibly leading to diminished storage capacity and managing efficiency. To address this, restic allows for cleaning up old, redundant snapshots, thus retaining only the most recent ones. This process helps optimize storage usage while ensuring you have the latest backups available.
Explanation:
restic
: Run the restic command tool.forget
: Initiates the forgetting operation, which manages retention policies.--keep-last 1
: Ensures that only the most recent snapshot will be kept, which is crucial for retaining the latest state of the data.--prune
: Follows up by removing the unreferenced data from the repository, effectively freeing up space.
Example Output:
apply retention policy for snapshots
snapshots for each host and path keep 1:
ID Date Host Tags Directory
----------------------------------------------------------------------------
7a854a72 2023-10-03 12:34:56 localhost /home/user/photos
keeping snapshot 7a854a72
removing snapshot <old_snapshot_id>
This output indicates that snapshots older than the latest one have been successfully removed, and space has been reclaimed.
Conclusion:
Restic is a versatile and powerful tool designed for secure and efficient handling of backups. These example use cases demonstrate the tool’s adaptability to a range of backup and restore scenarios, helping to safeguard data across multiple environments. Whether initializing a new repository or cleaning up old snapshots, restic provides structured, reliable solutions for your backup needs.