Improve script
Move code to functions. Manage arguments. debug and error dedicated messages. Exit with error message if a command fails.
This commit is contained in:
parent
5985288620
commit
70e7c27c72
|
@ -9,58 +9,218 @@
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# Vars {{{
|
# Vars {{{
|
||||||
DEBUG=1
|
PROGNAME=$(basename "${0}"); readonly PROGNAME
|
||||||
|
PROGDIR=$(readlink -m $(dirname "${0}")); readonly PROGDIR
|
||||||
|
ARGS="${*}"; readonly ARGS
|
||||||
|
readonly NBARGS="${#}"
|
||||||
|
[ -z "${DEBUG}" ] && DEBUG=1
|
||||||
|
|
||||||
CEPH_DEB_PKG_DIR=$(mktemp --directory --tmpdir="/tmp" -- ceph-deb-files-XXXXXX.tmp)
|
# Default values for some vars
|
||||||
CEPH_DEB_PKG_PATTERN="ceph|Ceph|rados|RADOS|rbd|rgw"
|
CEPH_VERSION_DEFAULT=$(ceph version | cut -d" " -f3)
|
||||||
|
BKP_DIR_DEFAULT="/srv/ceph"
|
||||||
|
|
||||||
CEPH_VERSION=$(ceph version | cut -d" " -f3)
|
## 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]
|
||||||
|
|
||||||
|
Try to download all deb files of all Ceph related packages installed
|
||||||
|
on the system (eg. ceph, rbd, rados,…) and create one tar file.
|
||||||
|
|
||||||
|
EXAMPLES :
|
||||||
|
- Backup Ceph related packages to default directory (${BKP_DIR_DEFAULT})
|
||||||
|
${PROGNAME}
|
||||||
|
|
||||||
|
- Store tar files to a different location
|
||||||
|
${PROGNAME} --dir /opt/backup/ceph
|
||||||
|
|
||||||
|
OPTIONS :
|
||||||
|
-d,--dir,--directory
|
||||||
|
Path to store tar files
|
||||||
|
(default: ${BKP_DIR_DEFAULT})
|
||||||
|
|
||||||
|
--debug
|
||||||
|
Enable debug messages.
|
||||||
|
|
||||||
|
--help
|
||||||
|
Print this help message.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
error_message() { # {{{
|
||||||
|
|
||||||
|
local_error_message="${1}"
|
||||||
|
local_error_code="${2}"
|
||||||
|
|
||||||
|
## Print error message
|
||||||
|
printf '%b\n' "ERROR − ${PROGNAME} : ${RED}${local_error_message}${RESET}"
|
||||||
|
|
||||||
|
exit "${local_error_code:=66}"
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
define_vars() { # {{{
|
||||||
|
|
||||||
|
# If BKP_DIR wasn't defined (argument) {{{
|
||||||
|
if [ -z "${BKP_DIR}" ]; then
|
||||||
|
## Use default value
|
||||||
|
BKP_DIR="${BKP_DIR_DEFAULT}"
|
||||||
|
fi
|
||||||
|
# }}}
|
||||||
|
# If CEPH_VERSION wasn't defined (argument) {{{
|
||||||
|
if [ -z "${CEPH_VERSION}" ]; then
|
||||||
|
## Use default value
|
||||||
|
CEPH_VERSION="${CEPH_VERSION_DEFAULT}"
|
||||||
|
fi
|
||||||
|
# }}}
|
||||||
|
# If CEPH_VERSION remains empty {{{
|
||||||
|
if [ -z "${CEPH_VERSION}" ]; then
|
||||||
|
error_message "Can't get ceph version. \
|
||||||
|
Ensure you have enought permissions to run \`ceph\` command." 01
|
||||||
|
fi
|
||||||
|
# }}}
|
||||||
|
|
||||||
BKP_DIR="/srv/ceph"
|
|
||||||
BKP_TAR_PATH="${BKP_DIR}/ceph-deb_${CEPH_VERSION}-$(date +%Y%m%d).tar"
|
BKP_TAR_PATH="${BKP_DIR}/ceph-deb_${CEPH_VERSION}-$(date +%Y%m%d).tar"
|
||||||
|
|
||||||
EXISTENT_BACKUP_TAR="$(find "${BKP_DIR}" -type f -iname "ceph-deb_${CEPH_VERSION}*")"
|
EXISTENT_BACKUP_TAR="$(find "${BKP_DIR}" -type f -iname "ceph-deb_${CEPH_VERSION}*")"
|
||||||
|
CEPH_DEB_PKG_PATTERN="ceph|Ceph|rados|RADOS|rbd|rgw"
|
||||||
|
|
||||||
|
## Temp file vars {{{
|
||||||
|
CEPH_DEB_PKG_DIR=$(mktemp --directory --tmpdir="/tmp" -- ceph-deb-files-XXXXXX.tmp)
|
||||||
|
## }}}
|
||||||
|
|
||||||
|
}
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# If Ceph is installed on the system
|
main() { # {{{
|
||||||
if [ "$(command -v ceph)" ]; then
|
|
||||||
|
|
||||||
# and no backup of this version already exists
|
## If Ceph is not available on the system {{{
|
||||||
if [ ! "${EXISTENT_BACKUP_TAR}" ]; then
|
### AND Exit with error
|
||||||
## Ensure to create backup directory
|
! command -v ceph >/dev/null \
|
||||||
mkdir -p -- "${BKP_DIR}"
|
&& error_message "Ceph doesn't seems to be available on this host. Please take a look to \`ceph version\` command." 11
|
||||||
|
## }}}
|
||||||
|
|
||||||
## Create and go to a temp directory
|
## Define all vars
|
||||||
cd -- "${CEPH_DEB_PKG_DIR}" > /dev/null || exit 2
|
define_vars
|
||||||
|
|
||||||
|
## If a backup already exists {{{
|
||||||
|
### AND Print debug message
|
||||||
|
### AND Delete temp directory
|
||||||
|
### AND Exit
|
||||||
|
test -f "${EXISTENT_BACKUP_TAR}" \
|
||||||
|
&& debug_message "A backup of this version already exists : ${EXISTENT_BACKUP_TAR}" \
|
||||||
|
&& rmdir -- "${CEPH_DEB_PKG_DIR}" \
|
||||||
|
&& exit 0
|
||||||
|
## }}}
|
||||||
|
|
||||||
|
## Ensure backup directory exists
|
||||||
|
mkdir --parents -- "${BKP_DIR}"
|
||||||
|
|
||||||
|
## Go to temp directory
|
||||||
|
cd -- "${CEPH_DEB_PKG_DIR}" > /dev/null \
|
||||||
|
|| error_message "Can't cd to ${CEPH_DEB_PKG_DIR}" 12
|
||||||
|
|
||||||
## Download deb files of all ceph related packages installed on the system
|
## Download deb files of all ceph related packages installed on the system
|
||||||
## Use aptitude before Debian Stretch because apt doesn't yet know "download" action
|
debug_message "Download (with apt) deb files of all ceph related packages installed on the system."
|
||||||
if [ "$(cat /etc/debian_version | cut -d"." -f1)" -lt "9" ]; then
|
apt download -- $(dpkg -l | awk -v pattern="${CEPH_DEB_PKG_PATTERN}" '$0~pattern {print $2"="$3}') 2>/dev/null \
|
||||||
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "Download (with aptitude) deb files of all ceph related packages installed on the system."
|
|| error_message "Can't download Ceph related packages (pattern: ${CEPH_DEB_PKG_PATTERN})." 13
|
||||||
aptitude download -- $(dpkg -l | awk -v pattern="${CEPH_DEB_PKG_PATTERN}" '$0~pattern {print $2"="$3}') || exit 3
|
|
||||||
else
|
|
||||||
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "Download (with apt) deb files of all ceph related packages installed on the system."
|
|
||||||
apt download -- $(dpkg -l | awk -v pattern="${CEPH_DEB_PKG_PATTERN}" '$0~pattern {print $2"="$3}') || exit 3
|
|
||||||
fi
|
|
||||||
|
|
||||||
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "Backup Ceph deb files in version : ${CEPH_VERSION}"
|
debug_message "Backup Ceph deb files in version : ${CEPH_VERSION}"
|
||||||
tar cf "${BKP_TAR_PATH}" ./*.deb > /dev/null 2>&1 || exit 4
|
tar cf "${BKP_TAR_PATH}" ./*.deb > /dev/null 2>&1 \
|
||||||
|
|| error_message "Error with creation of ${BKP_TAR_PATH} archive file." 14
|
||||||
|
|
||||||
## Remove deb files
|
## Remove deb files
|
||||||
rm -rf -- *.deb
|
rm --force -- *.deb \
|
||||||
|
|| error_message "Can't delete temp deb files." 15
|
||||||
|
|
||||||
cd - > /dev/null || exit 2
|
cd - > /dev/null \
|
||||||
|
|| error_message "Can't cd to previous directory (before ${CEPH_DEB_PKG_DIR})" 16
|
||||||
|
|
||||||
## Remove temp directory
|
## Remove temp directory
|
||||||
rmdir -- "${CEPH_DEB_PKG_DIR}"
|
rmdir -- "${CEPH_DEB_PKG_DIR}" \
|
||||||
else
|
|| error_message "Can't delete temp directory (${CEPH_DEB_PKG_DIR})" 17
|
||||||
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "A backup of this version already exists : ${EXISTENT_BACKUP_TAR}"
|
|
||||||
exit 0
|
}
|
||||||
fi
|
# }}}
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
else
|
|
||||||
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "Ceph doesn't seems to be available. Please take a look on \`ceph version\` command"
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Parse all options (start with a "-") one by one
|
||||||
|
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
|
||||||
|
|
||||||
|
case "${1}" in
|
||||||
|
-d|--dir|--directory|--backup-dir ) ## Define BKP_DIR
|
||||||
|
## Move to the next argument
|
||||||
|
shift
|
||||||
|
## Define var
|
||||||
|
readonly BKP_DIR="${1}"
|
||||||
|
;;
|
||||||
|
--debug ) ## debug
|
||||||
|
DEBUG=0
|
||||||
|
;;
|
||||||
|
--help ) ## help
|
||||||
|
usage
|
||||||
|
## Exit after help informations
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
* ) ## 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 0
|
exit 0
|
||||||
|
|
Loading…
Reference in New Issue