美文网首页
使用Elasticdump备份所有Elasticsearch中的

使用Elasticdump备份所有Elasticsearch中的

作者: 无味wy | 来源:发表于2023-05-15 10:52 被阅读0次

使用以下bash脚本自动备份Elasticsearch中的所有索引。该脚本将遍历索引列表并使用Elasticdump为每个索引创建一个JSON备份文件

#!/bin/bash

# Replace this with your Elasticsearch URL with your credentials
ELASTICSEARCH_URL="http://username:passwd@2021@es-cn-7pp2gm8ni001zx2am.public.elasticsearch.aliyuncs.com:9200"

# Replace this with the path where you want to store the backup files
BACKUP_PATH="/path/to/your/backup/"

# Get the list of indices
indices=$(curl -s -X GET "${ELASTICSEARCH_URL}/_cat/indices?h=index" | xargs)

# Loop through each index and backup using Elasticdump
for index in $indices
do
  echo "Backing up index: $index"
  elasticdump --input="${ELASTICSEARCH_URL}/${index}" --output="${BACKUP_PATH}/${index}.json" --type=data
done

echo "Backup completed."
请确保将ELASTICSEARCH_URL变量更改为包含您的Elasticsearch实例凭据的URL。
更改BACKUP_PATH变量以指向您希望存储备份文件的目录。
保存脚本为backup_elasticsearch.sh。
在命令行上,赋予脚本可执行权限: chmod +x backup_elasticsearch.sh
运行脚本:./backup_elasticsearch.sh
此脚本将遍历您的Elasticsearch实例中的所有索引,并在指定的备份路径下为每个索引创建一个JSON文件。当脚本完成时,所有索引的备份将存储在该目录中

如果无法备份,应该是密码中包含特殊字符 "@" 则需要转义 ,可以使用以下脚本

#!/bin/bash

# Replace this with your Elasticsearch URL with your credentials
ELASTICSEARCH_URL="http://username:passwd%40%32%30%32%31@es-cn-7pp2gm8ni001zx2am.public.elasticsearch.aliyuncs.com:9200"

# Replace this with the path where you want to store the backup files
BACKUP_PATH="/root/esbak/"

# Get the list of indices including a proper username and password
indices=$(curl -s -u "username:passwd@2021" -X GET "${ELASTICSEARCH_URL}/_cat/indices?h=index" | xargs)

# Loop through each index and backup using Elasticdump
for index in $indices
do
  echo "Backing up index: $index"
  elasticdump --input="${ELASTICSEARCH_URL}/${index}" --output="${BACKUP_PATH}/${index}.json" --type=data
done

echo "Backup completed."

恢复Elasticsearch 索引脚本
这个脚本将读取您的备份目录中的每个 JSON 文件并使用 Elasticdump 逐个恢复索引。在运行此脚本之前,请确保 Elasticsearch URL 和备份目录设置为正确的值

#!/bin/bash

# Replace this with your Elasticsearch URL with your credentials
ELASTICSEARCH_URL="http://username:passwd%40%32%30%32%31@es-cn-7pp2gm8ni001zx2am.public.elasticsearch.aliyuncs.com:9200"

# Replace this with the path where you stored the backup files
BACKUP_PATH="/root/esbak/"

# Get the list of backup files
backup_files=$(ls ${BACKUP_PATH}/*.json)

# Loop through each backup file and restore the index using Elasticdump
for file in $backup_files
do
  index=$(basename "$file" .json)
  echo "Restoring index: $index"
  elasticdump --input="${BACKUP_PATH}/${index}.json" --output="${ELASTICSEARCH_URL}/${index}" --type=data
done

echo "Restore completed."

有的备份出来是隐藏文件 ls 无法检索出来,使用下一个脚本
这个脚本现在将查找包括隐藏 JSON 文件在内的备份文件,并使用 ElasticDump 恢复这些文件。请确保运行脚本前已正确设置 Elasticsearch URL 和备份目录

#!/bin/bash

# Replace this with your Elasticsearch URL with your credentials
ELASTICSEARCH_URL="http://username:passwd%40%32%30%32%31@es-cn-7pp2gm8ni001zx2am.public.elasticsearch.aliyuncs.com:9200"

# Replace this with the path where you stored the backup files
BACKUP_PATH="/root/esbak/"

# Get the list of backup files (including hidden files)
backup_files=$(find ${BACKUP_PATH} -name '.*.json' -type f)

# Loop through each backup file and restore the index using Elasticdump
for file in $backup_files
do
  index=$(basename "$file" .json)
  echo "Restoring index: $index"
  elasticdump --input="${BACKUP_PATH}/${index}.json" --output="${ELASTICSEARCH_URL}/${index}" --type=data
done

echo "Restore completed."

相关文章

网友评论

      本文标题:使用Elasticdump备份所有Elasticsearch中的

      本文链接:https://www.haomeiwen.com/subject/uwumsdtx.html