#!/bin/sh # This script will backup /etc/pve content : # 1. Make a tar to /etc/proxmox.pve/backup/ # This script can be call by a cronjob (eg. daily). # Vars {{{ readonly PROGNAME=$(basename "${0}") readonly PROGDIR=$(readlink -m $(dirname "${0}")) readonly ARGS="${*}" readonly NBARGS="${#}" [ -z "${DEBUG}" ] && DEBUG=1 readonly DEFAULT_LOCAL_BKP_DIR="/etc/proxmox.pve/backup" readonly TODAY_VAR=$(date +%Y%m%d) ## Colors readonly PURPLE='\033[1;35m' readonly RED='\033[0;31m' readonly RESET='\033[0m' readonly COLOR_DEBUG="${PURPLE}" # }}} usage() { # {{{ cat <<- EOF usage: $PROGNAME [-d|-h|-l] Backup /etc/pve content EXAMPLES : - Backup /etc/pve content to ${DEFAULT_LOCAL_BKP_DIR} directory ${PROGNAME} OPTIONS : -d,--debug Enable debug messages. -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 } # }}} debug_message() { # {{{ local_message="${1}" ## Print message if DEBUG is enable (=0) [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_message}" return 0 } # }}} define_vars() { # {{{ ## If local_bkp_dir wasn't defined {{{ if [ -z "${local_bkp_dir}" ]; then ## 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 test -d "${local_directory_absent}"; 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() { # {{{ ## Define all vars define_vars ## Verify if /etc/pve directory is absent {{{ ### Display an explicit error message ### AND exit with error code 1 is_directory_absent /etc/pve \ && printf '%b\n' "${RED}/etc/pve directory doesn't seems available. Are you sure you run this script on a Proxmox host?${RESET}" \ && exit 1 ## }}} ## Verify if the local destination directory is absent {{{ ### AND create it is_directory_absent "${local_bkp_dir}" \ && mkdir -p -- ${local_bkp_dir} ## }}} ## Create an archive of /etc/pve to $local_bkp_dir {{{ ### OR exit with error code 2 if it fails tar czf "${local_bkp_dir}/pve.${TODAY_VAR}.tar.gz" -C /etc/ pve/ \ || exit 2 ## }}} ## Create an hard link to pve.latest.tar.gz {{{ ### OR exit with error code 3 if it fails ln --force -- "${local_bkp_dir}/pve.${TODAY_VAR}.tar.gz" "${local_bkp_dir}/pve.latest.tar.gz" \ || exit 3 ## }}} } # }}} # Manage arguments # {{{ # This code can't be in a function due to argument management if [ ! "${NBARGS}" -eq "0" ]; then manage_arg="0" ## If the first argument is not an option if ! printf -- '%s' "${1}" | grep -q -E -- "^-+"; then ## Print help message and exit printf '%b\n' "${RED}Invalid option: ${1}${RESET}" printf '%b\n' "---" usage exit 1 fi # Parse all options (start with a "-") one by one while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do case "${1}" in -d|--debug ) ## debug DEBUG=0 ;; -h|--help ) ## help usage ## 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' "---" usage exit 1 ;; esac debug_message "Arguments management − \ ${RED}${1}${COLOR_DEBUG} option managed." ## Move to the next argument shift manage_arg=$((manage_arg+1)) done debug_message "Arguments management − \ ${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed." else debug_message "Arguments management − \ No arguments/options to manage." fi # }}} main exit 255