Last active
July 7, 2021 03:42
-
-
Save roy4801/0b5feea34801323523f79bc5995e9af0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Do not add the trailing slash | |
INPUT_HOST=127.0.0.1 | |
INPUT_PORT=1859 | |
INPUT_URL=$INPUT_HOST:$INPUT_PORT | |
OUTPUT_HOST= | |
OUTPUT_PORT=1859 | |
OUTPUT_URL=$OUTPUT_HOST:$OUTPUT_PORT | |
INPUT_INDEX=.kibana_1 | |
OUTPUT_INDEX=.kibana_1 | |
USER_PASSWORD=kibana:nspectrum | |
declare -a indexType=("settings" "mapping" "data") | |
# "template" "component_template" "index_template" "analyzer" "policy" "alias" | |
INDEX_LIST=`curl -u ${USER_PASSWORD} http://${INPUT_URL}/_cat/indices 2>/dev/null| tr -s ' ' | awk -F" " '{print $3}' | grep -E '^\.' | sort | xargs` | |
export_es() { | |
for j in ${INDEX_LIST}; do | |
for i in "${indexType[@]}"; do | |
printf "\u001b[34m" | |
echo Dumping $i of index $j | |
printf "\u001b[0m" | |
TARGET=$j | |
OUTPUT_FILENAME=`echo $TARGET | sed -e 's/^\.//'`_${i}.json | |
elasticdump --input=http://${USER_PASSWORD}@${INPUT_URL}/${TARGET} --output=${OUTPUT_FILENAME} --type=${i} | |
done | |
done | |
return 0 | |
} | |
# import_es <file_prefix> | |
import_es() { | |
for j in "${indexType[@]}"; do | |
TYPE=$j | |
printf "\u001b[34m" | |
echo Loading $TYPE from file $j | |
printf "\u001b[0m" | |
elasticdump --input=$1_$j.json --output=http://${USER_PASSWORD}@${OUTPUT_URL}/${OUTPUT_INDEX} --type=$TYPE | |
done | |
return 0 | |
} | |
print_usage() { | |
echo "Usage: $0 [-f|-t|-F|-T|--import|--export|--clean] - Dump ES" | |
echo -e "\t-f <host>[:port] From host:port" | |
echo -e "\t-t <host>[:port] To host:port" | |
echo -e "\t-F <index> From index (default: .kibana_1)" | |
echo -e "\t-T <index> To index (default: .kibana_1)" | |
echo -e "\t--import <prefix> Import from file" | |
echo -e "\t--export Export to file" | |
echo -e "\t-c|--clean Clean" | |
echo -e "\nExamples:" | |
echo -e "\tDump from index .kibana_1 to index .kibana_test123 at 192.168.2.x" | |
echo -e "\t`basename $0` -F .kibana_1 -t 192.168.2.x -T .kibana_test123" | |
echo | |
echo -e "\tExport to files" | |
echo -e "\t`basename $0` --export" | |
echo | |
echo -e "\tImport from files (kibana_1_*.json)" | |
echo -e "\t`basename $0` --import kibana_1 -t 127.0.0.1 -T .kibana_test" | |
echo | |
echo -e "\tClean the env" | |
echo -e "\t`basename $0` -c" | |
} | |
abort() { | |
printf "\u001b[31m" | |
printf "$1\n" | |
printf "\u001b[0m\n" | |
print_usage | |
exit 1 | |
} | |
SELECTED=0 | |
OK=0 | |
DO_IMPORT=0 | |
DO_IMPORT_ARG= | |
DO_EXPORT=0 | |
POSITIONAL=() | |
while [[ $# -gt 0 ]]; do | |
key="$1" | |
case $key in | |
# Arguments | |
-f|--from) | |
INPUT_URL=$2 | |
shift | |
shift | |
;; | |
-t|--to) | |
if [[ "$2" == *":"* ]]; then | |
OUTPUT_URL=$2 | |
else | |
OUTPUT_HOST=$2 | |
OUTPUT_URL=$OUTPUT_HOST:$OUTPUT_PORT | |
fi | |
shift | |
shift | |
;; | |
-F|--from-index) | |
INPUT_INDEX=$2 | |
shift | |
shift | |
;; | |
-T|--to-index) | |
OUTPUT_INDEX=$2 | |
shift | |
shift | |
;; | |
# Main methods | |
--export) | |
SELECTED=1 | |
DO_EXPORT=1 | |
shift | |
;; | |
--import) | |
SELECTED=1 | |
DO_IMPORT=1 | |
shift | |
DO_IMPORT_ARG=$1 | |
shift | |
;; | |
-c|--clean) | |
SELECTED=1 | |
shift | |
rm -f ./*.json | |
echo Clean | |
;; | |
-h|--help) | |
SELECTED=1 | |
shift | |
print_usage $* | |
exit 0 | |
;; | |
*) # unknown option | |
POSITIONAL+=("$1") # save it in an array for later | |
shift # past argument | |
;; | |
esac | |
done | |
if [[ $DO_IMPORT -eq 1 ]]; then | |
if [[ "$OUTPUT_HOST" == "" ]]; then | |
abort "Not specified target host:port (-t)" | |
fi | |
import_es $DO_IMPORT_ARG | |
if [[ $? -eq 0 ]]; then | |
printf "\u001b[32mCompleted to import. \u001b[0m\n" | |
exit 0 | |
fi | |
fi | |
if [[ $DO_EXPORT -eq 1 ]]; then | |
export_es | |
if [[ $? -eq 0 ]]; then | |
printf "\u001b[32mCompleted to export. \u001b[0m\n" | |
exit 0 | |
fi | |
fi | |
if [[ $SELECTED -eq 0 ]] && [[ $OK -ge 1 ]]; then | |
SUCC=1 | |
printf "\u001b[36m" | |
echo Start to dump index from $INPUT_URL/$INPUT_INDEX to $OUTPUT_URL/$OUTPUT_INDEX | |
printf "\u001b[0m" | |
for i in "${indexType[@]}"; do | |
printf "\u001b[34m" | |
echo Dumping $i | |
printf "\u001b[0m" | |
elasticdump --input=http://${USER_PASSWORD}@${INPUT_URL}/${INPUT_INDEX} --output=http://${USER_PASSWORD}@${OUTPUT_URL}/${OUTPUT_INDEX} --type=${i} | |
if ! [[ $? -eq 0 ]]; then | |
printf "\u001b[31m" | |
printf "Failed to dump $i" | |
printf "\u001b[0m\n" | |
SUCC=0 | |
break | |
fi | |
done | |
if [[ $SUCC -eq 1 ]]; then | |
printf "\u001b[32m" | |
echo Succeeded to dump index from $INPUT_URL/$INPUT_INDEX to $OUTPUT_URL/$OUTPUT_INDEX | |
printf "\u001b[0m\n" | |
fi | |
else | |
print_usage | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment