elasticsearch备份恢复

  sre

backup_es.sh

#!/bin/bash
ip_addr=127.0.0.1
resposity_name=es_backup
snapshot_location_path=/home/user1/program/backups/es_backup/resposity_name
snapshot_tar_path=/home/user1/program/backups/es_backup/tarPackage
snapshot_name=test_snapshot
port=9200
#filename=`date +%Y%m%d%H`  增量时使用
tarFileName="resposity_name"_"snapshot_name".tar.gz
mkdir -psnapshot_location_path
mkdir -p  snapshot_tar_path

chmod -R 777snapshot_location_path

#创建快照仓库
echo -e '\n*******create _snapshot resposity 'resposity_name'\n'

curl -X PUT "ip_addr:port/_snapshot/resposity_name" -H 'Content-Type: application/json' -d '{ "type": "fs", "settings": { "location": "'snapshot_location_path'" } }'

#curl -XPUT localhost:9200/_snapshot/es_snapshot -H 'Content-Type: application/json' -d '{"type":"fs","settings":{"location":"'/home/user1/program/backups/es_backup/es_backup'"}}'

curl -X GET "ip_addr:port/_snapshot/resposity_name/_current"
echo -e '\n*******DELETE _snapshot 'snapshot_name', sleep 5\n'
curl -XDELETE "ip_addr:port/_snapshot/resposity_name/snapshot_name?"
sleep 5
#全量备份 
#curl -X PUT "ip_addr:port/_snapshot/resposity_name/snapshot_name?wait_for_completion=false"

#索引备份
curl -X PUT "ip_addr:port/_snapshot/resposity_name/snapshot_name?wait_for_completion=false" -H 'Content-Type: application/json' -d '{ "indices": "monitor_quota_20200311", "ignore_unavailable": true, "include_global_state": false }'
sleep 10

#查看备份情况
curl -X GET "ip_addr:port/_snapshot/resposity_name/snapshot_name?pretty"

cdsnapshot_location_path
echo -e '\n******* tar package 'tarFileName' ,sleep 5\n'
tar --warning=no-file-changed  -czvftarFileName  ./*
mv tarFileNamesnapshot_tar_path
rm  ./* -rf
echo -e '\nes data backup succeed!\n'
echo -e '\n package in 'snapshot_tar_path' 'tarFileName' \n'

restore_es.sh

#!/bin/bash
ip_addr=127.0.0.1
resposity_name=es_backup
snapshot_name=test_snapshot
port=9200
snapshot_location_path=/home/user1/program/backups/es_backup/resposity_name
snapshot_tar_path=/home/user1/program/backups/es_backup/tarPackage
# filename=`date +%Y%m%d%H`  增量时使用
tarFileName="resposity_name"_"snapshot_name".tar.gz
mkdir -psnapshot_location_path
mkdir -p  snapshot_tar_path
chmod -R 777snapshot_location_path
echo -e '\n*******create resposity:'resposity_name' /n'
curl -XPUTip_addr:port/_snapshot/resposity_name -H 'Content-Type: application/json' -d '{"type":"fs","settings":{"location":"'snapshot_location_path'"}}'

rmsnapshot_location_path/* -rf
cp snapshot_tar_path/tarFileName snapshot_location_path
cdsnapshot_location_path
echo -e '\n*******tar tarFileName: 'tarFileName' sleep5\n'
echo ' ********tar -zxvf 'tarFileName'/n'
tar -zxvf "tarFileName"

sleep 3
#查看快照
curl -X GET "ip_addr:port/_snapshot/resposity_name/snapshot_name?pretty"
#rmsnapshot_location_path/tarFileName -rf

#关闭索引  curl -XPOSTip_addr:port/_all/_close
curl -XPOSTip_addr:port/monitor_quota_20200311/_close
echo -e '\n*******_restore _snapshot sleep 5\n'
curl -XPOSTip_addr:port/_snapshot/resposity_name/snapshot_name/_restore?
## 恢复单个索引: curl -XPOST 'http://ip_addr:port/_snapshot/resposity_name/snapshot_name/_restore' -d '{"indices": "index_0101","rename_replacement": "index_0101"}'

sleep 5
#打开索引  curl -XPOST localhost:9200/monitor_quota_20200311/_open  curl -XPOSTip_addr:port/_all/_open
curl -XPOSTip_addr:$port/monitor_quota_20200311/_open
echo -e '\n es data restore succeed!'

LEAVE A COMMENT

Captcha Code