#!/bin/bash
#备份mongodb
# 设置变量
MONGOS_POD=`kubectl get pods -n mpks| grep mongos |head -n 1|awk '{print $1}'` # MongoDB Pod 的名称
MONGODB_BACKUPDIR="/data/backup/" # 宿主机上的备份目录
DATE=$(date +%Y%m%d_%H%M%S) # 备份文件的时间戳
DB_USER="root" # MongoDB 用户名
DB_PASS="kgclite" # MongoDB 密码
# 执行mongodump备份
kubectl exec -it "${MONGOS_POD}" -n mpks -- mongodump -h 127.0.0.1:27017 -u "${DB_USER}" -p "${DB_PASS}" --authenticationDatabase "admin" -o /tmp/mongo_${DATE}
# 打包备份文件
kubectl exec -it "${MONGOS_POD}" -n mpks -- tar zcvf /tmp/mongo_${DATE}.tar.gz /tmp/mongo_${DATE}/ 
# 将备份文件从Pod复制到宿主机
kubectl cp "${MONGOS_POD}":/tmp/mongo_${DATE}.tar.gz "${MONGODB_BACKUPDIR}/mongo_${DATE}.tar.gz" -n mpks
#判断文件是否生成
if [ -e ${MONGODB_BACKUPDIR}/mongo_${DATE}.tar.gz ];then
    echo -e "\033[32m备份完成,清除pod中的压缩包和备份目录\033[0m"
    kubectl exec -it "${MONGOS_POD}" -n mpks -- rm -f /tmp/mongo_${DATE}.tar.gz
    kubectl exec -it "${MONGOS_POD}" -n mpks -- rm -rf /tmp/mongo_${DATE}
else 
    echo -e "\033[31m备份失败\033[0m"
 fi
 
 #备份mysql
MYSQL_POD_NAME="mysql-online-mysql-ha-0" # Kubernetes 中 MySQL Pod 的名称
MYSQL_USER="root" # MySQL 用户名
MYSQL_PASSWORD="root" # MySQL 密码
BACKUP_DIR="/data/backup" # 宿主机上的备份目录
BACKUP_FILE="mysql_backup_$(date +%Y%m%d_%H%M%S).sql" # 备份文件名,包含时间戳
# 执行备份
kubectl exec -it  -n mpks "${MYSQL_POD_NAME}" -- mysqldump -u "${MYSQL_USER}" -p"${MYSQL_PASSWORD}" --all-databases --triggers --routines --events > ${BACKUP_DIR}/${BACKUP_FILE}
# 检查备份是否成功
if [ $? -eq 0 ]; then
  echo -e "\033[32mBackup successful: ${BACKUP_FILE}\033[0m"
else
  echo -e "\033[31mBackup failed\033[0m"
  exit 1
fi



# 关闭 kubelet
systemctl stop kubelet 
#检查kubelet状态
systemctl status docker | tee stop_kubelet_${DATE}.log
# 关闭 docker
systemctl stop docker
#检查docker状态
systemctl status docker | tee stop_docker_${DATE}.log
# 备份 etcd 数据
bash /data/backup/etcd/etcd_backup.sh |tee backup_etcd_${DATE}.log
#关闭gluster
systemctl stop glusterd
#检查gluster状态
systemctl status glusterd | tee stop_glusterd_${DATE}.log


# 关机
while true; do
    read -p "请输入Y或y确认: " input
    if [ "$input" == "Y" ] || [ "$input" == "y" ]; then
        echo "即将执行init 0"
        sleep 10
        init 0
        break
    else
        echo "输入错误,请重新输入Y或y"
    fi
done

    

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐