Etcd备份及恢复

一、Etcd数据备份

1、备份命令

[root@localhost ~]# export ETCDCTL_API=3
[root@localhost ~]# /data/etcd-3.4.9/bin/etcdctl --endpoints=10.2.20.108:2379 snapshot save etcd-`date "+%Y-%m-%d_%H-%M-%S"`.snapshot

2、备份完成后会在当前目录生成备份文件

[root@localhost ~]# ls
anaconda-ks.cfg  etcd-2023-08-17_16-43-08.snapshot  etcd-3.4.9.tar.gz

二、数据恢复

etcd集群中所有实例,可通过随意一台etcd的备份文件进行恢复,数据是一样的。

备份恢复命令模版

 ETCDCTL_API=3 etcdctl snapshot restore +备份文件名 \
  --name \                                                           #当前实例在集群中的名字   
  --initial-cluster  \                                               #集群中所有实例通讯地址。
  --initial-cluster-token etcd-cluster \                  #集群名字,不要与已有集群冲突
  --initial-advertise-peer-urls    \                         #当前实例进群通讯地址
  --data-dir /data/mydata_etcd_3.4.9                 #数据恢复目录

1、实例1恢复

$ ETCDCTL_API=3 etcdctl snapshot restore etcd-2023-08-17_16-43-08.snapshot \
  --name etcd-201 \
  --initial-cluster etcd-201=http://10.2.20.77:2380,etcd-202=http://10.2.20.88:2380,etcd-203=http://10.2.20.99:2380 \
  --initial-cluster-token etcd-cluster \
  --initial-advertise-peer-urls http://10.2.20.77:2380 \
  --data-dir /data/mydata_etcd_3.4.9

2、实例2恢复

$ ETCDCTL_API=3 etcdctl snapshot restore etcd-2023-08-17_16-43-08.snapshot \
  --name etcd-202 \
  --initial-cluster etcd-201=http://10.2.20.77:2380,etcd-202=http://10.2.20.88:2380,etcd-203=http://10.2.20.99:2380 \
  --initial-cluster-token etcd-cluster \
  --initial-advertise-peer-urls http://10.2.20.88:2380 \
  --data-dir /data/mydata_etcd_3.4.9

3、实例3恢复

$ ETCDCTL_API=3 etcdctl snapshot restore etcd-2023-08-17_16-43-08.snapshot \
  --name etcd-203 \
  --initial-cluster etcd-201=http://10.2.20.77:2380,etcd-202=http://10.2.20.88:2380,etcd-203=http://10.2.20.99:2380 \
  --initial-cluster-token etcd-cluster \
  --initial-advertise-peer-urls http://10.2.20.99:2380 \
  --data-dir /data/mydata_etcd_3.4.9

4、启动恢复后的集群

systemctl daemon-reload
systemctl start etcd

5、检查新进群

查看leader是否选举成功

[root@etcd003 ~]# ETCDCTL_API=3 /data/hxsd-etcd-3.4.9/bin/etcdctl  --endpoints="http://10.2.20.77:2379,http://10.2.20.88:2379,http://10.2.20.99:2379"   endpoint status --write-out=table
+-----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
|          ENDPOINT           |        ID        | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
|  http://10.2.20.77:2379 | 52fba0ba228f3da3 |   3.4.9 |  2.1 GB |     false |      false |        50 |          9 |                  9 |        |
|  http://10.2.20.88:2379 | 4907167561836ce3 |   3.4.9 |  2.1 GB |      true |      false |        50 |          9 |                  9 |        |
|  http://10.2.20.99:2379 | b80cd01b4c2b7a1c |   3.4.9 |  2.1 GB |     false |      false |        50 |          9 |                  9 |        |
+-----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+

你可能感兴趣的:(etcd,linux,数据库)