From a8cadc298eb84f5f23d5b1929d1ad7d69dcd4ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Mon, 12 Apr 2021 15:41:18 +0200 Subject: [PATCH] Manage backup destination path with argument --- proxmox/backup.pve.content.sh | 44 +++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/proxmox/backup.pve.content.sh b/proxmox/backup.pve.content.sh index 39cd420..dfab1c6 100755 --- a/proxmox/backup.pve.content.sh +++ b/proxmox/backup.pve.content.sh @@ -24,7 +24,7 @@ readonly COLOR_DEBUG="${PURPLE}" usage() { # {{{ cat <<- EOF -usage: $PROGNAME [-d|-h] +usage: $PROGNAME [-d|-h|-l] Backup /etc/pve content @@ -38,6 +38,11 @@ OPTIONS : -h,--help Print this help message. + + -l,--local + Path to a local directory to store backup and override + default path (${DEFAULT_LOCAL_BKP_DIR}). + EOF } @@ -56,11 +61,34 @@ define_vars() { # {{{ ## If local_bkp_dir wasn't defined {{{ 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}" 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() { # {{{ @@ -68,6 +96,12 @@ main() { # {{{ ## Define all 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 0 ;; + -l|--local ) ## local directory to store backup + ## Move to the next argument + shift + ## Define local_bkp_dir + local_bkp_dir="${1}" + ;; * ) ## unknow option printf '%b\n' "${RED}Invalid option: ${1}${RESET}" printf '%b\n' "---"