Fix arguments by using arrays to buildup

This commit is contained in:
Erik Westrup
2022-02-01 19:24:38 +01:00
parent b23d552f0b
commit dd073dfc73
5 changed files with 43 additions and 30 deletions
+4
View File
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [3.0.2] - 2022-02-01
### Fixed
- Use arrays to build up command lines. When fixing shellcheck errors, quotes would disable expansion on e.g. $RESTIC_BACKUP_PATHS
## [3.0.1] - 2022-02-01 ## [3.0.1] - 2022-02-01
### Fixed ### Fixed
- Environment variable assertion should allow empty values e.g. `RESTIC_BACKUP_EXTRA_ARGS` - Environment variable assertion should allow empty values e.g. `RESTIC_BACKUP_EXTRA_ARGS`
+2 -1
View File
@@ -20,7 +20,8 @@ export B2_ACCOUNT_KEY="<b2-application-key>" # TODO fill with your applicationKe
# How many network connections to set up to B2. Default is 5. # How many network connections to set up to B2. Default is 5.
export B2_CONNECTIONS=10 export B2_CONNECTIONS=10
# Extra args to restic-backup. This is empty here and profiles can override this after sourcing this file. # Optional extra space-separated args to restic-backup.
# This is empty here and profiles can override this after sourcing this file.
export RESTIC_BACKUP_EXTRA_ARGS= export RESTIC_BACKUP_EXTRA_ARGS=
# Verbosity level from 0-3. 0 means no --verbose. # Verbosity level from 0-3. 0 means no --verbose.
+4 -3
View File
@@ -15,10 +15,11 @@ source /etc/restic/_global.env
export RESTIC_REPOSITORY="b2:<b2-repo-name>" # TODO fill with your repo name export RESTIC_REPOSITORY="b2:<b2-repo-name>" # TODO fill with your repo name
# What to backup (paths our mountpoints) e.g. "/ /boot /home /mnt/media". # What to backup. Colon-separated paths e.g. to different mountpoints "/home:/mnt/usb_disk".
# To backup only your home directory, set "/home/your-user" # To backup only your home directory, set "/home/your-user"
export RESTIC_BACKUP_PATHS="" # TODO fill conveniently with one or multiple paths export RESTIC_BACKUP_PATHS="" # TODO fill conveniently with one or multiple paths
# Example below of how to dynamically add a path that is mounted e.g. external USB disk. # Example below of how to dynamically add a path that is mounted e.g. external USB disk.
# restic does not fail if a specified path is not mounted, but it's nicer to only add if they are available. # restic does not fail if a specified path is not mounted, but it's nicer to only add if they are available.
#test -d /mnt/media && RESTIC_BACKUP_PATHS+=" /mnt/media" #test -d /mnt/media && RESTIC_BACKUP_PATHS+=" /mnt/media"
@@ -33,8 +34,8 @@ export RESTIC_RETENTION_WEEKS=16
export RESTIC_RETENTION_MONTHS=18 export RESTIC_RETENTION_MONTHS=18
export RESTIC_RETENTION_YEARS=3 export RESTIC_RETENTION_YEARS=3
# Optional extra arguments to restic-backup. # Optional extra space-separated arguments to restic-backup.
# Example: Add two additional exclude files to the global one in RESTIC_PASSWORD_FILE. # Example: Add two additional exclude files to the global one in RESTIC_PASSWORD_FILE.
#RESTIC_BACKUP_EXTRA_ARGS="--exclude-file /path/to/extra/exclude/file/a /path/to/extra/exclude/file/b" #RESTIC_BACKUP_EXTRA_ARGS="--exclude-file /path/to/extra/exclude/file/a --exclude-file /path/to/extra/exclude/file/b"
# Example: exclude all directories that have a .git/ directory inside it. # Example: exclude all directories that have a .git/ directory inside it.
#RESTIC_BACKUP_EXTRA_ARGS="--exclude-if-present .git" #RESTIC_BACKUP_EXTRA_ARGS="--exclude-if-present .git"
+23 -17
View File
@@ -11,6 +11,17 @@
# Exit on error, unset var, pipe failure # Exit on error, unset var, pipe failure
set -euo pipefail set -euo pipefail
# Clean up lock if we are killed.
# If killed by systemd, like $(systemctl stop restic), then it kills the whole cgroup and all it's subprocesses.
# However if we kill this script ourselves, we need this trap that kills all subprocesses manually.
exit_hook() {
echo "In exit_hook(), being killed" >&2
jobs -p | xargs kill
restic unlock
}
trap exit_hook INT TERM
# Assert that all needed environment variables are set. # Assert that all needed environment variables are set.
# TODO in future if this grows, move this to a restic_lib.sh # TODO in future if this grows, move this to a restic_lib.sh
assert_envvars() { assert_envvars() {
@@ -29,25 +40,20 @@ assert_envvars \
RESTIC_RETENTION_DAYS RESTIC_RETENTION_MONTHS RESTIC_RETENTION_WEEKS RESTIC_RETENTION_YEARS RESTIC_RETENTION_DAYS RESTIC_RETENTION_MONTHS RESTIC_RETENTION_WEEKS RESTIC_RETENTION_YEARS
# Clean up lock if we are killed. # Convert to arrays, as arrays should be used to build command lines. See https://github.com/koalaman/shellcheck/wiki/SC2086
# If killed by systemd, like $(systemctl stop restic), then it kills the whole cgroup and all it's subprocesses. IFS=':' read -ra backup_paths <<< "$RESTIC_BACKUP_PATHS"
# However if we kill this script ourselves, we need this trap that kills all subprocesses manually. IFS=' ' read -ra extra_args <<< "$RESTIC_BACKUP_EXTRA_ARGS"
exit_hook() {
echo "In exit_hook(), being killed" >&2
jobs -p | xargs kill
restic unlock
}
trap exit_hook INT TERM
# Set up exclude files: global + path-specific ones # Set up exclude files: global + path-specific ones
# NOTE that restic will fail the backup if not all listed --exclude-files exist. Thus we should only list them if they are really all available. # NOTE that restic will fail the backup if not all listed --exclude-files exist. Thus we should only list them if they are really all available.
## Global backup configuration. ## Global backup configuration.
exclusion_args="--exclude-file ${RESTIC_BACKUP_EXCLUDE_FILE}" exclusion_args=(--exclude-file "$RESTIC_BACKUP_EXCLUDE_FILE")
## Self-contained backup files per backup path. E.g. having an USB disk at /mnt/media in RESTIC_BACKUP_PATHS, ## Self-contained backup exclusion files per backup path. E.g. having an USB disk at /mnt/media in RESTIC_BACKUP_PATHS,
# a file /mnt/media/.backup_exclude.txt will automatically be detected and used: # then a file /mnt/media/.backup_exclude.txt will automatically be detected and used:
for backup_path in "${RESTIC_BACKUP_PATHS[@]}"; do for backup_path in "${backup_paths[@]}"; do
if [ -f "$backup_path/.backup_exclude.txt" ]; then if [ -f "$backup_path/.backup_exclude.txt" ]; then
exclusion_args+=" --exclude-file $backup_path/.backup_exclude.txt" exclusion_args=("${exclusion_args[@]}" --exclude-file "$backup_path/.backup_exclude.txt")
fi fi
done done
@@ -69,9 +75,9 @@ restic backup \
--one-file-system \ --one-file-system \
--tag "$RESTIC_BACKUP_TAG" \ --tag "$RESTIC_BACKUP_TAG" \
--option b2.connections="$B2_CONNECTIONS" \ --option b2.connections="$B2_CONNECTIONS" \
"$exclusion_args" \ "${exclusion_args[@]}" \
"$RESTIC_BACKUP_EXTRA_ARGS" \ "${extra_args[@]}" \
"$RESTIC_BACKUP_PATHS" & "${backup_paths[@]}" &
wait $! wait $!
# Dereference and delete/prune old backups. # Dereference and delete/prune old backups.
+10 -9
View File
@@ -5,6 +5,16 @@
# Exit on error, unset var, pipe failure # Exit on error, unset var, pipe failure
set -euo pipefail set -euo pipefail
# Clean up lock if we are killed.
# If killed by systemd, like $(systemctl stop restic), then it kills the whole cgroup and all it's subprocesses.
# However if we kill this script ourselves, we need this trap that kills all subprocesses manually.
exit_hook() {
echo "In exit_hook(), being killed" >&2
jobs -p | xargs kill
restic unlock
}
trap exit_hook INT TERM
# Assert that all needed environment variables are set. # Assert that all needed environment variables are set.
assert_envvars() { assert_envvars() {
local varnames=("$@") local varnames=("$@")
@@ -19,15 +29,6 @@ assert_envvars \
B2_ACCOUNT_ID B2_ACCOUNT_KEY B2_CONNECTIONS \ B2_ACCOUNT_ID B2_ACCOUNT_KEY B2_CONNECTIONS \
RESTIC_PASSWORD_FILE RESTIC_REPOSITORY RESTIC_VERBOSITY_LEVEL RESTIC_PASSWORD_FILE RESTIC_REPOSITORY RESTIC_VERBOSITY_LEVEL
# Clean up lock if we are killed.
# If killed by systemd, like $(systemctl stop restic), then it kills the whole cgroup and all it's subprocesses.
# However if we kill this script ourselves, we need this trap that kills all subprocesses manually.
exit_hook() {
echo "In exit_hook(), being killed" >&2
jobs -p | xargs kill
restic unlock
}
trap exit_hook INT TERM
# Remove locks from other stale processes to keep the automated backup running. # Remove locks from other stale processes to keep the automated backup running.
# NOTE nope, don't unlock like restic_backup.sh. restic_backup.sh should take precedence over this script. # NOTE nope, don't unlock like restic_backup.sh. restic_backup.sh should take precedence over this script.