From 3c6beeaae9cde7f338826a1d25700ef5f48a9b40 Mon Sep 17 00:00:00 2001 From: Benoit Vianin Date: Wed, 14 Jun 2023 04:36:21 +0000 Subject: [PATCH 1/5] Adds a cronjob-like mechanism --- .../etc/services.available/10-db-backup/run | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/install/etc/services.available/10-db-backup/run b/install/etc/services.available/10-db-backup/run index 546383e..a3ebe44 100755 --- a/install/etc/services.available/10-db-backup/run +++ b/install/etc/services.available/10-db-backup/run @@ -16,17 +16,37 @@ else current_time=$(date +"%s") today=$(date +"%Y%m%d") - if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then + # Check if DB_DUMP_BEGIN is an integer + if [[ $DB_DUMP_BEGIN =~ ^[0-9]+$ ]]; then + print_info "DB_DUMP_BEGIN is an integer." waittime=$(( ${BASH_REMATCH[1]} * 60 )) target_time=$(($current_time + $waittime)) - else - target_time=$(date --date="${today}${DB_DUMP_BEGIN}" +"%s") - if [[ "$target_time" < "$current_time" ]]; then - target_time=$(($target_time + 24*60*60)) + elif [[ $DB_DUMP_BEGIN =~ ^([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then + print_info "DB_DUMP_BEGIN is time." + # Extract hours, minutes, and seconds from DB_DUMP_BEGIN + db_hour=${BASH_REMATCH[1]} + db_minute=${BASH_REMATCH[2]} + db_second=${BASH_REMATCH[3]} + + # Calculate DB_DUMP_BEGIN time in seconds + db_time=$(date -d "${db_hour}:${db_minute}:${db_second}" +%s) + + # Calculate the difference in seconds + waittime=$((db_time - current_time)) + print_info "Difference in seconds: $waittime" + + if (( $waittime < 0 )); then + waittime=$(( ($waittime + ($DB_DUMP_FREQ - 1)) / ($DB_DUMP_FREQ * 60) )) + waittime=$(( $waittime * -1 )) + print_info "Difference in seconds (rounded): $waittime" fi - waittime=$(($target_time - $current_time)) + + target_time=$(($current_time + $waittime)) + else + print_info "DB_DUMP_BEGIN is not an integer or in the correct format (hh:mm:ss)." fi - print_debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}" + + 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")" print_info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")" sleep $waittime fi From 250cae98ef16de23ac2741f72e1b05773664ef55 Mon Sep 17 00:00:00 2001 From: Benoit Vianin Date: Sat, 4 Nov 2023 10:34:40 +0000 Subject: [PATCH 2/5] Restore the +XX minutes fucntion and move print to debug mode --- .../etc/services.available/10-db-backup/run | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/install/etc/services.available/10-db-backup/run b/install/etc/services.available/10-db-backup/run index a3ebe44..a7fafbd 100755 --- a/install/etc/services.available/10-db-backup/run +++ b/install/etc/services.available/10-db-backup/run @@ -16,13 +16,19 @@ else current_time=$(date +"%s") today=$(date +"%Y%m%d") - # Check if DB_DUMP_BEGIN is an integer - if [[ $DB_DUMP_BEGIN =~ ^[0-9]+$ ]]; then - print_info "DB_DUMP_BEGIN is an integer." + print_debug "******** Begin DB_DUMP_BEGIN ********" + print_debug "Current time $current_time" + + if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then + print_debug "DB_DUMP_BEGIN is a jump of minute starting with +." + waittime=$(( ${BASH_REMATCH[1]} * 60 )) + target_time=$(($current_time + $waittime)) + elif [[ $DB_DUMP_BEGIN =~ ^[0-9]+$ ]]; then + print_debug "DB_DUMP_BEGIN is an integer." waittime=$(( ${BASH_REMATCH[1]} * 60 )) target_time=$(($current_time + $waittime)) elif [[ $DB_DUMP_BEGIN =~ ^([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then - print_info "DB_DUMP_BEGIN is time." + print_debug "DB_DUMP_BEGIN is a time." # Extract hours, minutes, and seconds from DB_DUMP_BEGIN db_hour=${BASH_REMATCH[1]} db_minute=${BASH_REMATCH[2]} @@ -30,24 +36,26 @@ else # Calculate DB_DUMP_BEGIN time in seconds db_time=$(date -d "${db_hour}:${db_minute}:${db_second}" +%s) + print_debug "DB time = $db_time" # Calculate the difference in seconds waittime=$((db_time - current_time)) - print_info "Difference in seconds: $waittime" + print_debug "Difference in seconds: $waittime" if (( $waittime < 0 )); then waittime=$(( ($waittime + ($DB_DUMP_FREQ - 1)) / ($DB_DUMP_FREQ * 60) )) waittime=$(( $waittime * -1 )) - print_info "Difference in seconds (rounded): $waittime" + print_debug "Difference in seconds (rounded) waittime is in the past : $waittime" fi target_time=$(($current_time + $waittime)) + print_debug "Target time = $target_time" else - print_info "DB_DUMP_BEGIN is not an integer or in the correct format (hh:mm:ss)." + print_info "DB_DUMP_BEGIN is not starting with + or is not an integer or is not in the correct format (hh:mm:ss)." fi + print_debug "******** End DB_DUMP_BEGIN ********" 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")" - print_info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")" sleep $waittime fi From 06cfba4952920a35597f2960aee77ab3f6d8083d Mon Sep 17 00:00:00 2001 From: Benoit Vianin Date: Sat, 4 Nov 2023 11:43:45 +0000 Subject: [PATCH 3/5] Modify DB_DUMP_BEGIN to support a full date as cron --- install/etc/services.available/10-db-backup/run | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/install/etc/services.available/10-db-backup/run b/install/etc/services.available/10-db-backup/run index a7fafbd..de44578 100755 --- a/install/etc/services.available/10-db-backup/run +++ b/install/etc/services.available/10-db-backup/run @@ -27,15 +27,18 @@ else print_debug "DB_DUMP_BEGIN is an integer." waittime=$(( ${BASH_REMATCH[1]} * 60 )) target_time=$(($current_time + $waittime)) - elif [[ $DB_DUMP_BEGIN =~ ^([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then - print_debug "DB_DUMP_BEGIN is a time." - # Extract hours, minutes, and seconds from DB_DUMP_BEGIN - db_hour=${BASH_REMATCH[1]} - db_minute=${BASH_REMATCH[2]} - db_second=${BASH_REMATCH[3]} + elif [[ $DB_DUMP_BEGIN =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})[[:space:]]([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; + then + # Extract year, month, day, hours, minutes, and seconds from DB_DUMP_BEGIN + db_year=${BASH_REMATCH[1]} + db_month=${BASH_REMATCH[2]} + db_day=${BASH_REMATCH[3]} + db_hour=${BASH_REMATCH[4]} + db_minute=${BASH_REMATCH[5]} + db_second=${BASH_REMATCH[6]} # Calculate DB_DUMP_BEGIN time in seconds - db_time=$(date -d "${db_hour}:${db_minute}:${db_second}" +%s) + db_time=$(date -d "${db_year}-${db_month}-${db_day} ${db_hour}:${db_minute}:${db_second}" +%s) print_debug "DB time = $db_time" # Calculate the difference in seconds From 6a28ac2d92479ff555111cfa2bb04f721d9017f2 Mon Sep 17 00:00:00 2001 From: Benoit Vianin Date: Sat, 4 Nov 2023 18:53:42 +0000 Subject: [PATCH 4/5] Fix code for absolute time --- install/etc/services.available/10-db-backup/run | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/install/etc/services.available/10-db-backup/run b/install/etc/services.available/10-db-backup/run index de44578..368b5fc 100755 --- a/install/etc/services.available/10-db-backup/run +++ b/install/etc/services.available/10-db-backup/run @@ -25,8 +25,12 @@ else target_time=$(($current_time + $waittime)) elif [[ $DB_DUMP_BEGIN =~ ^[0-9]+$ ]]; then print_debug "DB_DUMP_BEGIN is an integer." - waittime=$(( ${BASH_REMATCH[1]} * 60 )) - target_time=$(($current_time + $waittime)) + + target_time=$(date --date="${today}${DB_DUMP_BEGIN}" +"%s") + if [[ "$target_time" < "$current_time" ]]; then + target_time=$(($target_time + 24*60*60)) + fi + waittime=$(($target_time - $current_time)) elif [[ $DB_DUMP_BEGIN =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})[[:space:]]([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then # Extract year, month, day, hours, minutes, and seconds from DB_DUMP_BEGIN From e42f8e9a8cf09e5dfd96946f608073621ac84479 Mon Sep 17 00:00:00 2001 From: Benoit Vianin Date: Sun, 5 Nov 2023 06:48:47 +0000 Subject: [PATCH 5/5] Code refactoring --- install/etc/services.available/10-db-backup/run | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/install/etc/services.available/10-db-backup/run b/install/etc/services.available/10-db-backup/run index 368b5fc..12f2364 100755 --- a/install/etc/services.available/10-db-backup/run +++ b/install/etc/services.available/10-db-backup/run @@ -33,20 +33,12 @@ else waittime=$(($target_time - $current_time)) elif [[ $DB_DUMP_BEGIN =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})[[:space:]]([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then - # Extract year, month, day, hours, minutes, and seconds from DB_DUMP_BEGIN - db_year=${BASH_REMATCH[1]} - db_month=${BASH_REMATCH[2]} - db_day=${BASH_REMATCH[3]} - db_hour=${BASH_REMATCH[4]} - db_minute=${BASH_REMATCH[5]} - db_second=${BASH_REMATCH[6]} - # Calculate DB_DUMP_BEGIN time in seconds - db_time=$(date -d "${db_year}-${db_month}-${db_day} ${db_hour}:${db_minute}:${db_second}" +%s) - print_debug "DB time = $db_time" + dump_time=$(date -d "${DB_DUMP_BEGIN}" +%s) + print_debug "Dump time = $dump_time" # Calculate the difference in seconds - waittime=$((db_time - current_time)) + waittime=$((dump_time - current_time)) print_debug "Difference in seconds: $waittime" if (( $waittime < 0 )); then @@ -58,7 +50,7 @@ else target_time=$(($current_time + $waittime)) print_debug "Target time = $target_time" else - print_info "DB_DUMP_BEGIN is not starting with + or is not an integer or is not in the correct format (hh:mm:ss)." + 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)." fi print_debug "******** End DB_DUMP_BEGIN ********"