欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

etcd :备份与恢复

程序员文章站 2022-07-13 22:37:15
...

背景:
对于k8s集群,etcd集群的健康非常关键,我们要对etcd进行备份,以免出现问题
本文,代码,针对容器化部署的k8s (kubeadm安装方式)
备份:
脚本名: etcd-backup.sh

#!/bin/bash
#backup etcd data
set -e

etcdurl=`docker exec etcd ps -o args |grep -v ps |grep -v COMMAND |awk '{print $7}'`
health=`docker exec etcd etcdctl --endpoints ${etcdurl} cluster-health |grep 'cluster is healthy'`
if [ "$health" = "cluster is healthy" ]; then
  echo cluster is healthy
  backuptime=`date +%F-%H-%M-%S`
  etcdbackup=`docker exec -e ETCDCTL_API=3 etcd etcdctl --endpoints ${etcdurl} snapshot save snapshotdb-${backuptime} |awk '{print $4}'`
  mv ${etcdbackup}   /data/backup/etcd-backup
  echo cluster backup is done!
else
  echo cluster is not healthy
  exit 1
fi

测试
运行之后就可以在备份存储路径下面看到 新的备份文件
注意:代码默认的备份路径为:/data/backup/etcd-backup
可自行修改
etcd :备份与恢复
编写定时任务
crontab -e 编辑
etcd :备份与恢复
恢复:
脚本
#注意手动修改最近备份文件名为snapshot.db

#!/bin/bash
#restore etcd data
set -e

etcdurl=`docker exec etcd ps -o args |grep -v ps |grep -v COMMAND |awk '{print $7}'`
health=`docker exec etcd etcdctl --endpoints ${etcdurl} cluster-health |grep 'cluster is healthy'`
if [ "$health" = "cluster is healthy" ]; then
  echo "cluster is healthy"
  #注意手动修改最近备份文件名为snapshot.db
  docker exec etcd etcdctl  --endpoints ${etcdurl} snapshot restore snapshot.db   --data-dir=/var/lib/etcd
  echo "etcd restore is done!"
else
  echo "cluster is not healthy"
  exit 1
fi