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

67 lines
2.3 KiB
Bash
Executable File
Raw 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.

#!/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 {{{
DEBUG=1
CEPH_DEB_PKG_DIR=$(mktemp --directory --tmpdir="/tmp" -- ceph-deb-files-XXXXXX.tmp)
CEPH_DEB_PKG_PATTERN="ceph|Ceph|rados|RADOS|rbd|rgw"
CEPH_VERSION=$(ceph version | cut -d" " -f3)
BKP_DIR="/srv/ceph"
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}*")"
# }}}
# If Ceph is installed on the system
if [ "$(command -v ceph)" ]; then
# and no backup of this version already exists
if [ ! "${EXISTENT_BACKUP_TAR}" ]; then
## Ensure to create backup directory
mkdir -p -- "${BKP_DIR}"
## 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
## Use aptitude before Debian Stretch because apt doesn't yet know "download" action
if [ "$(cat /etc/debian_version | cut -d"." -f1)" -lt "9" ]; then
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "Download (with aptitude) deb files of all ceph related packages installed on the system."
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}"
tar cf "${BKP_TAR_PATH}" ./*.deb > /dev/null 2>&1 || exit 4
## Remove deb files
rm -rf -- *.deb
cd - > /dev/null || exit 2
## Remove temp directory
rmdir -- "${CEPH_DEB_PKG_DIR}"
else
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "A backup of this version already exists: ${EXISTENT_BACKUP_TAR}"
exit 0
fi
else
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "Ceph doesn't seems to be available. Please take a look on \`ceph version\` command"
exit 1
fi
exit 0