diff --git a/script-config.conf b/script-config.conf index b4825e0..ea2b314 100644 --- a/script-config.conf +++ b/script-config.conf @@ -104,24 +104,38 @@ CHECK_UPDATES=1 DEL_THRESHOLD=500 UP_THRESHOLD=500 -# This setting allows you to specify a pattern to exclude certain files when +# This setting allows you to specify a pattern to exclude certain files when # computing the counts of changed files using 'snapraid diff'. -# The patterns are based on the following regular expression: -# ^(?!.*(?:$IGNORE_PATTERN).*$).*$ -# This regex will exclude any file that matches the IGNORE_PATTERN. -# CAUTION: this is an advanced feature: pattern creation is not easy. -# You can test your pattern using this example: https://regex101.com/r/Igs4kX/1 -# Do not include the quotes used in the configuration as part of the pattern. -# The amount of "matches" shown by this example are the strings NOT captured -# by this rule. -# +# These use standard Bash "pathname expansion" (globs). +# +# WARNING: Please use this setting only if you really know what you are doing, +# it is meant for an advanced use. +# Do not use this to ignore files changing often, such as log files. +# This will cause the parity information to be out of sync and make recoveries +# impossible if those files are needed. +# Use the snapraid ignore configuration option instead. +# More details: https://www.snapraid.it/manual#7.7 +# https://www.snapraid.it/manual#8 +# +# Syntax: +# - the whole pattern must be enclosed in parentheses. +# - Multiple patterns are separated by spaces inside the parentheses. +# - Each pattern must be wrapped in double quotes. +# # Examples: -# IGNORE_PATTERN="Hello" -> All files containing "Hello" will be ignored. -# IGNORE_PATTERN="Backup/kopia" -> All files containing "Backup/kopia" in their -# path will be ignored. -# IGNORE_PATTERN="(Backup/kopia)|(Hello)" -> All files containing either -# "Backup/kopia" or "Hello" will be ignored. -IGNORE_PATTERN="" +# IGNORE_PATTERN=("*.tmp" "/var/cache/*" "*/backup/*") +# *.tmp → ignore all .tmp files +# /var/cache/* → ignore everything under /var/cache +# */backup/* → ignore any "backup" directory, anywhere +# +# Glob cheatsheet: +# * → matches anything (zero or more characters) +# ? → matches exactly one character +# [abc] → matches any of a, b, or c +# [!abc] → matches any character except a, b, or c +# +# More details: https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html +IGNORE_PATTERN=() # Allow a sync that would otherwise violate the delete threshold, but only # if the ratio of added to deleted files is greater than the value set. diff --git a/snapraid-aio-script.sh b/snapraid-aio-script.sh index f4d4c54..c5a086d 100644 --- a/snapraid-aio-script.sh +++ b/snapraid-aio-script.sh @@ -483,12 +483,11 @@ function sanity_check() { function get_counts() { EQ_COUNT=$(grep -wE '^ *[0-9]+ equal' "$TMP_OUTPUT" | sed 's/^ *//g' | cut -d ' ' -f1) - if [ $IGNORE_PATTERN ]; then - ADD_COUNT=$(grep -c -P "^add (?!.*(?:$IGNORE_PATTERN).*$).*$" "$TMP_OUTPUT") - UPDATE_COUNT=$(grep -c -P "^update (?!.*(?:$IGNORE_PATTERN).*$).*$" "$TMP_OUTPUT") - DEL_COUNT=$(grep -c -P "^remove (?!.*(?:$IGNORE_PATTERN).*$).*$" "$TMP_OUTPUT") - MOVE_COUNT=$(grep -c -P "^move (?!.*(?:$IGNORE_PATTERN).*$).*$" "$TMP_OUTPUT") - IGNORE_COUNT=$(grep -c -P ".*(?:$IGNORE_PATTERN).*" "$TMP_OUTPUT") + if [ ${#IGNORE_PATTERN[@]} -gt 0 ]; then + ADD_COUNT=$(count_actions_with_ignore add) + UPDATE_COUNT=$(count_actions_with_ignore update) + DEL_COUNT=$(count_actions_with_ignore remove) + MOVE_COUNT=$(count_actions_with_ignore move) else ADD_COUNT=$(grep -c -P '^add .+$' "$TMP_OUTPUT") UPDATE_COUNT=$(grep -c -P '^update .+$' "$TMP_OUTPUT") @@ -499,6 +498,25 @@ function get_counts() { # REST_COUNT=$(grep -wE '^ *[0-9]+ restored' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1) } +function count_actions_with_ignore() { + local action=$1 + local count=0 + + while read -r act path; do + [[ $act == "$action" ]] || continue + + # check ignore globs + for glob in "${IGNORE_PATTERN[@]}"; do + # shellcheck disable=SC2053 # glob matching is wanted here + [[ $path == $glob ]] && continue 2 + done + + ((count++)) + done < "$TMP_OUTPUT" + + echo "$count" +} + function sed_me(){ # Close the open output stream first, then perform sed and open a new tee # process and redirect output. We close stream because of the calls to new