#!/bin/bash

# 具体备份恢复参考https://vip.kingdee.com/article/480372844078268928?productLineId=29&isKnowledge=2

backup_dir=$1
cert="/etc/kubernetes/pki/etcd/server.crt"
cacert="/etc/kubernetes/pki/etcd/ca.crt"
key="/etc/kubernetes/pki/etcd/server.key"

if ! command -v etcdctl > /dev/null; then
  echo "不存在etcdctl，从docker容器中拷贝一份"
  docker cp $(docker ps  | grep etcd | grep -v pause | head -1 |awk '{print $1}'):/usr/local/bin/etcdctl /usr/bin/
fi

# 查看etcd集群状态
etcdctl --cluster --cert=${cert} --cacert=${cacert} --key=${key} endpoint status -w table

# 备份
now=$(date --rfc-3339=date)
ETCDCTL_API=3 etcdctl --endpoints="https://127.0.0.1:2379" --cert=${cert} --cacert=${cacert} --key=${key} snapshot save ${backup_dir}/backup-${now}.db

echo "etcd恢复流程请参考：https://vip.kingdee.com/article/480372844078268928?productLineId=29&isKnowledge=2"