etcd v3集羣備份和恢復

官方文檔

https://github.com/etcd-io/etcd/blob/master/Documentation/op-guide/recovery.mdnode

1、運行3個etcd節點

咱們用一臺機器的不一樣商品來模擬3個etcd節點
啓動腳本差很少,這裏我寫成了一個shell以下,vim /home/chenqionghe/etcdTest/run-node.shgit

#!/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

使用方式以下github

Usage: run-node.sh nodeName dataDir clientPort peerPort cluster

接下來咱們啓動3個節點shell

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個節點都已經起來了ubuntu

2、準備數據

這裏咱們設置幾個數據項vim

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

測試集羣和設置的數據bash

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

都已經生效ide

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} snapshot save "/home/chenqionghe/etcdTest/backup/snapshot.db"

4、恢復數據

執行恢復,咱們指定恢復的目錄爲nodeName.etcd.restore,比以前的數據目錄多了個.restoreui

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,就這麼簡單

相關文章
相關標籤/搜索