1. 备份
#!/bin/bash
currentUser=$(whoami)
if [ "$currentUser" != "root" ];then
echo "当前用户为非root,请切换用户:${currentUser}"
exit 1
fi
esDir="/opt/elk/elasticsearch-6.7.0/" #es安装目录
es_backup="/home/gocode/app/backup/elk" #es备份数据目录
filename=`date +%Y%m%d%H` #快照名称
ipAddress=172.20.32.241 #IP
backupBeforeDelete=yes #备份之前是否删除旧备份
echo "*******************************"
echo "* ES备份开始 *"
echo "*******************************"
#当前安装目录
currentpath=$(pwd)
echo "**当前安装目录:" ${currentpath}
#设置备份目录
echo "1.设置备份目录"
echo " * 修改elasticsearch.yml"
#查找配置文件是否存在字符串
if cat ${esDir}/config/elasticsearch.yml |grep "path.repo:"
then
#找到
echo " * elasticsearch.yml无需修改"
else
#未找到
#cat elasticsearch.yml |grep "path.repo:"
#echo "path.repo: ["'${es_backup}'"]">> ${esDir}/config/elasticsearch.yml
res=`echo $?`
if [ $res = "1" ]; then
echo -e "\033[31m修改elasticsearch.yml失败 \033[0m"
exit 1
fi
fi
echo " * 创建ES数据备份目录以及权限*"
mkdir -p ${es_backup}
chmod 755 ${es_backup}
chown es:es ${es_backup}
res=`echo $?`
if [ $res = "1" ]; then
echo -e "\033[31m创建ES数据备份目录失败 \033[0m"
exit 1
fi
#检测es访问地址是否有效
esStatus=$(curl -s -m 5 -IL http://${ipAddress}:9200|grep 200)
if [ "$esStatus" != "" ];then
echo $(date +'%Y-%m-%d %H:%M:%S') "es地址访问正常:http://${ipAddress}:9200,开始备份数据"
#创建仓库
#set -x #打印上一步命令
curl -H "Content-Type: application/json" -XPUT 'http://'${ipAddress}':9200/_snapshot/datasvr' -d ' {"type":"fs","settings":{"location":"'${es_backup}'","compress":true}}'
#set +x
res=`echo $?`
if [ $res = "1" ]; then
echo -e "\033[31m创建仓库失败 \033[0m"
exit 1
fi
echo -e " \n创建仓库成功"
#删除快照snapshot_1
curl -X DELETE "localhost:9200/_snapshot/datasvr/snapshot_1"
echo -e " \n删除旧快照snapshot_1成功"
#创建快照snapshot_1(备份索引)
curl -H "Content-Type:application/json" -XPUT ''${ipAddress}':9200/_snapshot/datasvr/snapshot_1?wait_for_completion=true'
res=`echo $?`
if [ $res = "1" ]; then
echo -e "\033[31m 创建快照snapshot_1失败 \033[0m"
exit 1
fi
echo -e " \n创建快照snapshot_1成功"
else
echo -e "\033[31m $(date +'%Y-%m-%d %H:%M:%S') es地址访问异常:http://${ipAddress}:9200 \033[0m"
exit 1
fi
echo "*******************************"
echo "* ES备份完成 *"
echo "*******************************"
脚本执行结果:
2. 恢复
#!/bin/bash
currentUser=$(whoami)
if [ "$currentUser" != "root" ];then
echo "当前用户为非root,请切换用户:${currentUser}"
exit 1
fi
ipAddress=172.20.32.241 #IP
echo "*******************************"
echo "* ES恢复备份开始 *"
echo "*******************************"
#当前安装目录
currentpath=$(pwd)
echo "**当前安装目录:" ${currentpath}
#检测es访问地址是否有效
esStatus=$(curl -s -m 5 -IL http://${ipAddress}:9200|grep 200)
if [ "$esStatus" != "" ];then
echo " * es地址访问正常:http://${ipAddress}:9200"
#快照恢复
curl -H "Content-Type:application/json" -XPOST '127.0.0.1:9200/_snapshot/datasvr/snapshot_1/_restore'
res=`echo $?`
if [ $res = "1" ]; then
echo -e "\033[31m * 快照恢复snapshot_1失败 \033[0m"
exit 1
fi
echo -e " \n * 快照恢复snapshot_1成功"
else
echo -e "\033[31m * es地址访问异常:http://${ipAddress}:9200 \033[0m"
exit 1
fi
echo "*******************************"
echo "* ES恢复备份完成 *"
echo "*******************************"
执行快照恢复脚本运行结果: