前言:
备份重要数据,简化重复操作,让一指禅、点点点也能完成运维任务。
脚本呈现界面如下:
1、查看Etcd版本
root@master:~# cat /etc/kubernetes/manifests/etcd.yaml | grep image: | awk '{print $2}'
registry.aliyuncs.com/google_containers/etcd:3.5.10-0
2、下载安装Etcd操作命令etcdctl
#下载etcdctl
root@master:~# wget https://github.com/etcd-io/etcd/releases/download/v3.5.10/etcd-v3.5.10-linux-amd64.tar.gz
root@master:~#
#解压缩
root@master:~# tar -zxvf etcd-v3.5.10-linux-amd64.tar.gz
root@master:~#
#移进可执行程序目录
root@master:~# sudo mv etcd-v3.5.10-linux-amd64/etcdctl /usr/bin/
root@master:~# sudo mv etcd-v3.5.10-linux-amd64/etcdutl /usr/bin/
root@master:~#
#查看一下etcdctl版本
root@master:~# etcdctl
NAME:etcdctl - A simple command line client for etcd3.USAGE:etcdctl [flags]VERSION:3.5.10API VERSION:3.5
#创建备份目录
root@master:~# mkdir -p /data/backup/file
3、Etcd数据备份/恢复脚本
#!/bin/bash
#AUTHOR : Created by YaoButing
#PLATFORM : Ubuntu 20.04.6 LTS (Focal Fossa)
#VERSION : 1.0
#DATE : 2024-05-06
#DESCRIPTION : This script is used to muanual backup/restore etcd data.
###############################################################################
NUMBER=100
while (($NUMBER>0))
doecho " "echo " 请选择需要执行的操作 "echo "********************************************************************"echo "* *"echo "* [ 1 ]. Etcd数据备份 [ 2 ]. Etcd数据恢复 *" echo "* *"echo "********************************************************************"echo "* 按 0 推出脚本 *"read -p "请输入将要执行的动作序号:" NUMBERecho " "echo " "if (($NUMBER==1))then echo "###########-- 1 --########### 开始备份Etcd数据 ###############"echo " "ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 \--cacert=/etc/kubernetes/pki/etcd/ca.crt \--cert=/etc/kubernetes/pki/etcd/server.crt \--key=/etc/kubernetes/pki/etcd/server.key \snapshot save /data/backup/etcd-snapshot-`date "+%Y-%m-%d"`.dbecho ""echo "###########-- 1 --############ Etcd数据备份完毕 ##############"echo " "echo "###########-- 1 --############ 验证Etcd快照 ##################"echo " "ETCDCTL_API=3 etcdctl --write-out=table snapshot status /data/backup/etcd-snapshot-`date "+%Y-%m-%d"`.dbecho " " elif (($NUMBER==2))thenecho "###########-- 2 --########### 下面是现有的Etcd备份文件 ########"ls /data/backup/ | sort -k 2 | nl ls /data/backup/ | sort -k 2 > /data/backup/file/file.txtread -p "#####-----------请输入要恢复的Etcd备份文件行号:" DBetcdfile=$(sed -n "${DB}p" /data/backup/file/file.txt)echo ""echo "创建恢复目录'/var/lib/etcd-restore'"mkdir /opt/backupmkdir /var/lib/etcd-restoremv /etc/kubernetes/manifests/kube* /opt/backup/ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 \--cacert=/etc/kubernetes/pki/etcd/ca.crt \--cert=/etc/kubernetes/pki/etcd/server.crt \--key=/etc/kubernetes/pki/etcd/server.key \--data-dir=/var/lib/etcd-restore \snapshot restore /data/backup/$etcdfilecp /opt/backup/* /etc/kubernetes/manifests/mv /etc/kubernetes/manifests/etcd.yaml /etc/kubernetes/manifests/bak.etcd.yamlsed 's!/var/lib/etcd!/var/lib/etcd-restore!' /etc/kubernetes/manifests/bak.etcd.yaml > /etc/kubernetes/manifests/etcd.yamlcat /etc/kubernetes/manifests/etcd.yaml | grep '/var/lib/etcd-restore'systemctl daemon-reloadsystemctl restart kubeletecho ""echo "###########-- 2 --########### Etcd数据恢复结束 ################"echo ""echo "数据恢复已结束,但仍需等待几分钟再进行查看验证..."elif (($NUMBER==0))thenecho "###########--- 程序即将退出 ---######################"elseecho "########--- 输入错误,请按照指定序号输入 --------############" fi
done