diwakar thyagaraj 4869f57038 Remove OSH Authors copyright
The current copyright refers to a non-existent group
"openstack helm authors" with often out-of-date references that
are confusing when adding a new file to the repo.

This change removes all references to this copyright by the
non-existent group and any blank lines underneath.

Change-Id: Icdf013090dd26985d4443fdd4d1232182a1f699c
Signed-off-by: diwakar thyagaraj <diwakar.chitoor.thyagaraj@att.com>
2020-05-27 20:24:21 +00:00

122 lines
4.6 KiB
Smarty

#!/bin/bash
{{/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
usage() {
echo "Backup Usage: utilscli rbd_pv [-b <pvc name>] [-n <namespace>] [-d <backup dest> (optional, default: /backup)] [-p <ceph rbd pool> (optional, default: rbd)]"
echo "Restore Usage: utilscli rbd_pv [-r <restore_file>] [-p <ceph rbd pool> (optional, default: rbd)]"
echo "Snapshot Usage: utilscli rbd_pv [-b <pvc name>] [-n <namespace>] [-p <ceph rbd pool> (optional, default: rbd] [-s <create|rollback|remove|show> (required) ]"
exit 1
}
while getopts ":b:p:n:d:r:h:s:" opt; do
case $opt
in
b) pvc_name=${OPTARG};;
n) nspace=${OPTARG};;
d) backup_dest=${OPTARG};;
r) restore_file=${OPTARG};;
p) rbd_pool=${OPTARG};;
s) snapshot=${OPTARG};;
h) usage ;;
esac
done
if [[ -z "${pvc_name}" || -z "${nspace}" ]]; then
if [[ -z "${restore_file}" ]]; then
usage
echo "ERROR: Missing command line arguement(s)!"
exit 1
fi
fi
if [[ -z "${rbd_pool}" ]]; then
rbd_pool="rbd"
fi
timestamp="$(date +%F_%T)"
if [[ ! -z "${restore_file}" ]]; then
if [[ -e "${restore_file}" ]]; then
rbd_image="$(echo "${restore_file}" | rev | awk -v FS='/' '{print $1}' | rev | cut -f 1 -d '.')"
if (rbd info "${rbd_pool}"/"${rbd_image}" | grep -q id); then
rbd mv ${rbd_pool}/${rbd_image} ${rbd_pool}/${rbd_image}.orig-${timestamp}
echo "WARNING: Existing PVC/RBD image has been moved to ${rbd_pool}/${rbd_image}.orig-${timestamp}"
fi
rbd import ${restore_file} ${rbd_pool}/${rbd_image}
echo "INFO: Backup has been restored into ${rbd_pool}/${rbd_image}"
else
echo "ERROR: Missing restore file!"
exit 1
fi
elif [[ ! -z "${snapshot}" ]]; then
volume="$(kubectl -n ${nspace} get pvc ${pvc_name} --no-headers | awk '{ print $3 }')"
rbd_image="$(kubectl get pv "${volume}" -o json | jq -r '.spec.rbd.image')"
if [[ "x${snapshot}x" == "xcreatex" ]]; then
snap_name="${pvc_name}-${timestamp}"
rbd snap create ${rbd_pool}/${rbd_image}@${snap_name}
echo "INFO: Snapshot ${rbd_pool}/${rbd_image}@${snap_name} has been created for PVC ${pvc_name}"
elif [[ "x${snapshot}x" == "xrollback" ]]; then
snap_name=$(rbd snap ls ${rbd_pool}/${rbd_image})
rbd snap rollback ${rbd_pool}/${rbd_image}@${snap_name}
echo "WARNING: Rolled back snapshot ${rbd_pool}/${rbd_image}@${snap_name} for ${pvc_name}"
elif [[ "x${snapshot}x" == "xremovex" ]]; then
rbd snap purge ${rbd_pool}/${rbd_image}
echo "Removed snapshot(s) for ${pvc_name}"
elif [[ "x${snapshot}x" == "xshowx" ]]; then
echo "INFO: This PV is mapped to the following RBD Image:"
echo "${rbd_pool}/${rbd_image}"
echo -e "\nINFO: Current open sessions to RBD Image:"
rbd status ${rbd_pool}/${rbd_image}
echo -e "\nINFO: RBD Image information:"
rbd info ${rbd_pool}/${rbd_image}
echo -e "\nINFO: RBD Image snapshot details:"
rbd snap ls ${rbd_pool}/${rbd_image}
echo -e "\nINFO: RBD Image size details:"
rbd du ${rbd_pool}/${rbd_image}
else
echo "ERROR: Missing arguement for snapshot option!"
fi
else
if [[ -z "${backup_dest}" ]]; then
backup_dest="/backup"
fi
if [[ ! -d "${backup_dest}" ]]; then
echo "ERROR: Backup destination does not exist, cannot continue with the backup!"
exit 1
fi
echo "INFO: Backing up ${pvc_name} within namespace ${nspace}"
volume="$(kubectl -n ${nspace} get pvc ${pvc_name} --no-headers | awk '{ print $3 }')"
rbd_image="$(kubectl get pv "${volume}" -o json | jq -r '.spec.rbd.image')"
if [[ -z "${volume}" ]] || (! rbd info "${rbd_pool}"/"${rbd_image}" | grep -q id); then
echo "ERROR: PVC does not exist or is missing! Cannot continue with backup for ${pvc_name}"
exit 1
else
# Create current snapshot and export to a file
snap_name="${pvc_name}-${timestamp}"
backup_name="${rbd_image}.${pvc_name}-${timestamp}"
rbd snap create ${rbd_pool}/${rbd_image}@${snap_name}
rbd export ${rbd_pool}/${rbd_image}@${snap_name} ${backup_dest}/${backup_name}
# Remove snapshot otherwise we may see an issue cleaning up the PVC from K8s, and from Ceph.
rbd snap rm ${rbd_pool}/${rbd_image}@${snap_name}
echo "INFO: PV ${pvc_name} saved to:"
echo "${backup_dest}/${backup_name}"
fi
fi
exit 0