Skip to content

Commit 3367888

Browse files
wangyeleiapecloud-bot
authored andcommitted
chore: support backup and restore sentinel users for full method (#2444)
(cherry picked from commit 79c445c)
1 parent 2464d65 commit 3367888

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

addons/redis/dataprotection/backup.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ trap handle_exit EXIT
1515

1616
export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
1717
export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"
18+
19+
function save_sentinel_acl() {
20+
if [ -z "$SENTINEL_POD_FQDN_LIST" ]; then
21+
return
22+
fi
23+
for sentinel_fqdn in $(echo "$SENTINEL_POD_FQDN_LIST" | tr "," "\n"); do
24+
echo "INFO: save sentinel ${sentinel_fqdn} ACL file"
25+
sentinel_cmd="redis-cli $REDIS_CLI_TLS_CMD -h $sentinel_fqdn -p ${SENTINEL_SERVICE_PORT}"
26+
if [ -n "$SENTINEL_PASSWORD" ]; then
27+
sentinel_cmd="$sentinel_cmd -a $SENTINEL_PASSWORD"
28+
fi
29+
acl_list=$($sentinel_cmd ACL LIST)
30+
if [ $? -eq 0 ]; then
31+
break
32+
fi
33+
done
34+
if [ -z "$acl_list" ]; then
35+
return
36+
fi
37+
echo -n > /tmp/sentinel.acl
38+
while IFS= read -r user_rule; do
39+
echo "$user_rule" >> /tmp/sentinel.acl
40+
done <<< "$acl_list"
41+
datasafed push /tmp/sentinel.acl "sentinel.acl"
42+
}
43+
1844
connect_url="redis-cli $REDIS_CLI_TLS_CMD -h ${DP_DB_HOST} -p ${DP_DB_PORT} -a ${DP_DB_PASSWORD}"
1945
if [ -z ${DP_DB_PASSWORD} ]; then
2046
connect_url="redis-cli $REDIS_CLI_TLS_CMD -h ${DP_DB_HOST} -p ${DP_DB_PORT}"
@@ -42,6 +68,7 @@ else
4268
# NOTE: if files changed during taring, the exit code will be 1 when it ends.
4369
# and will archive the aof file together.
4470
tar -cvf - ./ | datasafed push -z zstd-fastest - "${DP_BACKUP_NAME}.tar.zst"
71+
save_sentinel_acl
4572
fi
4673
echo "INFO: save data file successfully"
4774
TOTAL_SIZE=$(datasafed stat / | grep TotalSize | awk '{print $2}')

addons/redis/dataprotection/restore-keys.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
#!/bin/bash
22

3+
function restore_sentinel_acl() {
4+
export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
5+
export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"
6+
7+
if [ -z "$SENTINEL_POD_FQDN_LIST" ]; then
8+
echo "INFO: no sentinel found, skip restore sentinel ACL file"
9+
return
10+
fi
11+
12+
sentinel_acl_file="sentinel.acl"
13+
if [ "$(datasafed list $sentinel_acl_file)" == "${sentinel_acl_file}" ]; then
14+
datasafed pull "${sentinel_acl_file}" /tmp/sentinel.acl
15+
fi
16+
17+
for sentinel_fqdn in $(echo "$SENTINEL_POD_FQDN_LIST" | tr "," "\n"); do
18+
echo "INFO: restore sentinel ${sentinel_fqdn} ACL file"
19+
sentinel_cmd="redis-cli $REDIS_CLI_TLS_CMD -h $sentinel_fqdn -p ${SENTINEL_SERVICE_PORT}"
20+
if [ -n "$SENTINEL_PASSWORD" ]; then
21+
sentinel_cmd="$sentinel_cmd -a $SENTINEL_PASSWORD"
22+
fi
23+
if [ "$($sentinel_cmd ping)" != "PONG" ]; then
24+
echo "Waring: failed to connect sentinel ${sentinel_fqdn}, skip"
25+
continue
26+
fi
27+
while IFS= read -r user_rule; do
28+
[[ -z "$user_rule" ]] && continue
29+
30+
if [[ "$user_rule" =~ ^user[[:space:]]+([^[:space:]]+) ]]; then
31+
username="${BASH_REMATCH[1]}"
32+
else
33+
# skip invalid user rule
34+
continue
35+
fi
36+
37+
if [[ "$username" == "default" ]]; then
38+
continue
39+
fi
40+
rule_part="${user_rule#user $username }"
41+
echo "$username" $rule_part
42+
$sentinel_cmd ACL SETUSER "$username" $rule_part >&2
43+
done < /tmp/sentinel.acl
44+
break
45+
done
46+
}
47+
# restore sentinel acl
48+
restore_sentinel_acl
349
if [ -z "$DP_RESTORE_KEY_PATTERNS" ]; then
450
echo "DP_RESTORE_KEY_PATTERNS is not set. Exiting..."
551
exit 0

0 commit comments

Comments
 (0)