北青永恒 - Elasticsearch备份脚本
type: channel
link: http://kococ.cn/tag/Elasticsearch备份脚本/
items: ES-Elasticsearch数据库备份恢复脚本

ES-Elasticsearch数据库备份恢复脚本
type: item
date: 2021-01-13T11:50:00+08:00
description: 1.备份数据1.1 创建备份仓库$ curl -u elastic:123456 -XPOST '192.168.1.1:9200/_snapshot/hsbc_backup' -H 'Content-Type: application/json' -d '{ "type": "fs", "settings": { "location": "/alidata1/admin/backup/elasticsearch", "compress": true, "chunk_size": "1g", "max_snapshot_bytes_per_sec": "50m", "max_restore_bytes_per_sec": "50m"}}' 11.2 备份脚本$ cat /alidata1/admin/scripts/elasticsearch/es_bak_del.sh #!/bin/bash ## define vars es_url="http://192.168.1.1:9200" es_bak_repo="hsbc_backup" es_user="elastic" es_pass="123456" es_bak_date=`date +'%Y-%m-%d-%H'` echo -e "\n =========== $es_bak_date ============" ## bak curl -u ${es_user}:${es_pass} -XPUT "${es_url}/_snapshot/${es_bak_repo}/es_bak_${es_bak_date}?wait_for_completion=true" echo "" ##sleep sleep 10 ##del 最后的60: 定时任务 每4小时备份一次 一天备份6次 数据保留10天 es_del_snapshots=`curl -u ${es_user}:${es_pass} -s -XGET "${es_url}/_snapshot/${es_bak_repo}/_all" | python -m json.tool| python -c 'import json; import sys; input = sys.stdin.read(); result = [x["snapshot"] for x in json.loads(input)["snapshots"]]; print("\n".join(result[:-60]));'` for es_del_snapshot in ${es_del_snapshots}; do echo "===========$es_del_snapshot del" curl -u ${es_user}:${es_pass} -s -XDELETE "${es_url}/_snapshot/${es_bak_repo}/${es_del_snapshot}" echo "" done1.3 创建定时任务$ crontab -e 0 */4 * ** /bin/bash /alidata1/admin/scripts/elasticsearch/es_bak_del.sh >> /alidata1/admin/logs/crontab/es_bak/crontab.log 2>&1 122.数据还原目的：将某一个备份的文件还原到192.168.1.1$ cat es_restore.sh #!/bin/bash set -e ## define vars es_url="http://192.168.1.1:9200" es_bak_repo="hsbc_backup" es_restore_snapshots="es_bak_20200811" es_user="elastic" es_pass="123456" ##(1)获取备份文件中存在的索引 es_get_snapshots_index=`curl -u ${es_user}:${es_pass} -s -XGET "${es_url}/_snapshot/${es_bak_repo}/${es_restore_snapshots}" | python -m json.tool| python -c 'import json; import sys; input = sys.stdin.read(); result=json.loads(input); indices = result["snapshots"][0]["indices"]; sys.stdout.write(" ".join([i for i in indices]));'` ##(2)将…