Skip to content

Commit 8687ff5

Browse files
committed
feat(backup): refactor local database host checks for improved readability and maintainability
1 parent aed0174 commit 8687ff5

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

lib/pasarguard-backup.sh

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ filter_backup_cron_entries() {
2424
grep -F -v "# pasarguard-backup-service" "$source_file" >"$target_file" || true
2525
}
2626

27+
is_local_db_host() {
28+
local host="${1:-}"
29+
30+
host="${host#[}"
31+
host="${host%]}"
32+
host="${host%.}"
33+
host="${host,,}"
34+
35+
[[ -z "$host" ]] && return 1
36+
[[ "$host" == "localhost" ]] && return 0
37+
[[ "$host" == "localhost.localdomain" ]] && return 0
38+
[[ "$host" == "127" ]] && return 0
39+
[[ "$host" =~ ^127\.([0-9]{1,3}\.){2}[0-9]{1,3}$ ]] && return 0
40+
[[ "$host" == "::1" ]] && return 0
41+
[[ "$host" == "0:0:0:0:0:0:0:1" ]] && return 0
42+
43+
return 1
44+
}
45+
2746
send_backup_to_telegram() {
2847
if [ -f "$ENV_FILE" ]; then
2948
while IFS='=' read -r key value; do
@@ -1001,11 +1020,17 @@ backup_command() {
10011020
fi
10021021

10031022
# Extract host, port, and database
1004-
if [[ "$url_part" =~ ^([^:/]+)(:([0-9]+))?/(.+)$ ]]; then
1023+
if [[ "$url_part" =~ ^\[([^]]+)\](:([0-9]+))?/(.+)$ ]]; then
1024+
db_host="${BASH_REMATCH[1]}"
1025+
db_port="${BASH_REMATCH[3]:-}"
1026+
db_name="${BASH_REMATCH[4]}"
1027+
elif [[ "$url_part" =~ ^([^:/]+)(:([0-9]+))?/(.+)$ ]]; then
10051028
db_host="${BASH_REMATCH[1]}"
10061029
db_port="${BASH_REMATCH[3]:-}"
10071030
db_name="${BASH_REMATCH[4]}"
1031+
fi
10081032

1033+
if [ -n "$db_host" ]; then
10091034
# Remove query parameters from database name if any
10101035
db_name="${db_name%%\?*}"
10111036
db_name="${db_name%%#*}"
@@ -1026,7 +1051,7 @@ backup_command() {
10261051
fi
10271052

10281053
# For local databases, try to find container name from docker-compose
1029-
if [[ "$db_host" == "127.0.0.1" || "$db_host" == "localhost" || "$db_host" == "::1" ]]; then
1054+
if is_local_db_host "$db_host"; then
10301055
container_name=$(find_container "$db_type")
10311056
echo "Container name/ID for $db_type: $container_name" >>"$log_file"
10321057
fi
@@ -1040,7 +1065,7 @@ backup_command() {
10401065
colorized_echo blue "Backing up database..."
10411066
case $db_type in
10421067
mariadb)
1043-
if [[ "$db_host" == "127.0.0.1" || "$db_host" == "localhost" || "$db_host" == "::1" ]]; then
1068+
if is_local_db_host "$db_host"; then
10441069
if [ -z "$container_name" ]; then
10451070
colorized_echo red "Error: MariaDB container not found. Is the container running?"
10461071
echo "MariaDB container not found. Container name: ${container_name:-empty}" >>"$log_file"
@@ -1108,7 +1133,7 @@ backup_command() {
11081133
fi
11091134
;;
11101135
mysql)
1111-
if [[ "$db_host" == "127.0.0.1" || "$db_host" == "localhost" || "$db_host" == "::1" ]]; then
1136+
if is_local_db_host "$db_host"; then
11121137
if [ -z "$container_name" ]; then
11131138
colorized_echo red "Error: MySQL container not found. Is the container running?"
11141139
echo "MySQL container not found. Container name: ${container_name:-empty}" >>"$log_file"
@@ -1217,7 +1242,7 @@ backup_command() {
12171242
fi
12181243
;;
12191244
postgresql)
1220-
if [[ "$db_host" == "127.0.0.1" || "$db_host" == "localhost" || "$db_host" == "::1" ]]; then
1245+
if is_local_db_host "$db_host"; then
12211246
if [ -z "$container_name" ]; then
12221247
colorized_echo red "Error: PostgreSQL container not found. Is the container running?"
12231248
echo "PostgreSQL container not found. Container name: ${container_name:-empty}" >>"$log_file"
@@ -1257,7 +1282,7 @@ backup_command() {
12571282
fi
12581283
;;
12591284
timescaledb)
1260-
if [[ "$db_host" == "127.0.0.1" || "$db_host" == "localhost" || "$db_host" == "::1" ]]; then
1285+
if is_local_db_host "$db_host"; then
12611286
if [ -z "$container_name" ]; then
12621287
colorized_echo red "Error: TimescaleDB container not found. Is the container running?"
12631288
echo "Container name detection failed. Checked for: timescaledb, postgresql" >>"$log_file"

0 commit comments

Comments
 (0)