#etcd数据备份
#!/bin/bash
#备份数据目录创建
dir=/data/apisix-etcd-0
if [ ! -d "dir" ];then
mkdir -p /data/{apisix-etcd-0,apisix-etcd-1,apisix-etcd-2}
#apisix etcd数据备份
ETCDCTL_API=3 /opt/etcd/bin/etcdctl --endpoints="http://apisix-etcd-0.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2379" snapshot save /data/apisix-etcd-0/etcd-snapshot-`date +%Y%m%d`.db
ETCDCTL_API=3 /opt/etcd/bin/etcdctl --endpoints="http://apisix-etcd-1.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2379" snapshot save /data/apisix-etcd-1/etcd-snapshot-`date +%Y%m%d`.db
ETCDCTL_API=3 /opt/etcd/bin/etcdctl --endpoints="http://apisix-etcd-2.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2379" snapshot save /data/apisix-etcd-2/etcd-snapshot-`date +%Y%m%d`.db
#数据备份保留15天
find /data/apisix-etcd-0/ -name *.db -mtime +14 -exec rm -f {} \;
find /data/apisix-etcd-1/ -name *.db -mtime +14 -exec rm -f {} \;
find /data/apisix-etcd-2/ -name *.db -mtime +14 -exec rm -f {} \;
else
#apisix etcd数据备份
ETCDCTL_API=3 /opt/etcd/bin/etcdctl --endpoints="http://apisix-etcd-0.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2379" snapshot save /data/apisix-etcd-0/etcd-snapshot-`date +%Y%m%d`.db
ETCDCTL_API=3 /opt/etcd/bin/etcdctl --endpoints="http://apisix-etcd-1.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2379" snapshot save /data/apisix-etcd-1/etcd-snapshot-`date +%Y%m%d`.db
ETCDCTL_API=3 /opt/etcd/bin/etcdctl --endpoints="http://apisix-etcd-2.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2379" snapshot save /data/apisix-etcd-2/etcd-snapshot-`date +%Y%m%d`.db
#数据备份保留15天
find /data/apisix-etcd-0/ -name *.db -mtime +14 -exec rm -f {} \;
find /data/apisix-etcd-1/ -name *.db -mtime +14 -exec rm -f {} \;
find /data/apisix-etcd-2/ -name *.db -mtime +14 -exec rm -f {} \;
fi
#注: 需要把corednsIP写在主机/etc/resolv.conf里面
#etcd 数据恢复
注:重建etcd 集群时再执行恢复操作 如下是apisix-etcd-0 的恢复操作过程apisix-etcd-1、apisix-etcd-2也类似操作
1、如果服务在跑暂停etcd 服务,再删除原来的数据目录
rm /nfs/data/ingress-apisix-data-apisix-etcd-0-pvc-693e2816-b868-4c21-a89f-c704ff89aa5b/data/ -rf
TCDCTL_API=3 /opt/etcd/bin/etcdctl snapshot restore /home/etcd-0/etcd-snapshot-20231018.db \
--name apisix-etcd-0 --initial-cluster="apisix-etcd-0=http://apisix-etcd-0.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2380,apisix-etcd-1=http://apisix-etcd-1.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2380,apisix-etcd-2=http://apisix-etcd-2.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2380" \
--initial-cluster-token etcd-cluster --initial-advertise-peer-urls http://apisix-etcd-0.apisix-etcd-headless.ingress-apisix.svc.cluster.local:2380 \
--data-dir=/nfs/data/ingress-apisix-data-apisix-etcd-0-pvc-693e2816-b868-4c21-a89f-c704ff89aa0b/data
# --data-dir 是对应etcd 的pvc的数据存储目录
#给数据目录授权
chown -R 1001.root /nfs/data/ingress-apisix-data-apisix-etcd-0-pvc-693e2816-b868-4c21-a89f-c704ff89aa0b/data/
#重启etcd服务即可
#注意标颜色的地方需要修改成对应的