I am writing a script for backup like this:
backup.sh:
dir="$1"
mode="$2"
delta="$3"
for file in "$dir/backup."*".$mode.tar.gz"; do
[ "$file" -nt "$ref" ] && ref="$file"
done
if [ "$delta" = "true" ]; then
delta_cmd=-N "'$ref'"
fi
backup_file="$dir/backup.$(date +%Y%m%d-%H%M%S).$mode.tar.gz"
case "$mode" in
config)
tar -cpzvf "$backup_file" $delta_cmd \
/etc \
/usr/local
;;
# still other modes here...
esac
I want to pass a single variable $delta_cmd to the tar command so that it tars all files or only delta files since last backup depending on the value of $delta.
The above code creates an error message and does not tar the delta files correctly if $delta is set to true. How to fix it?
P.S: The script would better be POSIX compatible.
eval) or overwriting$@(which is quite not compatible with the use of adelta_cmdstring) -- which would mean encapsulating the code in a function if you don't want to override the script-global value for that array. See BashFAQ #50 for a description of why string variables (as opposed to arrays) can't be safely used to store argument lists or commands, at mywiki.wooledge.org/BashFAQ/050$@way?ls, but a better-practices replacement for that code is also somewhat outside the scope of the question at hand.ls-- robustly find the most or least recent N files).