Skip to content

Commit 8203717

Browse files
committed
chore: support backup and restore sentinel users for full method (#2444)
(cherry picked from commit 79c445c)
1 parent 0222f00 commit 8203717

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
@@ -14,6 +14,32 @@ trap handle_exit EXIT
1414

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