51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

k8s 备份-etcd 备份

etcd 备份:

etcd is running.
{"level":"info","ts":1698069449.0897536,"caller":"snapshot/v3_snapshot.go:119","msg":"created temporary db file","path":"/root/etcd-backup/etcd_backup_2023年10月23日21时57分.db.part"}
{"level":"info","ts":"2023-10-23T21:57:29.097+0800","caller":"clientv3/maintenance.go:200","msg":"opened snapshot stream; downloading"}
{"level":"info","ts":1698069449.0973763,"caller":"snapshot/v3_snapshot.go:127","msg":"fetching snapshot","endpoint":"https://192.168.1.208:2379"}
{"level":"info","ts":"2023-10-23T21:57:29.380+0800","caller":"clientv3/maintenance.go:208","msg":"completed snapshot read; closing"}
{"level":"info","ts":1698069449.5135188,"caller":"snapshot/v3_snapshot.go:142","msg":"fetched snapshot","endpoint":"https://192.168.1.208:2379","size":"26 MB","took":0.423625173}
{"level":"info","ts":1698069449.5136635,"caller":"snapshot/v3_snapshot.go:152","msg":"saved","path":"/root/etcd-backup/etcd_backup_2023年10月23日21时57分.db"}
Snapshot saved at /root/etcd-backup/etcd_backup_2023年10月23日21时57分.db
Backup successful: /root/etcd-backup/etcd_backup_2023年10月23日21时57分.db

etcd-backup.sh

使用方法: 先粘贴到 notpad++,然后再复制粘贴到 .sh 文件中。 不然会有编码问题。

#!/bin/bash
SERVICE_NAME="etcd"
# 检查服务是否在运行
if systemctl is-active --quiet $SERVICE_NAME; then
  echo "$SERVICE_NAME is running."
else
  echo "$SERVICE_NAME is not running."
  exit 1
fi
 
# 获取当前主机的 IP 地址
CURRENT_IP=$(hostname -I | cut -d' ' -f1)
 
# 设置备份存储路径
BACKUP_DIR="/root/etcd-backup"
 
# 设置 etcd 证书路径
CACERT="/etc/ssl/etcd/ssl/ca.pem"
CERT="/etc/ssl/etcd/ssl/admin-master1.pem"
KEY="/etc/ssl/etcd/ssl/admin-master1-key.pem"
 
# 检查备份目录是否存在,如果不存在则创建
if [ ! -d "$BACKUP_DIR" ]; then
  mkdir -p "$BACKUP_DIR"
fi
 
# 获取当前时间的年、月、日、小时和分钟
YEAR=$(date +"%Y")
MONTH=$(date +"%m")
DAY=$(date +"%d")
HOUR=$(date +"%H")
MINUTE=$(date +"%M")
 
# 构建格式化的时间戳
TIMESTAMP="${YEAR}年${MONTH}月${DAY}日${HOUR}时${MINUTE}分"
 
# 构建备份文件名
BACKUP_FILE="$BACKUP_DIR/etcd_backup_$TIMESTAMP.db"
 
# 执行 etcd 备份命令
ETCDCTL_API=3 /usr/local/bin/etcdctl --endpoints="https://$CURRENT_IP:2379" snapshot save "$BACKUP_FILE" \
--cacert="$CACERT" \
--cert="$CERT" \
--key="$KEY"
 
if [ $? -eq 0 ]; then
  echo "Backup successful: $BACKUP_FILE"
else
  echo "Backup failed"
fi
赞(7)
未经允许不得转载:工具盒子 » k8s 备份-etcd 备份