Skip to content

Commit 3c6beea

Browse files
committed
Adds a cronjob-like mechanism
1 parent f83f97b commit 3c6beea

File tree

1 file changed

+27
-7
lines changed
  • install/etc/services.available/10-db-backup

1 file changed

+27
-7
lines changed

install/etc/services.available/10-db-backup/run

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,37 @@ else
1616
current_time=$(date +"%s")
1717
today=$(date +"%Y%m%d")
1818

19-
if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then
19+
# Check if DB_DUMP_BEGIN is an integer
20+
if [[ $DB_DUMP_BEGIN =~ ^[0-9]+$ ]]; then
21+
print_info "DB_DUMP_BEGIN is an integer."
2022
waittime=$(( ${BASH_REMATCH[1]} * 60 ))
2123
target_time=$(($current_time + $waittime))
22-
else
23-
target_time=$(date --date="${today}${DB_DUMP_BEGIN}" +"%s")
24-
if [[ "$target_time" < "$current_time" ]]; then
25-
target_time=$(($target_time + 24*60*60))
24+
elif [[ $DB_DUMP_BEGIN =~ ^([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then
25+
print_info "DB_DUMP_BEGIN is time."
26+
# Extract hours, minutes, and seconds from DB_DUMP_BEGIN
27+
db_hour=${BASH_REMATCH[1]}
28+
db_minute=${BASH_REMATCH[2]}
29+
db_second=${BASH_REMATCH[3]}
30+
31+
# Calculate DB_DUMP_BEGIN time in seconds
32+
db_time=$(date -d "${db_hour}:${db_minute}:${db_second}" +%s)
33+
34+
# Calculate the difference in seconds
35+
waittime=$((db_time - current_time))
36+
print_info "Difference in seconds: $waittime"
37+
38+
if (( $waittime < 0 )); then
39+
waittime=$(( ($waittime + ($DB_DUMP_FREQ - 1)) / ($DB_DUMP_FREQ * 60) ))
40+
waittime=$(( $waittime * -1 ))
41+
print_info "Difference in seconds (rounded): $waittime"
2642
fi
27-
waittime=$(($target_time - $current_time))
43+
44+
target_time=$(($current_time + $waittime))
45+
else
46+
print_info "DB_DUMP_BEGIN is not an integer or in the correct format (hh:mm:ss)."
2847
fi
29-
print_debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}"
48+
49+
print_info "Wait Time: ${waittime} Target time: $(date -d @${target_time} +"%Y-%m-%d %T %Z") Current Time: $(date -d @${current_time} +"%Y-%m-%d %T %Z")"
3050
print_info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")"
3151
sleep $waittime
3252
fi

0 commit comments

Comments
 (0)