Manage backup destination path with argument

This commit is contained in:
Jeremy Gardais 2021-04-12 15:41:18 +02:00
parent 72e9c7a98c
commit a8cadc298e
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 42 additions and 2 deletions

View File

@ -24,7 +24,7 @@ readonly COLOR_DEBUG="${PURPLE}"
usage() { # {{{ usage() { # {{{
cat <<- EOF cat <<- EOF
usage: $PROGNAME [-d|-h] usage: $PROGNAME [-d|-h|-l]
Backup /etc/pve content Backup /etc/pve content
@ -38,6 +38,11 @@ OPTIONS:
-h,--help -h,--help
Print this help message. Print this help message.
-l,--local
Path to a local directory to store backup and override
default path (${DEFAULT_LOCAL_BKP_DIR}).
EOF EOF
} }
@ -56,11 +61,34 @@ define_vars() { # {{{
## If local_bkp_dir wasn't defined {{{ ## If local_bkp_dir wasn't defined {{{
if [ -z "${local_bkp_dir}" ]; then if [ -z "${local_bkp_dir}" ]; then
## Use local host for sge_hostname ## Use default path to store backup
local_bkp_dir="${DEFAULT_LOCAL_BKP_DIR}" local_bkp_dir="${DEFAULT_LOCAL_BKP_DIR}"
fi fi
## }}} ## }}}
}
# }}}
is_directory_absent() { # {{{
local_directory_absent="${1}"
## Directory exists by default
return_is_directory_absent="1"
### Check if the directory exists
# shellcheck disable=SC2086
if find ${local_directory_absent} -type d > /dev/null 2>&1; then
return_is_directory_absent="1"
debug_message "is_directory_absent \
The directory ${RED}${local_directory_absent}${COLOR_DEBUG} exists."
else
return_is_directory_absent="0"
debug_message "is_directory_absent \
The directory ${RED}${local_directory_absent}${COLOR_DEBUG} doesn't exist."
fi
return "${return_is_directory_absent}"
} }
# }}} # }}}
main() { # {{{ main() { # {{{
@ -68,6 +96,12 @@ main() { # {{{
## Define all vars ## Define all vars
define_vars define_vars
## Verify if the destination directory is absent {{{
### AND create it
is_directory_absent "${local_bkp_dir}" \
&& mkdir -p -- ${local_bkp_dir}
## }}}
} }
# }}} # }}}
@ -101,6 +135,12 @@ if [ ! "${NBARGS}" -eq "0" ]; then
## Exit after help informations ## Exit after help informations
exit 0 exit 0
;; ;;
-l|--local ) ## local directory to store backup
## Move to the next argument
shift
## Define local_bkp_dir
local_bkp_dir="${1}"
;;
* ) ## unknow option * ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}" printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---" printf '%b\n' "---"