Skip to content

Commit baa08d4

Browse files
authored
fix[elasticsearch]: 6.x and 7.x backup failed (#2071) (#2265)
1 parent e512945 commit baa08d4

File tree

11 files changed

+314
-565
lines changed

11 files changed

+314
-565
lines changed

addons/elasticsearch/dataprotection/backup.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ trap handle_exit EXIT
2828

2929
function getToolConfigValue() {
3030
local var=$1
31-
cat $toolConfig | grep "$var[[:space:]]*=" | awk '{print $NF}'
31+
cat $toolConfig | grep "${var}[[:space:]]*=" | awk '{print $NF}'
3232
}
3333

3434
s3_endpoint=$(getToolConfigValue endpoint)
@@ -94,6 +94,18 @@ done
9494
echo "INFO: All nodes keystore configured, reloading secure settings"
9595
curl -X POST "${ES_ENDPOINT}/_nodes/reload_secure_settings"
9696

97+
# Wait for secure settings to be reloaded and plugins to be loaded
98+
echo "INFO: Waiting for nodes to be ready after reloading secure settings..."
99+
sleep 10
100+
101+
# Verify that S3 repository plugin is available
102+
echo "INFO: Checking if S3 repository plugin is available..."
103+
if curl -s -f "${ES_ENDPOINT}/_snapshot" | grep -q 's3'; then
104+
echo "INFO: S3 repository plugin is available"
105+
else
106+
echo "WARNING: S3 repository plugin may not be available, attempting to create repository anyway"
107+
fi
108+
97109
# DP_BACKUP_BASE_PATH is the path to the backup directory
98110
# if the target policy is All, the path pattern is: /${namespace}/${clusterName}-${clusterUID}/${componentDef}/${backupName}/${podName}
99111
# if the target policy is Any, the path pattern is: /${namespace}/${clusterName}-${clusterUID}/${componentDef}/${backupName}
@@ -105,7 +117,7 @@ base_path=${base_path#*/}
105117

106118
function wait_for_snapshot_completion() {
107119
while true; do
108-
state=$(curl -s -X GET "${ES_ENDPOINT}/_snapshot/${REPOSITORY}/${DP_BACKUP_NAME}?sort=name&pretty" | grep -w state | awk '{print $NF}' | tr -d ',"')
120+
state=$(curl -s -X GET "${ES_ENDPOINT}/_snapshot/${REPOSITORY}/${DP_BACKUP_NAME}?pretty" | grep -w state | awk '{print $NF}' | tr -d ',"')
109121
if [ "$state" == "SUCCESS" ]; then
110122
echo "INFO: backup success"
111123
break

addons/elasticsearch/dataprotection/restore.sh

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trap handle_exit EXIT
2222

2323
function getToolConfigValue() {
2424
local var=$1
25-
cat $toolConfig | grep "$var[[:space:]]*=" | awk '{print $NF}'
25+
cat $toolConfig | grep "${var}[[:space:]]*=" | awk '{print $NF}'
2626
}
2727

2828
s3_endpoint=$(getToolConfigValue endpoint)
@@ -93,6 +93,25 @@ done
9393
echo "INFO: All nodes keystore configured for restore, reloading secure settings"
9494
curl -X POST "${ES_ENDPOINT}/_nodes/reload_secure_settings"
9595

96+
# Get Elasticsearch major version (call once at the beginning)
97+
function get_es_major_version() {
98+
local version_info=$(curl -s ${BASIC_AUTH} -X GET "${ES_ENDPOINT}/_nodes" | grep -o '"version":"[^"]*"' | head -1 | cut -d'"' -f4)
99+
if [ -z "$version_info" ]; then
100+
echo "ERROR: Failed to get Elasticsearch version" >&2
101+
return 1
102+
fi
103+
# Extract major version (e.g., "7.10.1" -> "7")
104+
echo "$version_info" | cut -d'.' -f1
105+
}
106+
107+
echo "INFO: Getting Elasticsearch version"
108+
es_major_version=$(get_es_major_version)
109+
if [ $? -ne 0 ]; then
110+
echo "ERROR: Failed to get Elasticsearch version"
111+
exit 1
112+
fi
113+
echo "INFO: Detected Elasticsearch major version: $es_major_version"
114+
96115
cat > /tmp/repository.json<< EOF
97116
{
98117
"type": "s3",
@@ -120,14 +139,20 @@ function enable_indexing_and_geoip() {
120139
exit 1
121140
;;
122141
esac
123-
curl -f -X PUT "${ES_ENDPOINT}/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
124-
{
125-
"persistent": {
126-
"ingest.geoip.downloader.enabled": '$switch',
127-
"indices.lifecycle.history_index_enabled": '$switch'
128-
}
129-
}
130-
'
142+
143+
# Check if version is less than 8.0
144+
if [ "$es_major_version" -lt 8 ]; then
145+
echo "WARNING: Elasticsearch version is $es_major_version.x, which does not support dynamically updating 'indices.lifecycle.history_index_enabled' and 'ingest.geoip.downloader.enabled'. "
146+
else
147+
curl -f -X PUT "${ES_ENDPOINT}/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
148+
{
149+
"persistent": {
150+
"ingest.geoip.downloader.enabled": '$switch',
151+
"indices.lifecycle.history_index_enabled": '$switch'
152+
}
153+
}
154+
'
155+
fi
131156
}
132157

133158
function switch_ilm() {
@@ -258,7 +283,12 @@ fi
258283
enable_destructive_requires_name false
259284

260285
# Delete all existing data streams on the cluster.
261-
curl -f -s -X DELETE "${ES_ENDPOINT}/_data_stream/*?expand_wildcards=all&pretty"
286+
# expand_wildcards parameter is not supported in ES 7.x for data stream deletion
287+
if [ "$es_major_version" -lt 8 ]; then
288+
curl -f -s -X DELETE "${ES_ENDPOINT}/_data_stream/*?pretty"
289+
else
290+
curl -f -s -X DELETE "${ES_ENDPOINT}/_data_stream/*?expand_wildcards=all&pretty"
291+
fi
262292

263293
# Delete all existing indices on the cluster.
264294
curl -f -s -X DELETE "${ES_ENDPOINT}/*?expand_wildcards=all&pretty"

addons/elasticsearch/plugins/Dockerfile

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ FROM alpine:3.19.1
22

33
WORKDIR /plugins
44

5-
RUN wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.8.2/elasticsearch-analysis-ik-8.8.2.zip \
6-
&& unzip elasticsearch-analysis-ik-8.8.2.zip -d ./analysis-ik/ \
7-
&& rm elasticsearch-analysis-ik-8.8.2.zip
5+
# Install all plugins
6+
RUN set -e && \
7+
# IK plugins for all versions \
8+
for version in 6.8.23 7.7.1 7.8.1 7.10.1 8.1.3 8.8.2 8.15.5; do \
9+
echo "Installing IK plugin for Elasticsearch $version"; \
10+
mkdir -p ./${version}/ik && \
11+
wget -q https://release.infinilabs.com/analysis-ik/stable/elasticsearch-analysis-ik-${version}.zip && \
12+
unzip -q elasticsearch-analysis-ik-${version}.zip -d ./${version}/ik/ && \
13+
rm elasticsearch-analysis-ik-${version}.zip; \
14+
done && \
15+
\
16+
# S3 plugins for 6.x and 7.x (8.x has it built-in) \
17+
for version in 6.8.23 7.7.1 7.8.1 7.10.1; do \
18+
echo "Installing S3 plugin for Elasticsearch $version"; \
19+
mkdir -p ./${version}/s3 && \
20+
wget -q https://artifacts.elastic.co/downloads/elasticsearch-plugins/repository-s3/repository-s3-${version}.zip && \
21+
unzip -q repository-s3-${version}.zip -d ./${version}/s3/ && \
22+
rm repository-s3-${version}.zip; \
23+
done && \
24+
\
25+
echo "All plugins installed successfully"

addons/elasticsearch/scripts/install-plugins.sh

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ if [ ! -d $src_plugins_dir ]; then
1010
exit 0
1111
fi
1212

13+
# [root@35bde36fc986 elasticsearch]# elasticsearch --version
14+
# Version: 7.10.1, Build: default/docker/1c34507e66d7db1211f66f3513706fdf548736aa/2020-12-05T01:00:33.671820Z, JVM: 15.0.1
15+
ELASTICSEARCH_VERSION=$(elasticsearch --version | grep "Version:" | awk '{print $2}' | sed 's/,.*//')
16+
17+
if [ -z "$ELASTICSEARCH_VERSION" ]; then
18+
echo "ELASTICSEARCH_VERSION is not set"
19+
exit 1
20+
fi
21+
1322
function native_install_plugin() {
1423
plugin=$1
1524
msg=`/usr/share/elasticsearch/bin/elasticsearch-plugin install -b $plugin`
@@ -36,12 +45,21 @@ function copy_install_plugin() {
3645
echo "successfully installed plugin $plugin"
3746
}
3847

39-
for plugin in $(ls $src_plugins_dir); do
40-
# check if plugin has suffix .zip or .gz or .tar.gz
41-
echo "installing plugin $plugin"
42-
if [[ $plugin == *.zip || $plugin == *.gz || $plugin == *.tar.gz ]]; then
43-
native_install_plugins $src_plugins_dir/$plugin
44-
else
45-
copy_install_plugin $src_plugins_dir/$plugin
46-
fi
47-
done
48+
# Install version-specific plugins - simply install all plugins that exist for this version
49+
echo "Installing plugins for Elasticsearch version $ELASTICSEARCH_VERSION"
50+
51+
# Check if version-specific plugin directory exists
52+
if [ -d "$src_plugins_dir/$ELASTICSEARCH_VERSION" ]; then
53+
echo "Found plugin directory for version $ELASTICSEARCH_VERSION"
54+
55+
# Install all plugin subdirectories that exist
56+
for plugin_dir in "$src_plugins_dir/$ELASTICSEARCH_VERSION"/*/; do
57+
if [ -d "$plugin_dir" ]; then
58+
plugin_name=$(basename "$plugin_dir")
59+
echo "Installing $plugin_name plugin for version $ELASTICSEARCH_VERSION"
60+
copy_install_plugin "$plugin_dir"
61+
fi
62+
done
63+
else
64+
echo "No plugin directory found for version $ELASTICSEARCH_VERSION"
65+
fi

0 commit comments

Comments
 (0)