etcd集群备份脚本

in kubernetes with 0 comment

话不多说,直接上脚本,实际应用时只要修改对应的node列表值即可。

#! /bin/bash
ETCDCTL_PATH='/usr/local/bin/etcdctl'
ETCD_DATA_DIR="/var/lib/etcd"
BACKUP_DIR="/data/backups/kube_etcd/etcd-$(date +%Y-%m-%d_%H:%M:%S)"

ETCDCTL_CERT="/etc/kubernetes/ssl/etcd/etcd.pem"
ETCDCTL_KEY="/etc/kubernetes/ssl/etcd/etcd-key.pem"
ETCDCTL_CA_FILE="/etc/kubernetes/ssl/ca.pem"

[ ! -d $BACKUP_DIR ] && mkdir -p $BACKUP_DIR

sleep 3
for node in 192.168.0.146 192.168.0.147 192.168.0.148;do
  echo "Backing up $node..."
  export ETCDCTL_API=3;$ETCDCTL_PATH --endpoints="https://$node:2379" snapshot save $BACKUP_DIR/snapshot-$node.db \
                                   --cacert="$ETCDCTL_CA_FILE" \
                                   --cert="$ETCDCTL_CERT" \
                                   --key="$ETCDCTL_KEY"
done

sleep 3

cd $BACKUP_DIR/../;ls -lt |awk '{if(NR>10){print "rm -rf "$9}}'|sh

tar czf $BACKUP_DIR.tar.gz $BACKUP_DIR

#添加计划任务

* */2 * * * /bin/bash /data/etcd_back.sh
Responses