sync.sh: add some code in functions

This commit is contained in:
2022-05-10 20:38:34 +02:00
parent 9ea7e38759
commit 28780ef13c

View File

@@ -142,13 +142,8 @@
############################################################################### ###############################################################################
######################### options default values ######################### options default values
############################################################################### ###############################################################################
<<<<<<< Updated upstream
# These ones can be set by command-line.
# They can also be overwritten in configuration file (prefered option).
=======
# These ones can be set by command-line and in configuration file. # These ones can be set by command-line and in configuration file.
# priority is given to configuration file. # priority is given to configuration file.
>>>>>>> Stashed changes
YEARLY=n # (-ay) yearly backup (y/n) YEARLY=n # (-ay) yearly backup (y/n)
MONTHLY=n # (-am) monthly backup (y/n) MONTHLY=n # (-am) monthly backup (y/n)
WEEKLY=n # (-aw) weekly backup (y/n) WEEKLY=n # (-aw) weekly backup (y/n)
@@ -378,59 +373,68 @@ exit_handler() {
############################################################################### ###############################################################################
######################### Options/Environment setup ######################### Options/Environment setup
############################################################################### ###############################################################################
# command-line options parsing. # command-line parsing / configuration file read.
OPTIND=1 parse_opts() {
shopt -s extglob # to parse "-a" option OPTIND=0
while getopts a:DfnruvzZ todo; do shopt -s extglob # to parse "-a" option
case "$todo" in while getopts a:DfnruvzZ todo; do
a) # we use US (Unit Separator, 0x1F, control-_) as separator case "$todo" in
# next line will add US before each char (including 1st one) a) # we use US (Unit Separator, 0x1F, control-_) as separator
IFS=$'\x1F' read -ra periods <<< "${OPTARG//?()/$'\x1F'}" # next line will add US before each char (including 1st one)
# we skip 1st (empty) ellement of array IFS=$'\x1F' read -ra periods <<< "${OPTARG//?()/$'\x1F'}"
for period in "${periods[@]:1}"; do # we skip 1st (empty) ellement of array
case "$period" in for period in "${periods[@]:1}"; do
d) DAILY=y;; case "$period" in
w) WEEKLY=y;; d) DAILY=y;;
m) MONTHLY=y;; w) WEEKLY=y;;
y) YEARLY=y;; m) MONTHLY=y;;
*) printf '%s: unknown period "%s"\n' "$CMDNAME" "$period" y) YEARLY=y;;
usage *) printf '%s: unknown period "%s"\n' "$CMDNAME" "$period"
esac usage
done esac
;; done
f) FILTERLNK=y;; ;;
r) RESUME=y;; f) FILTERLNK=y;;
n) MAILTO="";; r) RESUME=y;;
z) COMPRESS=-y;; # rsync compression. Depends on net/CPU perfs n) MAILTO="";;
u) NUMID="--numeric-ids";; z) COMPRESS=-y;; # rsync compression. Depends on net/CPU perfs
D) DEBUG=y;; u) NUMID="--numeric-ids";;
Z) ZIPMAIL="cat";; D) DEBUG=y;;
*) usage;; Z) ZIPMAIL="cat";;
esac *) usage;;
done esac
# Now check remaining argument (configuration file), which should be unique, done
# and read the file. # Now check remaining argument (configuration file), which should be unique,
shift $((OPTIND - 1)) # and read the file.
(( $# != 1 )) && usage shift $((OPTIND - 1))
CONFIG="$1" (( $# != 1 )) && usage
LOCKDIR=".sync-$SERVER-${CONFIG##*/}.lock" CONFIG="$1"
if [[ ! -r "$CONFIG" ]]; then LOCKDIR=".sync-$SERVER-${CONFIG##*/}.lock"
printf "%s: Cannot open $CONFIG file. Exiting.\n" "$CMDNAME"
exit 1 if [[ ! -r "$CONFIG" ]]; then
fi printf "%s: Cannot open $CONFIG file. Exiting.\n" "$CMDNAME"
# shellcheck source=/dev/null exit 1
source "$CONFIG" fi
# shellcheck source=/dev/null
source "$CONFIG"
}
parse_opts "$@"
# we set backups to be done if none has been set yet (i.e. none is "y"). # we set backups to be done if none has been set yet (i.e. none is "y").
# Note: we use the form +%-d to avoid zero padding : # Note: we use the form +%-d to avoid zero padding :
# for bash, starting with 0 => octal => 08 is invalid # for bash, starting with 0 => octal => 08 is invalid
if ! [[ "$DAILY$WEEKLY$MONTHLY$YEARLY" =~ .*y.* ]]; then adjust_targets() {
(( $(date +%u) == 7 )) && WEEKLY=y if ! [[ "$DAILY$WEEKLY$MONTHLY$YEARLY" =~ .*y.* ]]; then
(( $(date +%-d) == 1 )) && MONTHLY=y (( $(date +%u) == 7 )) && WEEKLY=y
(( $(date +%-d) == 1 && $(date +%-m) == 1 )) && YEARLY=y (( $(date +%-d) == 1 )) && MONTHLY=y
DAILY=y (( $(date +%-d) == 1 && $(date +%-m) == 1 )) && YEARLY=y
fi DAILY=y
fi
}
adjust_targets
# After these basic initializations, errors will be managed by the # After these basic initializations, errors will be managed by the
# following handler. It is better to do this before the redirections below. # following handler. It is better to do this before the redirections below.
@@ -485,11 +489,7 @@ log -n "Compression: " && [[ $ZIPMAIL = gzip ]] && log "gzip" || log "none"
declare -a cmdavail=() declare -a cmdavail=()
log -n "Checking for commands : " log -n "Checking for commands : "
for cmd in rsync base64 sendmail gzip; do for cmd in rsync base64 sendmail gzip; do
<<<<<<< Updated upstream
log -n "%s..." "$cmd..."
=======
log -n "%s..." "$cmd" log -n "%s..." "$cmd"
>>>>>>> Stashed changes
if type -P "$cmd" > /dev/null; then if type -P "$cmd" > /dev/null; then
log -n "ok " log -n "ok "
else else