Skip to content

Commit c5c026d

Browse files
authored
Merge pull request #232 from benvia/feature-cron
Adds a cronjob-like mechanism
2 parents 50b61ad + e42f8e9 commit c5c026d

File tree

1 file changed

+30
-3
lines changed
  • install/etc/services.available/10-db-backup

1 file changed

+30
-3
lines changed

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

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

19+
print_debug "******** Begin DB_DUMP_BEGIN ********"
20+
print_debug "Current time $current_time"
21+
1922
if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then
23+
print_debug "DB_DUMP_BEGIN is a jump of minute starting with +."
2024
waittime=$(( ${BASH_REMATCH[1]} * 60 ))
2125
target_time=$(($current_time + $waittime))
22-
else
26+
elif [[ $DB_DUMP_BEGIN =~ ^[0-9]+$ ]]; then
27+
print_debug "DB_DUMP_BEGIN is an integer."
28+
2329
target_time=$(date --date="${today}${DB_DUMP_BEGIN}" +"%s")
2430
if [[ "$target_time" < "$current_time" ]]; then
2531
target_time=$(($target_time + 24*60*60))
2632
fi
2733
waittime=$(($target_time - $current_time))
34+
elif [[ $DB_DUMP_BEGIN =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})[[:space:]]([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]];
35+
then
36+
# Calculate DB_DUMP_BEGIN time in seconds
37+
dump_time=$(date -d "${DB_DUMP_BEGIN}" +%s)
38+
print_debug "Dump time = $dump_time"
39+
40+
# Calculate the difference in seconds
41+
waittime=$((dump_time - current_time))
42+
print_debug "Difference in seconds: $waittime"
43+
44+
if (( $waittime < 0 )); then
45+
waittime=$(( ($waittime + ($DB_DUMP_FREQ - 1)) / ($DB_DUMP_FREQ * 60) ))
46+
waittime=$(( $waittime * -1 ))
47+
print_debug "Difference in seconds (rounded) waittime is in the past : $waittime"
48+
fi
49+
50+
target_time=$(($current_time + $waittime))
51+
print_debug "Target time = $target_time"
52+
else
53+
print_info "DB_DUMP_BEGIN is not starting with + or is not an integer or is not in the correct format (YYYY-mm-dd hh:mm:ss)."
2854
fi
29-
print_debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}"
30-
print_info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")"
55+
56+
print_debug "******** End DB_DUMP_BEGIN ********"
57+
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")"
3158
sleep $waittime
3259
fi
3360

0 commit comments

Comments
 (0)