官方文檔
https://github.com/etcd-io/etcd/blob/master/Documentation/op-guide/recovery.md
一、運行3個etcd節點
我們用一台機器的不同商品來模擬3個etcd節點
啟動腳本差不多,這里我寫成了一個shell如下,vim /home/chenqionghe/etcdTest/run-node.sh
#!/usr/bin/env bash
usage() {
echo "Usage: `basename $0` nodeName dataDir clientPort peerPort cluster "
echo "例如:`basename $0` node1 /data/node1.etcd 11379 11380 \"node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380\""
exit 1;
}
# 檢測參數
if [ $# -lt 5 ];then
usage
exit 1
fi
name=$1
dataDir=$2
clientPort=$3
peerPort=$4
cluster=$5
token="light-weight-baby"
# 運行節點
etcd --name ${name} \
--data-dir ${dataDir} \
--initial-cluster-token ${token} \
--initial-advertise-peer-urls http://127.0.0.1:${peerPort} \
--listen-peer-urls http://0.0.0.0:${peerPort} \
--listen-client-urls http://0.0.0.0:${clientPort} \
--advertise-client-urls http://0.0.0.0:${clientPort} \
--initial-cluster ${cluster} \
--initial-cluster-state new
使用方式如下
Usage: run-node.sh nodeName dataDir clientPort peerPort cluster
接下來我們啟動3個節點
export cluster="node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380"
./run-node.sh node1 /home/chenqionghe/etcdTest/node1.etcd 11379 11380 ${cluster}
./run-node.sh node2 /home/chenqionghe/etcdTest/node2.etcd 12379 12380 ${cluster}
./run-node.sh node3 /home/chenqionghe/etcdTest/node3.etcd 13379 13380 ${cluster}
ok,我們看到3個節點都已經起來了
二、准備數據
這里我們設置幾個數據項
export ETCD_ENDPOINTS="http://127.0.0.1:11379,http://127.0.0.1:12379,http://127.0.0.1:13379"
# 設置數據
etcdctl --endpoints=${ETCD_ENDPOINTS} put /cqh chenqionghe
etcdctl --endpoints=${ETCD_ENDPOINTS} put /cqh/bench_press 100kg
etcdctl --endpoints=${ETCD_ENDPOINTS} put /cqh/dead_lift 160kg
etcdctl --endpoints=${ETCD_ENDPOINTS} put /cqh/deep_squal 140kg
測試集群和設置的數據
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} member list
25ad84e18438ca4e, started, node2, http://127.0.0.1:12380, http://0.0.0.0:12379
3d78c9dc937b8bce, started, node3, http://127.0.0.1:13380, http://0.0.0.0:13379
9328257f7d3eded0, started, node1, http://127.0.0.1:11380, http://0.0.0.0:11379
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --keys-only
/cqh
/cqh/bench_press
/cqh/dead_lift
/cqh/deep_squal
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --print-value-only
chenqionghe
100kg
160kg
140kg
都已經生效
三、備份數據
很簡單,就是設置一個保存的文件
export ETCD_ENDPOINTS="http://127.0.0.1:11379,http://127.0.0.1:12379,http://127.0.0.1:13379"
etcdctl --endpoints=${ETCD_ENDPOINTS} snapshot save "/home/chenqionghe/etcdTest/backup/snapshot.db"
四、恢復數據
執行恢復,我們指定恢復的目錄為nodeName.etcd.restore,比之前的數據目錄多了個.restore
export ETCDCTL_API=3
etcdctl snapshot restore /home/chenqionghe/etcdTest/backup/snapshot.db \
--data-dir="/home/chenqionghe/etcdTest/node1.etcd.restore" \
--name node1 \
--initial-cluster "node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380" \
--initial-cluster-token etcdv3-cluster \
--initial-advertise-peer-urls http://127.0.0.1:11380
export ETCDCTL_API=3
etcdctl snapshot restore /home/chenqionghe/etcdTest/backup/snapshot.db \
--data-dir="/home/chenqionghe/etcdTest/node2.etcd.restore" \
--name node2 \
--initial-cluster "node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380" \
--initial-cluster-token etcdv3-cluster \
--initial-advertise-peer-urls http://127.0.0.1:12380
export ETCDCTL_API=3
etcdctl snapshot restore /home/chenqionghe/etcdTest/backup/snapshot.db \
--data-dir="/home/chenqionghe/etcdTest/node3.etcd.restore" \
--name node3 \
--initial-cluster "node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380" \
--initial-cluster-token etcdv3-cluster \
--initial-advertise-peer-urls http://127.0.0.1:13380
測試恢復,我們先刪除所有示例數據
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh chenqionghe
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/bench_press 100kg
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/dead_lift 160kg
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/deep_squal 140kg
確認已經沒有數據了
# 測試恢復,先刪除所有示例數據
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh chenqionghe
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/bench_press 100kg
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/dead_lift 160kg
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/deep_squal 140kg
# 確認已經沒有數據了
etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --keys-only
etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --print-value-only
停止之前的3個節點,再重新根據.restore目錄分別運行3個節點
export cluster="node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380"
./run-node.sh node1 /home/chenqionghe/etcdTest/node1.etcd.restore 11379 11380 ${cluster}
./run-node.sh node2 /home/chenqionghe/etcdTest/node2.etcd.restore 12379 12380 ${cluster}
./run-node.sh node3 /home/chenqionghe/etcdTest/node3.etcd.restore 13379 13380 ${cluster}
重新執行命令
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --keys-only
/cqh
/cqh/bench_press
/cqh/dead_lift
/cqh/deep_squal
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --print-value-only
chenqionghe
100kg
160kg
140kg
看到數據恢復了,ok,就這么簡單
