-
Notifications
You must be signed in to change notification settings - Fork 110
/
elasticsearch-restore-index.sh
executable file
·141 lines (120 loc) · 3.13 KB
/
elasticsearch-restore-index.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
#
# elasticsearch-restore-index.sh
#
# Retrieve a specified logstash index from s3 and restore with an accompanying
# restore script.
# http://logstash.net
# http://www.elasticsearch.org
# https://github.com/s3tools/s3cmd | http://s3tools.org/s3cmd
#
# Inspiration:
# http://tech.superhappykittymeow.com/?p=296
#
# Must run on an elasticsearch node with data, the restore script restarts
# elasticsearch.
usage()
{
cat << EOF
elasticsearch-restore-index.sh
USAGE: ./elasticsearch-restore-index.sh -b S3_BUCKET [OPTIONS]
OPTIONS:
-h Show this message
-b S3 path for backups (Required)
-i Elasticsearch index directory (Required)
-d Date to retrieve (Required, format: YYYY.mm.dd)
-t Temporary directory for download and extract (default: /tmp)
-c Command for s3cmd (default: s3cmd get)
-e Elasticsearch URL (default: http://localhost:9200)
-n How nice tar must be (default: 19)
EXAMPLES:
./elasticsearch-restore-index.sh -b "s3://someBucket" -i /mnt/es/data/nodes/0/indices \
-d "2013.05.01"
Get the backup and restore script for the 2013.05.01 index from this s3
bucket and restore the index to the provided elasticsearch index directory.
EOF
}
if [ "$USER" != 'root' ] && [ "$LOGNAME" != 'root' ]; then
# I don't want to troubleshoot the permissions of others
echo "This script must be run as root."
exit 1
fi
# Defaults
S3CMD="s3cmd get"
ELASTICSEARCH="http://localhost:9200"
NICE=19
TMP_DIR="/tmp"
while getopts ":b:i:t:d:c:e:n:h" flag
do
case "$flag" in
h)
usage
exit 0
;;
b)
S3_BASE=$OPTARG
;;
i)
INDEX_DIR=$OPTARG
;;
t)
TMP_DIR=$OPTARG
;;
d)
DATE=$OPTARG
;;
c)
S3CMD=$OPTARG
;;
e)
ELASTICSEARCH=$OPTARG
;;
n)
if [[ $OPTARG =~ $RE_D ]]; then
NICE=$OPTARG
fi
# If nice is not an integer, just use default
;;
?)
usage
exit 1
;;
esac
done
# We need an S3 base path
if [ -z "$S3_BASE" ]; then
ERROR="${ERROR}Please provide an s3 bucket and path with -b.\n"
fi
# We need an elasticsearch index directory
if [ -z "INDEX_DIR" ]; then
ERROR="${ERROR}Please provide an Elasticsearch index directory with -i.\n"
fi
# We need a date to restore
if [ -z "$DATE" ]; then
ERROR="${ERROR}Please provide a date for restoration with -d.\n"
fi
# If we have errors, show the errors with usage data and exit.
if [ -n "$ERROR" ]; then
echo -e $ERROR
usage
exit 1
fi
# Default logstash index naming is hardcoded, as are YYYY-mm container directories.
INDEX="logstash-$DATE"
YEARMONTH=${DATE//\./-}
YEARMONTH=${YEARMONTH:0:7}
S3_TARGET="$S3_BASE/$YEARMONTH"
# Get archive and execute the restore script. TODO check file existence first
$S3CMD $S3_TARGET/$INDEX.tgz $TMP_DIR/$INDEX.tgz
$S3CMD $S3_TARGET/$INDEX-restore.sh $TMP_DIR/$INDEX-restore.sh
if [ -f $TMP_DIR/$INDEX-restore.sh ]; then
chmod 750 $TMP_DIR/$INDEX-restore.sh
$TMP_DIR/$INDEX-restore.sh
# cleanup tmp files
rm $TMP_DIR/$INDEX.tgz
rm $TMP_DIR/$INDEX-restore.sh
else
echo "Unable to find restore script, does that backup exist?"
exit 1
fi
exit 0