sync.sh: refactoring, add -m option

This commit is contained in:
2022-05-11 15:29:24 +02:00
parent 28780ef13c
commit 6df830554d

View File

@@ -11,6 +11,7 @@
# #
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html> # SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
# #
#%MAN_BEGIN%
# NAME # NAME
# sync.sh - a backup utility using ssh/rsync facilities. # sync.sh - a backup utility using ssh/rsync facilities.
# #
@@ -18,7 +19,7 @@
# sync.sh [-ymwdznt] CONFIG # sync.sh [-ymwdznt] CONFIG
# #
# DESCRIPTION # DESCRIPTION
# Performs a backup to a local or remote destination, keeping different # Perform a backup to a local or remote destination, keeping different
# versions (daily, weekly, monthly, yearly). All options can be set in # versions (daily, weekly, monthly, yearly). All options can be set in
# CONFIG file, which is mandatory. # CONFIG file, which is mandatory.
# The synchronization is make with rsync(1), and only files changed or # The synchronization is make with rsync(1), and only files changed or
@@ -50,6 +51,8 @@
# bash's -x option) when some errors are difficult to track. # bash's -x option) when some errors are difficult to track.
# -f # -f
# Filter some rsync output, such as hard and soft links, dirs, etc. # Filter some rsync output, such as hard and soft links, dirs, etc.
# -m
# Display a "man-like" description and exit.
# -n # -n
# Do not send mail report (which is the default if MAILTO environment # Do not send mail report (which is the default if MAILTO environment
# is set). Practically, this option only unsets MAILTO. # is set). Practically, this option only unsets MAILTO.
@@ -100,6 +103,10 @@
# CONFIGURATION FILE # CONFIGURATION FILE
# TODO: Write documentation. See example (sync-conf-example.sh). # TODO: Write documentation. See example (sync-conf-example.sh).
# #
# AUTHOR
# Bruno Raoult.
#
#%MAN_END%
# BUGS # BUGS
# Many. # Many.
# This was written for a "terastation" NAS server, which is a kind of # This was written for a "terastation" NAS server, which is a kind of
@@ -134,10 +141,6 @@
# GNU's getopt(1) could be an option, but not available everywhere # GNU's getopt(1) could be an option, but not available everywhere
# (for example on MacOS). Likely impossible to keep this script portable. # (for example on MacOS). Likely impossible to keep this script portable.
# #
# AUTHOR
# Bruno Raoult.
#
#
############################################################################### ###############################################################################
######################### options default values ######################### options default values
@@ -180,6 +183,7 @@ function aftersync () {
# understand exactly what you do. # understand exactly what you do.
# Some variables were moved into the code (example: in the log() function), # Some variables were moved into the code (example: in the log() function),
# for practical reasons, the absence of associative arrays being one of them. # for practical reasons, the absence of associative arrays being one of them.
SCRIPT="$0" # full path to script
CMDNAME=${0##*/} # script name CMDNAME=${0##*/} # script name
PID=$$ # current pricess PID PID=$$ # current pricess PID
LOCKED=n # indicates if we created lock file. LOCKED=n # indicates if we created lock file.
@@ -190,8 +194,12 @@ STARTTIME=$(date +%s) # time since epoch in seconds
############################################################################### ###############################################################################
######################### helper functions ######################### helper functions
############################################################################### ###############################################################################
man() {
sed -n '/^#%MAN_BEGIN%/,/^#%MAN_END%$/{//!s/^#[ ]\{0,1\}//p}' "$SCRIPT" | more
}
usage () { usage () {
printf "usage: %s [-a PERIOD][-DfnruvzZ] config-file\n" "$CMDNAME" printf "usage: %s [-a PERIOD][-DfmnruvzZ] config-file\n" "$CMDNAME"
exit 1 exit 1
} }
@@ -377,9 +385,10 @@ exit_handler() {
parse_opts() { parse_opts() {
OPTIND=0 OPTIND=0
shopt -s extglob # to parse "-a" option shopt -s extglob # to parse "-a" option
while getopts a:DfnruvzZ todo; do while getopts a:DfmnruvzZ todo; do
case "$todo" in case "$todo" in
a) # we use US (Unit Separator, 0x1F, control-_) as separator a)
# we use US (Unit Separator, 0x1F, control-_) as separator
# next line will add US before each char (including 1st one) # next line will add US before each char (including 1st one)
IFS=$'\x1F' read -ra periods <<< "${OPTARG//?()/$'\x1F'}" IFS=$'\x1F' read -ra periods <<< "${OPTARG//?()/$'\x1F'}"
# we skip 1st (empty) ellement of array # we skip 1st (empty) ellement of array
@@ -394,14 +403,34 @@ parse_opts() {
esac esac
done done
;; ;;
f) FILTERLNK=y;; f)
r) RESUME=y;; FILTERLNK=y
n) MAILTO="";; ;;
z) COMPRESS=-y;; # rsync compression. Depends on net/CPU perfs r)
u) NUMID="--numeric-ids";; RESUME=y
D) DEBUG=y;; ;;
Z) ZIPMAIL="cat";; m)
*) usage;; man
exit 0
;;
n)
MAILTO=""
;;
z)
COMPRESS=-y # rsync compression. Depends on net/CPU perfs
;;
u)
NUMID="--numeric-ids"
;;
D)
DEBUG=y
;;
Z)
ZIPMAIL="cat"
;;
*)
usage
;;
esac esac
done done
# Now check remaining argument (configuration file), which should be unique, # Now check remaining argument (configuration file), which should be unique,