0


傻瓜化备份/恢复K8S集群Etcd数据

前言:

   备份重要数据,简化重复操作,让一指禅、点点点也能完成运维任务。

   脚本呈现界面如下:

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.10

API 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))
do
    echo "                                                                    "
    echo "                         请选择需要执行的操作                         "
    echo "********************************************************************"
    echo "*                                                                  *"
    echo "*        [ 1 ]. Etcd数据备份           [ 2 ]. Etcd数据恢复           *"   
    echo "*                                                                  *"
    echo "********************************************************************"
    echo "*                         按 0 推出脚本                             *"
    read -p "请输入将要执行的动作序号:" NUMBER
    echo "                                                                    "
    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"`.db
        echo ""
        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"`.db
        echo " "    
    elif (($NUMBER==2))
    then
        echo "###########--  2  --###########  下面是现有的Etcd备份文件  ########"
        ls /data/backup/ | sort -k 2 | nl 
        ls /data/backup/ | sort -k 2 > /data/backup/file/file.txt
        read -p "#####-----------请输入要恢复的Etcd备份文件行号:" DB
        etcdfile=$(sed -n "${DB}p" /data/backup/file/file.txt)
        echo ""
        echo "创建恢复目录'/var/lib/etcd-restore'"
        mkdir /opt/backup
        mkdir /var/lib/etcd-restore
        mv /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/$etcdfile
        cp /opt/backup/* /etc/kubernetes/manifests/
        mv /etc/kubernetes/manifests/etcd.yaml /etc/kubernetes/manifests/bak.etcd.yaml
        sed 's!/var/lib/etcd!/var/lib/etcd-restore!'  /etc/kubernetes/manifests/bak.etcd.yaml > /etc/kubernetes/manifests/etcd.yaml
        cat /etc/kubernetes/manifests/etcd.yaml | grep '/var/lib/etcd-restore'
        
        systemctl daemon-reload
        systemctl restart kubelet
        echo ""
        echo "###########--  2  --###########  Etcd数据恢复结束  ################"
        echo ""
        echo "数据恢复已结束,但仍需等待几分钟再进行查看验证..."
    elif (($NUMBER==0))
    then
        echo "###########---        程序即将退出        ---######################"
    else
        echo "########---     输入错误,请按照指定序号输入     --------############"              
    fi  
done
标签: kubernetes etcd 容器

本文转载自: https://blog.csdn.net/tiger0310119/article/details/138466756
版权归原作者 藥瓿亭 所有, 如有侵权,请联系我们删除。

“傻瓜化备份/恢复K8S集群Etcd数据”的评论:

还没有评论