scripts/ceph/ceph.backup.deb.files.sh

227 lines
5.9 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# Purpose {{{
## This script will check if Ceph is available on the system.
## And download all deb files of all Ceph related packages installed on the system
## (eg. ceph, rbd, rados,…).
#
## You can use this script in a cron (for periodic backup of Ceph packages) or directly.
# }}}
# Vars {{{
PROGNAME=$(basename "${0}"); readonly PROGNAME
PROGDIR=$(readlink -m $(dirname "${0}")); readonly PROGDIR
ARGS="${*}"; readonly ARGS
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
# Default values for some vars
CEPH_VERSION_DEFAULT=$(ceph version | cut -d" " -f3)
BKP_DIR_DEFAULT="/srv/ceph"
## 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_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}*")"
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)
## }}}
}
# }}}
main() { # {{{
## If Ceph is not available on the system {{{
### AND Exit with error
! command -v ceph >/dev/null \
&& error_message "Ceph doesn't seems to be available on this host. Please take a look to \`ceph version\` command." 11
## }}}
## Define all vars
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
debug_message "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}') 2>/dev/null \
|| error_message "Can't download Ceph related packages (pattern: ${CEPH_DEB_PKG_PATTERN})." 13
debug_message "Backup Ceph deb files in version: ${CEPH_VERSION}"
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
rm --force -- *.deb \
|| error_message "Can't delete temp deb files." 15
cd - > /dev/null \
|| error_message "Can't cd to previous directory (before ${CEPH_DEB_PKG_DIR})" 16
## Remove temp directory
rmdir -- "${CEPH_DEB_PKG_DIR}" \
|| error_message "Can't delete temp directory (${CEPH_DEB_PKG_DIR})" 17
}
# }}}
# 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|--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