Elasticsearch使用shell脚本数据备份和恢复

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 "*******************************"

执行快照恢复脚本运行结果: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值