linux下etcd配置中心备份、还原

文章目录

etcd备份

//进入到etcd安装目录
[root@zabbix ~]# cd /opt/etcd/bin
[root@zabbix bin]# ls
etcd  etcdctl  nohup.out

//备份数据
[root@zabbix bin]# ./etcdctl snapshot save backup.db
Snapshot saved at backup.db
[root@zabbix bin]# ls
backup.db  etcd  etcdctl  nohup.out

//查看备份数据
[root@zabbix bin]# ./etcdctl snapshot status backup.db --write-out=table
+----------+----------+------------+------------+
|   HASH   | REVISION | TOTAL KEYS | TOTAL SIZE |
+----------+----------+------------+------------+
| 534a3e61 |    12415 |      16749 |     2.1 MB |
+----------+----------+------------+------------+

etcd安装

点击进入etcd官网下载
我用的包:https://github.com/etcd-io/etcd/releases/download/v3.4.16/etcd-v3.4.16-linux-amd64.tar.gz

//进入安装目录
[root@localhost opt]# ls
etcd-v3.4.16-linux-amd64.tar.gz
[root@localhost opt]# mkdir etcd
[root@localhost opt]# ls
etcd  etcd-v3.4.16-linux-amd64.tar.gz

//解压
[root@localhost opt]# tar -xf etcd-v3.4.16-linux-amd64.tar.gz -C etcd --strip-components=1

//编写配置文件
[root@localhost opt]# cat /etc/etcd/conf.yml
name: 'node1'
data-dir: /opt/etcd/data
listen-client-urls: http://192.168.1.116:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.1.116:2379
initial-advertise-peer-urls: http://192.168.1.116:2380

//启动服务
[root@localhost etcd]# ./etcd --config-file=/etc/etcd/conf.yml  &
[WARNING] Deprecated '--logger=capnslog' flag is set; use '--logger=zap' flag instead
2021-05-28 17:33:15.834613 I | etcdmain: Loading server configuration from "/etc/etcd/conf.yml". Other configuration command line flags and environment variables will be ignored if provided.
2021-05-28 17:33:15.834718 I | etcdmain: etcd Version: 3.4.16
2021-05-28 17:33:15.834726 I | etcdmain: Git SHA: d19fbe541
2021-05-28 17:33:15.834734 I | etcdmain: Go Version: go1.12.17
2021-05-28 17:33:15.834741 I | etcdmain: Go OS/Arch: linux/amd64
2021-05-28 17:33:15.834750 I | etcdmain: setting maximum number of CPUs to 1, total number of available CPUs is 1
......
......
2021-05-28 17:43:06.556813 I | embed: ready to serve client requests
2021-05-28 17:43:06.557751 N | embed: serving insecure client requests on 192.168.1.116:2379, this is strongly discouraged!



[root@localhost etcd]# netstat -nalp|grep 2379
tcp        0      0 192.168.1.116:2379      0.0.0.0:*               LISTEN      16613/./etcd        
tcp        0      0 127.0.0.1:2379          0.0.0.0:*               LISTEN      16613/./etcd 

etcd数据恢复

//删除数据存放目录,不删后面恢复时会报错
[root@localhost etcd]# rm -rf data/
[root@localhost etcd]# ./etcdctl snapshot restore backup.db --data-dir="/opt/etcd/data"
{"level":"info","ts":1622194917.46103,"caller":"snapshot/v3_snapshot.go:296","msg":"restoring snapshot","path":"backup.db","wal-dir":"/opt/etcd/data/member/wal","data-dir":"/opt/etcd/data","snap-dir":"/opt/etcd/data/member/snap"}
{"level":"info","ts":1622194917.91602,"caller":"membership/cluster.go:392","msg":"added member","cluster-id":"cdf818194e3a8c32","local-member-id":"0","added-peer-id":"8e9e05c52164694d","added-peer-peer-urls":["http://localhost:2380"]}
{"level":"info","ts":1622194918.0567791,"caller":"snapshot/v3_snapshot.go:309","msg":"restored snapshot","path":"backup.db","wal-dir":"/opt/etcd/data/member/wal","data-dir":"/opt/etcd/data","snap-dir":"/opt/etcd/data/member/snap"}

//修改data目录属主属组
[root@localhost etcd]# ll
总用量 42636
-rw-r--r--.  1 root    root   2052128 5月  28 17:26 backup.db
drwx------.  3 root    root        20 5月  28 17:43 data
drwxr-xr-x. 14 6810230 users     4096 5月  12 09:58 Documentation
-rwxr-xr-x.  1 6810230 users 23914048 5月  12 09:58 etcd
-rwxr-xr-x.  1 6810230 users 17616480 5月  12 09:58 etcdctl
-rw-r--r--.  1 6810230 users    43094 5月  12 09:58 README-etcdctl.md
-rw-r--r--.  1 6810230 users     8431 5月  12 09:58 README.md
-rw-r--r--.  1 6810230 users     7855 5月  12 09:58 READMEv2-etcdctl.md
[root@localhost etcd]# chown -R 6810230.users data/
[root@localhost etcd]# ll
总用量 42636
-rw-r--r--.  1 root    root   2052128 5月  28 17:26 backup.db
drwx------.  3 6810230 users       20 5月  28 17:43 data
drwxr-xr-x. 14 6810230 users     4096 5月  12 09:58 Documentation
-rwxr-xr-x.  1 6810230 users 23914048 5月  12 09:58 etcd
-rwxr-xr-x.  1 6810230 users 17616480 5月  12 09:58 etcdctl
-rw-r--r--.  1 6810230 users    43094 5月  12 09:58 README-etcdctl.md
-rw-r--r--.  1 6810230 users     8431 5月  12 09:58 README.md
-rw-r--r--.  1 6810230 users     7855 5月  12 09:58 READMEv2-etcdctl.md

//重启服务
[root@localhost etcd]# kill -9 16613
[root@localhost etcd]#  ./etcd --config-file=/etc/etcd/conf.yml  &

etcd网页访问安装

//安装视图
[root@zabbix opt]# cd etcd/etcd_view/
[root@zabbix etcd_view]# ls
etcd-viewer.war  jetty-runner.jar  nohup.out
[root@zabbix etcd_view]# java -DETCD_CLIENT_URL=http://127.0.0.1:2379 -Detcd.clientURL=http://127.0.0.1:2379 -jar jetty-runner.jar --port 8085 etcd-viewer.war

这步执行完就可以用网页访问配置中心了
http://192.168.1.116:8085/etcd/default/?0

上一篇:etcd做服务注册和服务发现


下一篇:服务发现:Zookeeper vs etcd vs Consul