restic can be used as a CLI utility that you run manually when you want to create backups, but you can also script its usage, making it great for scheduled backups. The documentation in this section is not complete, but the Restic docs have an entry for scripting restic that is worth reading.
The documentation on this page assumes you are using my Restic setup docs. If not, you should be able to adapt the scripts for your own setup.
Warning
This documentation is not complete. I am starting by providing example scripts, and will fill in details/write more generic scripts over time. This page is incomplete until this message is gone.
Examples
Generic backup script
If you did not follow my Restic setup guide, or you want to create a more 'self contained' script that does not rely on it, you can use something like the script below.
#!/bin/bash## Set default varsSRC_DIR=""RESTIC_REPO_FILE=""RESTIC_PW_FILE=""DRY_RUN=""## Create array of exclude files to pass to resticEXCLUDE_FILES=()## Create array of exclude patternss to pass to resticEXCLUDE_PATTERNS=()## Define -h/--help functionfunctionprint_help(){cat<<EOFUsage: $(basename "$0") [OPTIONS]Restic backup script with support for multiple exclude files and patterns.OPTIONS: -s, --backup-src DIR Source directory to back up (required) -r, --repo-file PATH Path to file with restic repository path (required) -p, --password-file PATH Path to restic password file (required) --exclude-file PATH Path to a file containing exclude patterns (can be used multiple times) --exclude-pattern PATTERN Single exclude pattern (can be used multiple times) --dry-run Print the restic command that would be run, but do not execute -h, --help Display this help and exitEXAMPLES: $(basename "$0") -s /home/username -r /etc/restic/repo -p /etc/restic/pw \ --exclude-file ~/.restic/ignores/default --exclude-pattern "*.cache" --dry-runNOTES: - You can specify --exclude-file and --exclude-pattern multiple times to pass multiple values. - Run with --dry-run to see command without running it.EOF}## Parse CLI argswhile[[$#-gt0]];docase"$1"in-s|--backup-src)if[[-z$2]];thenecho"[ERROR] --backup-src provided but no path given."print_help
exit1fiSRC_DIR="$2"shift2;;-r|--repo-file)if[[-z$2]];thenecho"[ERROR] --repo-file provided but no path given."print_help
exit1fiRESTIC_REPO_FILE="$2"shift2;;-p|--password-file)if[[-z$2]];thenecho"[ERROR] --password-file provided but no path given."print_help
exit1fiRESTIC_PW_FILE="$2"shift2;;--exclude-file)if[[-n"$2"&&"$2"!=--*]];thenEXCLUDE_FILES+=("$2")shift2elseecho"[ERROR] --exclude-file provided but no path given."print_help
exit1fi;;--exclude-pattern)if[[-n"$2"&&"$2"!=--*]];thenEXCLUDE_PATTERNS+=("$2")shift2elseecho"[ERROR] --exclude-pattern provided but no path given."print_help
exit1fi;;--dry-run)DRY_RUN="true"shift;;-h|--help)print_help
exit0;;*)echo"[ERROR] Unrecognized flag: $1"print_help
exit1;;esacdone## Export env vars for scriptexportRESTIC_REPOSITORY_FILE="$RESTIC_REPO_FILE"exportRESTIC_PASSWORD_FILE="$RESTIC_PW_FILE"## Build commandcmd=(resticbackup"$SRC_DIR")## Append all exclude filesforexcl_filein"${EXCLUDE_FILES[@]}";docmd+=(--exclude-file"$excl_file")done## Append all exclude patternsforexcl_patternin"${EXCLUDE_PATTERNS[@]}";docmd+=(--exclude"$excl_pattern")done## Print or run commandif[[-z"$DRY_RUN"]]||[["$DRY_RUN"==""]];thenecho"Running restic cleanup command: $cmd"## Run the command"$cmd[@]"if[[$?-ne0]];thenecho"[ERROR] Failed to run restic cleanup command."exit1elseecho"Restic cleanup performed successfully"exit0fielseecho"[DRY RUN] Would run restic cleanup command: $cmd[@]"exit0fi
#!/bin/bashDOT_RESTIC="$HOME/.restic"DOT_RESTICRC="$DOT_RESTIC/.resticrc"RESTIC_IGNORES_DIR="$DOT_RESTIC/ignores"RESTIC_PASSWORDS_DIR="$DOT_RESTIC/passwords"if[[!-d"${DOT_RESTIC}"]];thenecho"[ERROR] Could not find restic directory at '${DOT_RESTIC}'"exit1fiif[[!-d"${RESTIC_IGNORES_DIR}"]];thenecho"[ERROR] Could not find restic exclude files directory at path '${RESTIC_IGNORES_DIR}'"exit1fiif[[!-d"${RESTIC_PASSWORDS_DIR}"]];thenecho"[ERROR] Could not find restic password files directory at path '${RESTIC_PASSWORDS_DIR}'"exit1fi## Source ~/.restic/.resticrcif[[!-f"${DOT_RESTICRC}"]];thenecho"[ERROR] Could not find ~/.restic/.resticrc"exit1fi."${DOT_RESTICRC}"## Ensure ~/.restic/.resticrc values loaded correctlyif[[-z"${RESTIC_REPOSITORY_FILE}"]]||[["${RESTIC_REPOSITORY_FILE}"==""]];thenecho"[ERROR] RESTIC_REPOSITORY_FILE should not be empty. Ensure ~/.restic/.resticrc defines this variable"exit1fiif[[-z"${RESTIC_PASSWORD_FILE}"]]||[["${RESTIC_PASSWORD_FILE}"==""]];thenecho"[ERROR] RESTIC_PASSWORD_FILE should not be empty. Ensure ~/.restic/.resticrc defines this variable"exit1fi## Ensure restic files existif[[!-f"${RESTIC_REPOSITORY_FILE}"]];thenecho"[ERROR] Could not find Restic repository file at path '$RESTIC_REPOSITORY_FILE'"exit1fiif[[!-f"${RESTIC_PASSWORD_FILE}"]];thenecho"[ERROR] Could not find Restic password file at path '$RESTIC_PASSWORD_FILE'"exit1fi## Set default varsDRY_RUN=""## Parse script inputswhile[[$#-gt0]];docase"$1"in--dry-run)DRY_RUN="true"shift1;;*)echo"[ERROR] Unknown argument: $1"shift1;;esacdone## Build restic commandcmd="restic backup /home/username --exclude-file \"${RESTIC_IGNORES_DIR}/default\""## Run or print commandif[[-z"$DRY_RUN"]]||[["$DRY_RUN"==""]];thenecho"Running restic backup command: $cmd"eval"${cmd}"if[[$?-ne0]];thenecho"[ERROR] Failed to create restic backup."exit1elseecho"Created restic snapshot of /home/jack"exit0fielseecho"[DRY RUN] Would run restic backup command: $cmd"exit0fi