-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticbackup.sh
executable file
·216 lines (197 loc) · 7.03 KB
/
elasticbackup.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
##
# [Elasticbackup agent]
# @author orykami <88.jacquot.benoit@gmail.com>
##
# Hostname
HOST=${HOSTNAME}
# Elasticbackup agent configuration path
CONFIG_PATH="/etc/elasticbackup.conf"
# Logger arguments
LOG_ARGS="-s -t elasticbackup"
# Path to `jq` command on your system
JQ="$(which jq)"
# Path to `curl` command on your system
CURL="$(which curl)"
# Elasticsearch cluster URL (with port if required)
ES_URL="http://localhost:9200"
# Name of the repository used to create backup on ES
ES_REPO_NAME="backup"
# Repository type (supported : fs)
ES_REPO_TYPE="fs"
# Repository settings as json object
ES_REPO_SETTINGS_JSON=""
# Maximum snapshot to preserve (int)
ES_SNAPSHOT_PRESERVE_COUNT=48
# Webhook URL to notify for backup status
SLACK_WEBHOOK_URL=""
# Snapshot prefix
ES_SNAPSHOT_PREFIX="elasticbackup"
# Current run date
RUN_DATE="$(date +%F_%H-%M)"
# Timestamp when the script start to run
START_TIME=`date +%s`
##
# Retrieve configuration file from argument
##
while [[ $# > 1 ]]; do
case $1 in
# Configuration file (-c|--config)
-c|--config)
shift
CONFIG_PATH="$1"
;;
esac
shift
done
##
# Load script configuration from specified path, exit otherwise
##
if [[ -f ${CONFIG_PATH} ]]
then
. ${CONFIG_PATH}
else
logger -p user.err -s "Configuration file ${CONFIG_PATH} not found."
exit 1
fi
# Generation of next snapshot name
NEXT_SNAPSHOT_NAME="${ES_SNAPSHOT_PREFIX}-${RUN_DATE}"
##
# Write log to stdout/syslog
# @param $1 log.level Log level
# @param $2 Log message
##
log() {
logger -p "$1" ${LOG_ARGS} "$2"
return 0
}
##
# Notify slack via webhook with arguments
# @param $1 Notification message
#
##
notify_slack() {
# Notify #devops on Slack network if webhook is specified
if [[ -n ${SLACK_WEBHOOK_URL} ]]; then
printf -v JSON '{"text":"[%s][%s] %s"}' ${HOST} ${RUN_DATE} "$1"
${CURL} -X POST -H 'Content-type: application/json' --data --insecure "$JSON" ${SLACK_WEBHOOK_URL} > /dev/null 2>&1
fi
return 0
}
##
# Main script ()
##
log user.info "Start elasticbackup.sh (Elasticsearch backup agent)"
# Check if command `jq` is available
if [[ -z ${JQ} ]]; then
ERROR_MESSAGE="Command 'jq' not found, use sudo apt-get install jq first !"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
exit 1
fi
# Check if command `curl` is available
if [[ -z ${CURL} ]]; then
ERROR_MESSAGE="Command 'curl' not found, use sudo apt-get install curl first !"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
exit 1
fi
# Check if ES_REPO_NAME is defined
if [[ -z ${ES_REPO_NAME} ]]; then
ERROR_MESSAGE="Missing configuration ES_REPO_NAME"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
exit 1
fi
# Check to make sure elasticsearch is actually up and running
ES_STATE=$(${CURL} -s ${ES_URL}/_cluster/health | ${JQ} --raw-output .status)
if [[ ${ES_STATE} == "green" ]] || [[ ${ES_STATE} == "yellow" ]]; then
log user.info "Elasticsearch cluster ${ES_URL} is up (status : ${ES_STATE})"
else
ERROR_MESSAGE="Elasticsearch cluster ${ES_URL} seems down"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
exit 1;
fi
# Check to see if the repo exists and create if necessary
ES_REPO_EXISTS=$(${CURL} -s ${ES_URL}/_snapshot/ | jq --raw-output "has(\"${ES_REPO_NAME}\")")
if [[ "false" == ${ES_REPO_EXISTS} ]]; then
log user.info "Repository ${ES_REPO_NAME} not found, create it from settings"
# Check if ES_REPO_TYPE is defined
if [[ -z ${ES_REPO_TYPE} ]]; then
ERROR_MESSAGE="Missing configuration ES_REPO_TYPE for repository creation"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
exit 1;
fi
# Check if ES_REPO_SETTINGS_JSON is defined
if [[ -z ${ES_REPO_SETTINGS_JSON} ]]; then
ERROR_MESSAGE="Missing configuration ES_REPO_SETTINGS_JSON for repository creation"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
exit 1;
fi
# Create PUT request to ES cluster
RESPONSE=$(${CURL} -s -X PUT ${ES_URL}/_snapshot/${ES_REPO_NAME} -H 'Content-Type: application/json' -d "{\"type\":\"${ES_REPO_TYPE}\",\"settings\":${ES_REPO_SETTINGS_JSON}}")
if [[ $(echo ${RESPONSE} | jq --raw-output .acknowledged) == "true" ]]; then
log user.info "Repository [${ES_REPO_TYPE}:${ES_REPO_NAME}] created on ES"
else
ERROR_MESSAGE="Repository [${ES_REPO_TYPE}:${ES_REPO_NAME}] creation failed : ${RESPONSE}"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
exit 1
fi
fi
# Check for existing number of snapshots
SNAPSHOT_LIST=$(${CURL} -s ${ES_URL}/_snapshot/${ES_REPO_NAME}/_all)
# If the current snapshot already exists, we skip
SNAPSHOT_EXISTS=$(echo ${SNAPSHOT_LIST} | jq --raw-output ".snapshots[] | .snapshot" | grep -c ${NEXT_SNAPSHOT_NAME})
if [[ ${SNAPSHOT_EXISTS} -gt 0 ]]; then
log user.info "Snapshot '${NEXT_SNAPSHOT_NAME}' already in progress/done, skip"
exit 0
fi
# No deletions needed, so take a snapshot!
RESPONSE=$(${CURL} -s -X PUT "${ES_URL}/_snapshot/${ES_REPO_NAME}/${NEXT_SNAPSHOT_NAME}?wait_for_completion=true")
if [[ "$(echo ${RESPONSE} | jq --raw-output .snapshot.state)" == "SUCCESS" ]]; then
log user.info "Snapshot '${NEXT_SNAPSHOT_NAME}' created"
else
ERROR_MESSAGE="Failed to create snapshot '${NEXT_SNAPSHOT_NAME}' : ${RESPONSE}"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
exit 1;
fi
##
# Refresh snapshot list from ES server
##
SNAPSHOT_LIST=$(${CURL} -s ${ES_URL}/_snapshot/${ES_REPO_NAME}/_all)
##
# Pruning previous snapshots if required
##
SNAPSHOT_COUNT=$(echo ${SNAPSHOT_LIST} | jq "[.snapshots[] | select(.snapshot | startswith(\"${ES_SNAPSHOT_PREFIX}\")) | .snapshot] | length")
# Check if we need to delete any outdated snapshot
log user.info "Current snapshot count : ${SNAPSHOT_COUNT}/${ES_SNAPSHOT_PRESERVE_COUNT}"
if [[ ${SNAPSHOT_COUNT} -gt ${ES_SNAPSHOT_PRESERVE_COUNT} ]]; then
TO_PURGE_SNAPSHOT_COUNT=$(expr ${SNAPSHOT_COUNT} - ${ES_SNAPSHOT_PRESERVE_COUNT})
if [[ ${TO_PURGE_SNAPSHOT_COUNT} -gt 0 ]]; then
log user.info "Start pruning ${TO_PURGE_SNAPSHOT_COUNT} snapshot(s)"
for OLD_SNAPSHOT in $(echo ${SNAPSHOT_LIST} | jq --raw-output "[.snapshots[] | select(.snapshot | startswith(\"$ES_SNAPSHOT_PREFIX\")) | .snapshot][0:${TO_PURGE_SNAPSHOT_COUNT}] | .[]"); do
RESPONSE=$(${CURL} -s -X DELETE ${ES_URL}/_snapshot/${ES_REPO_NAME}/${OLD_SNAPSHOT})
if [[ "$(echo ${RESPONSE} | jq --raw-output .acknowledged)" == "true" ]]; then
log user.info "Snapshot ${OLD_SNAPSHOT} pruned"
else
ERROR_MESSAGE="Failed to remove snapshot ${OLD_SNAPSHOT} : $(echo ${RESPONSE} | jq --raw-output .error)"
log user.err "${ERROR_MESSAGE}"
notify_slack "${ERROR_MESSAGE}"
fi
done
fi
fi
# Create log entry for backup trace
DURATION=$((`date +%s` - ${START_TIME}))
log user.info "Backup completed in ${DURATION} seconds"
# Notify #devops on Slack network if webhook is specified
if [[ -n ${SLACK_WEBHOOK_URL} ]]; then
SUCCESS_MESSAGE="ES snapshot ${NEXT_SNAPSHOT_NAME} completed in ${DURATION} second(s)"
notify_slack "${SUCCESS_MESSAGE}"
fi
exit 0