mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-21 13:23:12 +01:00
Merge pull request #232 from benvia/feature-cron
Adds a cronjob-like mechanism
This commit is contained in:
@@ -16,18 +16,45 @@ else
|
|||||||
current_time=$(date +"%s")
|
current_time=$(date +"%s")
|
||||||
today=$(date +"%Y%m%d")
|
today=$(date +"%Y%m%d")
|
||||||
|
|
||||||
|
print_debug "******** Begin DB_DUMP_BEGIN ********"
|
||||||
|
print_debug "Current time $current_time"
|
||||||
|
|
||||||
if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then
|
if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then
|
||||||
|
print_debug "DB_DUMP_BEGIN is a jump of minute starting with +."
|
||||||
waittime=$(( ${BASH_REMATCH[1]} * 60 ))
|
waittime=$(( ${BASH_REMATCH[1]} * 60 ))
|
||||||
target_time=$(($current_time + $waittime))
|
target_time=$(($current_time + $waittime))
|
||||||
else
|
elif [[ $DB_DUMP_BEGIN =~ ^[0-9]+$ ]]; then
|
||||||
|
print_debug "DB_DUMP_BEGIN is an integer."
|
||||||
|
|
||||||
target_time=$(date --date="${today}${DB_DUMP_BEGIN}" +"%s")
|
target_time=$(date --date="${today}${DB_DUMP_BEGIN}" +"%s")
|
||||||
if [[ "$target_time" < "$current_time" ]]; then
|
if [[ "$target_time" < "$current_time" ]]; then
|
||||||
target_time=$(($target_time + 24*60*60))
|
target_time=$(($target_time + 24*60*60))
|
||||||
fi
|
fi
|
||||||
waittime=$(($target_time - $current_time))
|
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
|
||||||
|
# Calculate DB_DUMP_BEGIN time in seconds
|
||||||
|
dump_time=$(date -d "${DB_DUMP_BEGIN}" +%s)
|
||||||
|
print_debug "Dump time = $dump_time"
|
||||||
|
|
||||||
|
# Calculate the difference in seconds
|
||||||
|
waittime=$((dump_time - current_time))
|
||||||
|
print_debug "Difference in seconds: $waittime"
|
||||||
|
|
||||||
|
if (( $waittime < 0 )); then
|
||||||
|
waittime=$(( ($waittime + ($DB_DUMP_FREQ - 1)) / ($DB_DUMP_FREQ * 60) ))
|
||||||
|
waittime=$(( $waittime * -1 ))
|
||||||
|
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 starting with + or is not an integer or is not in the correct format (YYYY-mm-dd hh:mm:ss)."
|
||||||
fi
|
fi
|
||||||
print_debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}"
|
|
||||||
print_info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")"
|
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")"
|
||||||
sleep $waittime
|
sleep $waittime
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user