Scripting
This is a list of how certain tasks may be accomplished when you use restic via scripts.
Check if a repository is already initialized
You may find a need to check if a repository is already initialized,
perhaps to prevent your script from trying to initialize a repository multiple
times (the init
command contains a check to prevent overwriting existing
repositories). The command cat config
may be used for this purpose:
$ restic -r /srv/restic-repo cat config
Fatal: repository does not exist: unable to open config file: stat /srv/restic-repo/config: no such file or directory
Is there a repository at the following location?
/srv/restic-repo
If a repository does not exist, restic (since 0.17.0) will return exit code 10
and print a corresponding error message. Older versions return exit code 1
.
Note that restic will also return exit code 1
if a different error is encountered
(e.g.: incorrect password to cat config
) and it may print a different error message.
If there are no errors, restic will return a zero exit code and print the repository
metadata.
Exit codes
Restic commands return an exit code that signals whether the command was successful. The following table provides a general description, see the help of each command for a more specific description.
Warning
New exit codes will be added over time. If an unknown exit code is returned, then it MUST be treated as a command failure.
0 |
Command was successful |
1 |
Command failed, see command help for more details |
2 |
Go runtime error |
3 |
|
10 |
Repository does not exist (since restic 0.17.0) |
11 |
Failed to lock repository (since restic 0.17.0) |
12 |
Wrong password (since restic 0.17.1) |
130 |
Restic was interrupted using SIGINT or SIGSTOP |
JSON output
Restic outputs JSON data to stdout
if requested with the --json
flag.
The structure of that data varies depending on the circumstance. The
JSON output of most restic commands are documented here.
Note
Not all commands support JSON output. If a command does not support JSON output, feel free to submit a pull request!
Warning
We try to keep the JSON output backwards compatible. However, new message types or fields may be added at any time. Similarly, enum-like fields for which a fixed list of allowed values is documented may be extended at any time.
Output formats
Currently only the output on stdout
is JSON formatted. Errors printed on stderr
are still printed as plain text messages. The generated JSON output uses one of the
following two formats.
Single JSON document
Several commands output a single JSON document that can be parsed in its entirety. Depending on the command, the output consists of either a single or multiple lines.
JSON lines
Several commands, in particular long running ones or those that generate a large output,
use a format also known as JSON lines. It consists of a stream of new-line separated JSON
messages. You can determine the nature of the message using the message_type
field.
backup
The backup
command uses the JSON lines format with the following message types.
Status
|
Always “status” |
|
Time since backup started |
|
Estimated time remaining |
|
Percentage of data backed up (bytes_done/total_bytes) |
|
Total number of files detected |
|
Files completed (backed up to repo) |
|
Total number of bytes in backup set |
|
Number of bytes completed (backed up to repo) |
|
Number of errors |
|
List of files currently being backed up |
Error
|
Always “error” |
|
Error message |
|
What restic was trying to do |
|
Usually, the path of the problematic file |
Verbose Status
Verbose status provides details about the progress, including details about backed up files.
|
Always “verbose_status” |
|
Either “new”, “unchanged”, “modified” or “scan_finished” |
|
The item in question |
|
How long it took, in seconds |
|
How big the item is |
|
How big the metadata is |
|
Total number of files |
Summary
Summary is the last output line in a successful backup.
|
Always “summary” |
|
Number of new files |
|
Number of files that changed |
|
Number of files that did not change |
|
Number of new directories |
|
Number of directories that changed |
|
Number of directories that did not change |
|
Number of data blobs |
|
Number of tree blobs |
|
Amount of (uncompressed) data added, in bytes |
|
Amount of data added (after compression), in bytes |
|
Total number of files processed |
|
Total number of bytes processed |
|
Total time it took for the operation to complete |
|
ID of the new snapshot. Field is omitted if snapshot creation was skipped |
cat
The cat
command returns data about various objects in the repository, which
are stored in JSON form. Specifying --json
or --quiet
will suppress any
non-JSON messages the command generates.
diff
The diff
command uses the JSON lines format with the following message types.
change
|
Always “change” |
|
Path that has changed |
|
Type of change, a concatenation of the following characters: “+” = added, “-” = removed, “T” = entry type changed, “M” = file content changed, “U” = metadata changed, “?” = bitrot detected |
statistics
|
Always “statistics” |
|
ID of first snapshot |
|
ID of second snapshot |
|
Number of changed files |
|
DiffStat object, see below |
|
DiffStat object, see below |
DiffStat object
|
Number of changed files |
|
Number of changed directories |
|
Number of changed other directory entries |
|
Number of data blobs |
|
Number of tree blobs |
|
Number of bytes |
find
The find
command outputs a single JSON document containing an array of JSON
objects with matches for your search term. These matches are organized by snapshot.
If the --blob
or --tree
option is passed, then the output is an array of
Blob objects.
|
Number of matches in the snapshot |
|
ID of the snapshot |
|
Array of Match objects detailing a match |
Match object
|
Object path |
|
UNIX permissions |
|
Object type e.g. file, dir, etc… |
|
Access time |
|
Modification time |
|
Change time |
|
Object name |
|
Name of owner |
|
Name of group |
|
Inode number |
|
UNIX file mode, shorthand of |
|
OS specific device identifier |
|
Number of hardlinks |
|
ID of owner |
|
ID of group |
|
Size of object in bytes |
Blob object
|
Either “blob” or “tree” |
|
ID of found blob |
|
Path in snapshot |
|
Parent tree blob, only set for type “blob” |
|
Snapshot ID |
|
Snapshot timestamp |
forget
The forget
command prints a single JSON document containing an array of
ForgetGroups. If specific snapshot IDs are specified, then no output is generated.
The prune
command does not yet support JSON such that forget --prune
results in a mix of JSON and text output.
ForgetGroup
|
Tags identifying the snapshot group |
|
Host identifying the snapshot group |
|
Paths identifying the snapshot group |
|
Array of Snapshot objects that are kept |
|
Array of Snapshot objects that were removed |
|
Array of Reason objects describing why a snapshot is kept |
Snapshot object
|
Timestamp of when the backup was started |
|
ID of the parent snapshot |
|
ID of the root tree blob |
|
List of paths included in the backup |
|
Hostname of the backed up machine |
|
Username the backup command was run as |
|
ID of owner |
|
ID of group |
|
List of paths and globs excluded from the backup |
|
List of tags for the snapshot in question |
|
restic version used to create snapshot |
|
Snapshot ID |
|
Snapshot ID, short form |
Reason object
|
Snapshot object, including |
|
Array containing descriptions of the matching criteria |
|
Object containing counters used by the policies |
init
The init
command uses the JSON lines format, but only outputs a single message.
|
Always “initialized” |
|
ID of the created repository |
|
URL of the repository |
key list
The key list
command returns an array of objects with the following structure.
|
Is currently used key? |
|
Unique key ID |
|
User who created it |
|
Name of machine it was created on |
|
Timestamp when it was created |
ls
The ls
command uses the JSON lines format with the following message types.
As an exception, the struct_type
field is used to determine the message type.
snapshot
|
Always “snapshot” |
|
Always “snapshot” (deprecated) |
|
Timestamp of when the backup was started |
|
ID of the parent snapshot |
|
ID of the root tree blob |
|
List of paths included in the backup |
|
Hostname of the backed up machine |
|
Username the backup command was run as |
|
ID of owner |
|
ID of group |
|
List of paths and globs excluded from the backup |
|
List of tags for the snapshot in question |
|
Snapshot ID |
|
Snapshot ID, short form |
node
|
Always “node” |
|
Always “node” (deprecated) |
|
Node name |
|
Node type |
|
Node path |
|
UID of node |
|
GID of node |
|
Size in bytes |
|
Node mode |
|
Node access time |
|
Node modification time |
|
Node creation time |
|
Inode number of node |
restore
The restore
command uses the JSON lines format with the following message types.
Status
|
Always “status” |
|
Time since restore started |
|
Percentage of data restored (bytes_restored/total_bytes) |
|
Total number of files detected |
|
Files restored |
|
Files skipped due to overwrite setting |
|
Total number of bytes in restore set |
|
Number of bytes restored |
|
Total size of skipped files |
Error
|
Always “error” |
|
Error message |
|
Always “restore” |
|
Usually, the path of the problematic file |
Verbose Status
Verbose status provides details about the progress, including details about restored files. Only printed if –verbose=2 is specified.
|
Always “verbose_status” |
|
Either “restored”, “updated”, “unchanged” or “deleted” |
|
The item in question |
|
Size of the item in bytes |
Summary
|
Always “summary” |
|
Time since restore started |
|
Total number of files detected |
|
Files restored |
|
Files skipped due to overwrite setting |
|
Total number of bytes in restore set |
|
Number of bytes restored |
|
Total size of skipped files |
snapshots
The snapshots command returns a single JSON object, an array with objects of the structure outlined below.
|
Timestamp of when the backup was started |
|
ID of the parent snapshot |
|
ID of the root tree blob |
|
List of paths included in the backup |
|
Hostname of the backed up machine |
|
Username the backup command was run as |
|
ID of owner |
|
ID of group |
|
List of paths and globs excluded from the backup |
|
List of tags for the snapshot in question |
|
restic version used to create snapshot |
|
Snapshot statistics, see “Summary object” |
|
Snapshot ID |
|
Snapshot ID, short form |
Summary object
The contained statistics reflect the information at the point in time when the snapshot was created.
|
Time at which the backup was started |
|
Time at which the backup was completed |
|
Number of new files |
|
Number of files that changed |
|
Number of files that did not change |
|
Number of new directories |
|
Number of directories that changed |
|
Number of directories that did not change |
|
Number of data blobs |
|
Number of tree blobs |
|
Amount of (uncompressed) data added, in bytes |
|
Amount of data added (after compression), in bytes |
|
Total number of files processed |
|
Total number of bytes processed |
stats
The stats command returns a single JSON object.
|
Repository size in bytes |
|
Number of files backed up in the repository |
|
Number of blobs in the repository |
|
Number of processed snapshots |
|
Repository size in bytes if blobs were uncompressed |
|
Factor by which the already compressed data has shrunk due to compression |
|
Percentage of already compressed data |
|
Overall space saving due to compression |
version
The version command returns a single JSON object.
|
restic version |
|
Go compile version |
|
Go OS |
|
Go architecture |