From fc7b154907a5473ecdd3376e4eecc54bde01a555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Mon, 22 Jul 2019 16:25:08 +0200 Subject: [PATCH] Dl deb files of all related Ceph pkg on the system --- ceph/ceph.backup.deb.files.sh | 41 ++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/ceph/ceph.backup.deb.files.sh b/ceph/ceph.backup.deb.files.sh index 8c5fa06..e4e7dad 100755 --- a/ceph/ceph.backup.deb.files.sh +++ b/ceph/ceph.backup.deb.files.sh @@ -10,8 +10,9 @@ # }}} # Vars {{{ -CEPH_DEB_DIR="/var/cache/apt/archives" -CEPH_DEB_PKG_FILE=$(find "${CEPH_DEB_DIR}" -maxdepth 1 -type f -iname "ceph*.deb" -print -quit) +CEPH_DEB_PKG_DIR=$(mktemp --directory --tmpdir="/tmp" -- ceph-deb-files-XXXXXX.tmp) +CEPH_DEB_PKG_PATTERN="ceph|rados|rbd" + CEPH_VERSION=$(ceph version | cut -d" " -f3) BKP_DIR="/srv/ceph" @@ -20,19 +21,39 @@ 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}*")" # }}} -# Test if a "ceph" package is available -if [ "${CEPH_DEB_PKG_FILE}" ]; then +# If Ceph is installed on the system +if [ "$(command -v ceph)" ]; then - mkdir -p -- "${BKP_DIR}" + # and no backup of this version already exists + if [ ! "${EXISTENT_BACKUP_TAR}" ]; then + ## Ensure to create backup directory + mkdir -p -- "${BKP_DIR}" - # If no backup of this version already exists, create a new one - if [ "${EXISTENT_BACKUP_TAR}" ]; then - printf '%b\n' "A backup of this version already exists : ${EXISTENT_BACKUP_TAR}" + ## Create and go to a temp directory + cd -- "${CEPH_DEB_PKG_DIR}" > /dev/null || exit 2 + + ## Download deb files of all ceph related packages installed on the system + printf '%b\n' "Download 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}') + + printf '%b\n' "Backup Ceph deb files in version : ${CEPH_VERSION}" + tar cf "${BKP_TAR_PATH}" ./*.deb > /dev/null 2>&1 + + ## Remove deb files + rm -rf -- *.deb + + cd - > /dev/null || exit 2 + + ## Remove temp directory + rmdir -- "${CEPH_DEB_PKG_DIR}" else - printf '%b\n' "Backup of Ceph deb files in version : ${CEPH_VERSION}" - tar cf "${BKP_TAR_PATH}" "${CEPH_DEB_DIR}"/*"${CEPH_VERSION}"*.deb > /dev/null 2>&1 + printf '%b\n' "A backup of this version already exists : ${EXISTENT_BACKUP_TAR}" + exit 1 fi +else + printf '%b\n' "Ceph doesn't seems to be available. Please take a look on \`ceph version\` command" + exit 1 fi exit 0