feat(resticw): Add a switch to run a diff on the latest snapshot (#129)
There's no obvious way to simply run a restic diff for the latest snapshot against the previous one. This simplifies the task to inspect the last snapshot changes. The user only needs to run `sudo resticw --diff-latest` (optionally specifying a profile).
This commit is contained in:
75
bin/resticw
75
bin/resticw
@@ -1,19 +1,24 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# @generated
|
# @generated
|
||||||
|
|
||||||
DOC="A little wrapper over restic just to handle profiles and environment loading.
|
DOC="A little wrapper over restic just to handle profiles and environment loading, with small extensions.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
resticw [options] <restic_arguments_line>...
|
resticw [--profile <name>] <restic_arguments_line>...
|
||||||
|
resticw [--profile <name>] --diff-latest
|
||||||
|
|
||||||
The <restic_arguments_line> is just the regular unwrapped restic command arguments, e.g. stats latest
|
The <restic_arguments_line> is just the regular unwrapped restic command arguments, e.g. \`stats latest\`.
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-p --profile=<name> Specify the profile to load or use default [default: default].
|
-p --profile=<name> Specify the profile to load or use default [default: default].
|
||||||
|
--diff-latest Show latest snapshot changes: Runs \`restic diff\` after finding the latest 2 snapshots.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
resticw --profile profileA snapshots
|
resticw --profile profileA snapshots
|
||||||
resticw stats latest # this will use the profile: default
|
resticw stats latest # this will use the profile: default
|
||||||
|
resticw -p profileB --diff-latest
|
||||||
|
|
||||||
|
💡 You may need to run it with sudo to source the profile environment.
|
||||||
"
|
"
|
||||||
|
|
||||||
# The following argument parser is generated with docopt.sh from the above docstring.
|
# The following argument parser is generated with docopt.sh from the above docstring.
|
||||||
@@ -86,35 +91,51 @@ local node_idx; ((testdepth++)) || true; for node_idx in "$@"; do
|
|||||||
if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true
|
if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true
|
||||||
return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then
|
return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then
|
||||||
left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi
|
left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi
|
||||||
return 0; }; optional() { local node_idx; for node_idx in "$@"; do
|
return 0; }; either() { local initial_left=("${left[@]}"); local best_match_idx
|
||||||
"node_$node_idx"; done; return 0; }; oneormore() { local i=0
|
local match_count; local node_idx; ((testdepth++)) || true
|
||||||
local prev=${#left[@]}; while "node_$1"; do ((i++)) || true
|
for node_idx in "$@"; do if "node_$node_idx"; then
|
||||||
|
if [[ -z $match_count || ${#left[@]} -lt $match_count ]]; then
|
||||||
|
best_match_idx=$node_idx; match_count=${#left[@]}; fi; fi
|
||||||
|
left=("${initial_left[@]}"); done; ((testdepth--)) || true
|
||||||
|
if [[ -n $best_match_idx ]]; then "node_$best_match_idx"; return 0; fi
|
||||||
|
left=("${initial_left[@]}"); return 1; }; optional() { local node_idx
|
||||||
|
for node_idx in "$@"; do "node_$node_idx"; done; return 0; }; oneormore() {
|
||||||
|
local i=0; local prev=${#left[@]}; while "node_$1"; do ((i++)) || true
|
||||||
[[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done
|
[[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done
|
||||||
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; value() { local i
|
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; switch() { local i
|
||||||
for i in "${!left[@]}"; do local l=${left[$i]}
|
for i in "${!left[@]}"; do local l=${left[$i]}
|
||||||
if [[ ${parsed_params[$l]} = "$2" ]]; then
|
if [[ ${parsed_params[$l]} = "$2" ]]; then
|
||||||
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
|
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
|
||||||
|
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
|
||||||
|
eval "((var_$1++))" || true; else eval "var_$1=true"; fi; return 0; fi; done
|
||||||
|
return 1; }; value() { local i; for i in "${!left[@]}"; do local l=${left[$i]}
|
||||||
|
if [[ ${parsed_params[$l]} = "$2" ]]; then
|
||||||
|
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
|
||||||
[[ $testdepth -gt 0 ]] && return 0; local value
|
[[ $testdepth -gt 0 ]] && return 0; local value
|
||||||
value=$(printf -- "%q" "${parsed_values[$l]}"); if [[ $3 = true ]]; then
|
value=$(printf -- "%q" "${parsed_values[$l]}"); if [[ $3 = true ]]; then
|
||||||
eval "var_$1+=($value)"; else eval "var_$1=$value"; fi; return 0; fi; done
|
eval "var_$1+=($value)"; else eval "var_$1=$value"; fi; return 0; fi; done
|
||||||
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
|
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
|
||||||
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
|
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
|
||||||
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
|
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
|
||||||
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:0:450}
|
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:0:751}
|
||||||
usage=${DOC:79:53}; digest=dc31d; shorts=(-p); longs=(--profile); argcounts=(1)
|
usage=${DOC:102:105}; digest=a9466; shorts=(-p '')
|
||||||
node_0(){ value __profile 0; }; node_1(){ value _restic_arguments_line_ a true
|
longs=(--profile --diff-latest); argcounts=(1 0); node_0(){ value __profile 0; }
|
||||||
}; node_2(){ optional 0; }; node_3(){ optional 2; }; node_4(){ oneormore 1; }
|
node_1(){ switch __diff_latest 1; }; node_2(){
|
||||||
node_5(){ required 3 4; }; node_6(){ required 5; }; cat <<<' docopt_exit() {
|
value _restic_arguments_line_ a true; }; node_3(){ optional 0; }; node_4(){
|
||||||
[[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:79:53}" >&2; exit 1
|
oneormore 2; }; node_5(){ required 3 4; }; node_6(){ required 3 1; }; node_7(){
|
||||||
}'; unset var___profile var__restic_arguments_line_; parse 6 "$@"
|
either 5 6; }; node_8(){ required 7; }; cat <<<' docopt_exit() {
|
||||||
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__profile" \
|
[[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:102:105}" >&2
|
||||||
"${prefix}_restic_arguments_line_"
|
exit 1; }'; unset var___profile var___diff_latest var__restic_arguments_line_
|
||||||
|
parse 8 "$@"; local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__profile" \
|
||||||
|
"${prefix}__diff_latest" "${prefix}_restic_arguments_line_"
|
||||||
eval "${prefix}"'__profile=${var___profile:-default}'
|
eval "${prefix}"'__profile=${var___profile:-default}'
|
||||||
|
eval "${prefix}"'__diff_latest=${var___diff_latest:-false}'
|
||||||
if declare -p var__restic_arguments_line_ >/dev/null 2>&1; then
|
if declare -p var__restic_arguments_line_ >/dev/null 2>&1; then
|
||||||
eval "${prefix}"'_restic_arguments_line_=("${var__restic_arguments_line_[@]}")'
|
eval "${prefix}"'_restic_arguments_line_=("${var__restic_arguments_line_[@]}")'
|
||||||
else eval "${prefix}"'_restic_arguments_line_=()'; fi; local docopt_i=1
|
else eval "${prefix}"'_restic_arguments_line_=()'; fi; local docopt_i=1
|
||||||
[[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do
|
[[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do
|
||||||
declare -p "${prefix}__profile" "${prefix}_restic_arguments_line_"; done; }
|
declare -p "${prefix}__profile" "${prefix}__diff_latest" \
|
||||||
|
"${prefix}_restic_arguments_line_"; done; }
|
||||||
# docopt parser above, complete command for generating this parser is `docopt.sh resticw`
|
# docopt parser above, complete command for generating this parser is `docopt.sh resticw`
|
||||||
|
|
||||||
# Parse arguments - See https://github.com/andsens/docopt.sh for the magic :)
|
# Parse arguments - See https://github.com/andsens/docopt.sh for the magic :)
|
||||||
@@ -132,6 +153,16 @@ ENV_DIR=$INSTALL_PREFIX/etc/restic
|
|||||||
ERR_NO_SUCH_PROFILE=2
|
ERR_NO_SUCH_PROFILE=2
|
||||||
ERR_PROFILE_NO_READ_PERM=3
|
ERR_PROFILE_NO_READ_PERM=3
|
||||||
|
|
||||||
|
# Compute the latest 2 snapshots and run the diff
|
||||||
|
latestSnapshotDiff() {
|
||||||
|
restic snapshots --tag "$RESTIC_BACKUP_TAG" --latest 2 --compact \
|
||||||
|
| grep -Ei "^[abcdef0-9]{8} " \
|
||||||
|
| awk '{print $1}' \
|
||||||
|
| tail -2 \
|
||||||
|
| tr '\n' ' ' \
|
||||||
|
| xargs restic diff
|
||||||
|
}
|
||||||
|
|
||||||
# shellcheck disable=SC2154
|
# shellcheck disable=SC2154
|
||||||
profile_file="${ENV_DIR}/${__profile}.env.sh"
|
profile_file="${ENV_DIR}/${__profile}.env.sh"
|
||||||
|
|
||||||
@@ -143,6 +174,12 @@ if [[ ! -r "$profile_file" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "‣ Using profile: ${__profile} -- (${profile_file})\n"
|
echo -e "‣ Using profile: ${__profile} -- (${profile_file})\n"
|
||||||
|
# shellcheck disable=SC1090
|
||||||
|
source "$profile_file"
|
||||||
|
|
||||||
# shellcheck disable=SC2154,SC1090
|
# shellcheck disable=SC2154
|
||||||
source "$profile_file" && restic "${_restic_arguments_line_[@]}"
|
if [[ "${__diff_latest}" == true ]]; then
|
||||||
|
latestSnapshotDiff
|
||||||
|
else
|
||||||
|
restic "${_restic_arguments_line_[@]}"
|
||||||
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user