k8s集群中mongdb、etcd、prometheus数据bash一键备份脚本
- 2025-04-03 12:19:28
- 脚本
- 31
- shevechco
对于k8s一中间件的数据进行一键备份,可以在做临时变更或者定期备份的场景下使用,备份mongodb主要使用了mongodump命令,etcd使用snapshot命令,prometheus是添加了--web.enable-admin-api配置,然后使用curl命令触发备份接口进行备份,具体脚本内容如下:
#!/bin/bash
set -xe
#backup mongodb/etc/prometheus
CURRENT_DIR=`readlink -f $(dirname $0)`
export PATH=$PATH:${CURRENT_DIR}
NAMESPACE="aiops"
MONGODB_ROOT="root"
MONGODB_PASS="admin123"
MONGODB_COLLS="gui logs monitor"
MONGODB_BACKUP_DIR="/bitnami/mongodb_bak"
LOCAL_BACKUP_DIR="${CURRENT_DIR}/backup_$(date +'%Y%m%d')"
mkdir -p ${LOCAL_BACKUP_DIR}
function INFO(){
/bin/echo -e "\e[104m\e[97m[INFO]\e[49m\e[39m ${*}"
}
function WARNING(){
/bin/echo >&2 -e "\e[101m\e[97m[WARNING]\e[49m\e[39m ${*}"
}
function ERROR(){
/bin/echo >&2 -e "\e[101m\e[97m[ERROR]\e[49m\e[39m ${*}"
}
function mongo_backup(){
INFO "创建备份目录 ${MONGODB_BACKUP_DIR} !"
if [[ ! -e ${MONGODB_BACKUP_DIR} ]]; then
kubectl exec -i -n ${NAMESPACE} controller-mongodb-0 -- mkdir -p ${MONGODB_BACKUP_DIR}
fi
INFO "循环导出集合数据..."
for COLL in ${MONGODB_COLLS}
do
INFO "开始导出 ${COLL} 集合数据..."
kubectl exec -i -n ${NAMESPACE} controller-mongodb-0 -- mongodump -h 127.0.0.1:27017 -u $MONGODB_ROOT -p $MONGODB_PASS --authenticationDatabase=admin -d ${COLL} -o /bitnami/mongodb_bak/
done
INFO "集合数据导出完成,开始打包备份数据到容器/tmp目录..."
kubectl exec -i -n ${NAMESPACE} controller-mongodb-0 -- tar -zcvf /tmp/mongodb_bak.tar.gz -C ${MONGODB_BACKUP_DIR} . >/dev/null
remote_md5=`kubectl exec -i -n ${NAMESPACE} controller-mongodb-0 -- md5sum /tmp/mongodb_bak.tar.gz | awk '{print $1}'`
INFO "mongodb数据打包完成,将打包数据备份到本地目录 ${LOCAL_BACKUP_DIR} 下..."
if [[ ! -e ${LOCAL_BACKUP_DIR} ]]; then
mkdir -p ${LOCAL_BACKUP_DIR}
fi
kubectl cp ${NAMESPACE}/controller-mongodb-0:/tmp/mongodb_bak.tar.gz ${LOCAL_BACKUP_DIR}/mongodb_bak.tar.gz >/dev/null
local_md5=`md5sum ${LOCAL_BACKUP_DIR}/mongodb_bak.tar.gz | awk '{print $1}'`
INFO "mongodb备份数据已经复制本地完成!!!"
INFO "本地mongodb备份文件md5: ${local_md5}, 容器内mongodb备份文件md5: ${remote_md5}"
if [[ ${local_md5} == ${remote_md5} ]]; then
INFO "mongodb数据备份完成, 备份路径${LOCAL_BACKUP_DIR}/mongodb_bak.tar.gz !"
else
ERROR "容器内mongodb备份文件与本地备份文件md5不一致,请重新操作!"
exit 1
fi
}
function etcd_backup(){
INFO "将etcd快照snapshot.db文件备份到容器/tmp目录下"
kubectl exec -i -n ${NAMESPACE} etcd-0 -- /home/etcd/etcdctl snapshot save /tmp/snapshot.db >/dev/null
remote_md5=`kubectl exec -i -n ${NAMESPACE} etcd-0 -- md5sum /tmp/snapshot.db | awk '{print $1}'`
INFO "将etcd备份文件复制到本地目录 ${LOCAL_BACKUP_DIR} 下..."
kubectl cp ${NAMESPACE}/etcd-0:/tmp/snapshot.db ${LOCAL_BACKUP_DIR}/snapshot.db >/dev/null
local_md5=`md5sum ${LOCAL_BACKUP_DIR}/snapshot.db | awk '{print $1}'`
INFO "本地etcd备份文件md5: ${local_md5}, 容器内etcd备份文件md5: ${remote_md5}"
if [[ ${local_md5} == ${remote_md5} ]]; then
INFO "etcd数据备份完成, 备份路径${LOCAL_BACKUP_DIR}/snapshot.db !"
else
ERROR "容器内etcd备份文件与本地备份文件md5不一致,请重新操作!"
exit 1
fi
}
function prometheus_backup(){
INFO "更改prometheus配置,添加admin-api"
kubectl exec -i -n ${NAMESPACE} web-gui-0 -- sed -i 's@/usr/share/prometheus/consoles@/usr/share/prometheus/consoles --web.enable-admin-api@' /prometheus/prometheus.sh
kubectl exec -i -n ${NAMESPACE} web-gui-0 -- pkill -f prometheus
sleep 3s #等待3s, 等待prometheus启动。
snapshot=`kubectl exec -i -n ${NAMESPACE} web-gui-0 -- curl -X POST http://localhost:9090/api/v1/admin/tsdb/snapshot -s`
snapshot_name=`echo ${snapshot} | jq -r .data.name`
INFO "将tsdb数据打包为snapshots.tar到容器/tmp目录下"
kubectl exec -i -n ${NAMESPACE} web-gui-0 -- tar -zcvf /tmp/snapshots.tar -C /prometheus/snapshots/${snapshot_name} . >/dev/null
remote_md5=`kubectl exec -i -n ${NAMESPACE} web-gui-0 -- md5sum /tmp/snapshots.tar | awk '{print $1}'`
INFO "将tsdb备份数据复制到本地目录 ${LOCAL_BACKUP_DIR} 下..."
kubectl cp ${NAMESPACE}/web-gui-0:/tmp/snapshots.tar ${LOCAL_BACKUP_DIR}/snapshots.tar >/dev/null
local_md5=`md5sum ${LOCAL_BACKUP_DIR}/snapshots.tar | awk '{print $1}'`
INFO "本地tsdb备份文件md5: ${local_md5}, 容器内tsdb备份文件md5: ${remote_md5}"
if [[ ${local_md5} == ${remote_md5} ]]; then
INFO "tsdb数据备份完成, 备份路径${LOCAL_BACKUP_DIR}/snapshots.tar !"
else
ERROR "容器内tsdb备份文件与本地备份文件md5不一致,请重新操作!"
exit 1
fi
}
function license_backup(){
node_ip=`kubectl get pod -n ${NAMESPACE} web-gui-0 -o jsonpath={.status.hostIP}`
INFO "获取web-gui节点 IP为: ${node_ip}"
curl -X GET "http://${node_ip}:8080/licenses" -s | jq -r .data[].Id | while read line;
do
curl -X GET "http://${node_ip}:8080/licenses/${line}" -s | jq -r .data.License >${LOCAL_BACKUP_DIR}/${line}.license
INFO "license ID: ${line} 备份到 ${LOCAL_BACKUP_DIR}/${line}.license文件内..."
done
}
INFO "开始备份mongodb数据..."
mongo_backup
INFO "开始备份etcd数据..."
etcd_backup
INFO "开始备份tsdb数据..."
prometheus_backup
if ! command -v "jq" >/dev/null 2>&1; then
WARNING "未检测到jq工具,跳过license备份!"
else
INFO "开始备份license文件"
license_backup
fi
INFO "备份完成!"
-
标签
- k8s
- mongdb
- etcd
- prometheus
- bash
内容版权声明:除非注明,否则皆为本站原创文章。