## Backup
_Created: 2026-01-16_ | #restic | [[restic]]
The contents of a directory at a specific point in time is called a “snapshot” in restic.
Backing up `~/.config` here.
```shell-session
$ restic backup ~/.config
repository c69b8030 opened (version 2, compression level auto)
no parent snapshot found, will read all files
[0:00] 0 index files loaded
Files: 1428 new, 0 changed, 0 unmodified
Dirs: 119 new, 0 changed, 0 unmodified
Added to the repository: 498.646 MiB (88.160 MiB stored)
processed 1428 files, 500.661 MiB in 0:02
snapshot ab204b53 saved
```
- The specific snapshot just created is identified by a sequence of hexadecimal characters, `ab204b53` in this case.
- 500.661 MiB processed, out of which 498.646 MiB was added, compressing to 88.160 MiB
- Data processed != data added because some of the data was duplicate
- Symlinks are copied as-is, not followed. On restore point to same target whether it exists or not
- Will descend into bind-mounts in backup source. Mount device is not checked
- `atime` is not saved in backup, so if changed, then snapshot is modified. Use `--with-atime` to save `atime` and avoid this behavior
- Backups can contain tags: `--tag projectX --tag foo`
- Useful to select snapshots for operations like keep or delete
>[!note] Disk full in target
>On disk full situation, incomplete data would be stored, but a snapshot would not be created as it is the last step.
### Exclude files
- You can use _exclude files_ to exclude paths, e.g. `--exclude="*.c" --exclude-file=excludes.txt`
- Patterns use the syntax of the Go function [filepath.Match](https://pkg.go.dev/path/filepath#Match) and are tested against the full path of a file/dir to be saved
- Exclude file can contain environment variables like `/home/$USER/foo`
- To get a literal dollar sign, write `$` to the file
- `~` not expanded, use `$HOME`
### Include files
- To backup multiple locations, use:
- `--files-from`: Path to a file containing list of locations, can contain wildcards like in exclude file
- `--files-from-verbatim`: Same as above, but doesnt support patterns. Useful for output of `find` command, with special characters
- `--files-from-raw`: Same as above, but line delimiter is _null_
### check
- Run `restic check` to verify that all data is properly stored in the repository.
- Should be run regularly to make sure the internal structure of the repository is free of errors.
- By default, the `check` command does not verify that the actual pack files on disk in the repository are unmodified, because doing so requires reading a copy of every pack file in the repository. To tell restic to also verify the integrity of the pack files in the repository, use the `--read-data` flag
### File change detection
- To do a diff, `restic` tries to find a previous snapshot (`parent` as per `restic` terminology) of a file. This is done by searching by the `--group-by` value
- `--group-by` defaults to `host,paths` and so it searches for snapshot from the host which contain the path
- Change detection only done for regular files, not dirs or specials
- Renames are not detected, so new name would be stored as a new file
- Following metadata of file used for change detection: `mtime`, `ctime`, inode and file size
- flags exist to force restic to ignore these metadata and force a backup even though they have not changed
### Override snapshot creation
- Every backup creates a snapshot even though nothing might have changed
- To skip snapshot if nothing as changed, use `--skip-if-unchanged`
- All parent directories of a backup path are checked for metadata changes. e.g. if backup source is `/data/database`, a change in metadata for `/data` can also trigger a snapshot as if something has changed
- If we dont care about the parent directory, we should change to backup source dir, and use relative paths: `cd /home/user/work && restic -r /srv/restic-repo backup . --skip-if-unchanged`
### Backing up command outputs
```
restic -r /srv/restic-repo backup \
--stdin-filename production.sql \
--stdin-from-command -- mysqldump --host example mydb [...]
```
Will save a backup of the file `production.sql` containing the contents of the stdout of the mysqldump command.
If the command has an error, snapshot is _not_ created.
### Backing up stdin
```
gzip bigfile.dat | restic -r /srv/restic-repo backup --stdin
```
However, even in case of failure of the first command, an incomplete snapshot will be created. Better to use `--stdin-from-command` instead.