mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-21 13:23:12 +01:00
90 lines
2.9 KiB
Plaintext
Executable File
90 lines
2.9 KiB
Plaintext
Executable File
#!/command/with-contenv bash
|
|
|
|
source /assets/functions/00-container
|
|
source /assets/functions/10-db-backup
|
|
source /assets/defaults/10-db-backup
|
|
PROCESS_NAME="db-backup"
|
|
|
|
bootstrap_variables
|
|
|
|
write_log info "Starting instance - hostname $(hostname) container_name ${CONTAINER_NAME}"
|
|
if [ "${MODE,,}" = "manual" ] || [ "${1,,}" = "manual" ] || [ "${1,,}" = "now" ]; then
|
|
DB_DUMP_BEGIN=+0
|
|
manual=TRUE
|
|
write_log debug "Detected Manual Mode"
|
|
else
|
|
sleep 5
|
|
current_time=$(date +"%s")
|
|
today=$(date +"%Y%m%d")
|
|
|
|
if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then
|
|
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))
|
|
fi
|
|
waittime=$(($target_time - $current_time))
|
|
fi
|
|
write_log debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}"
|
|
write_log info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")"
|
|
sleep $waittime
|
|
fi
|
|
|
|
while true; do
|
|
mkdir -p "${TEMP_LOCATION}"
|
|
backup_start_time=$(date +"%s")
|
|
write_log debug "Backup routines started time: $(date +'%Y-%m-%d %T %Z')"
|
|
case "${dbtype,,}" in
|
|
"couch" )
|
|
check_availability
|
|
backup_couch
|
|
;;
|
|
"influx" )
|
|
check_availability
|
|
backup_influx
|
|
;;
|
|
"mssql" )
|
|
check_availability
|
|
backup_mssql
|
|
;;
|
|
"mysql" )
|
|
check_availability
|
|
backup_mysql
|
|
;;
|
|
"mongo" )
|
|
check_availability
|
|
backup_mongo
|
|
;;
|
|
"pgsql" )
|
|
check_availability
|
|
backup_pgsql
|
|
;;
|
|
"redis" )
|
|
check_availability
|
|
backup_redis
|
|
;;
|
|
"sqlite3" )
|
|
check_availability
|
|
backup_sqlite3
|
|
;;
|
|
esac
|
|
|
|
backup_finish_time=$(date +"%s")
|
|
backup_total_time=$(echo $((backup_finish_time-backup_start_time)))
|
|
if [ -z "$master_exit_code" ] ; then master_exit_code="0" ; fi
|
|
write_log info "Backup routines finish time: $(date -d @${backup_finish_time} +"%Y-%m-%d %T %Z") with overall exit code ${master_exit_code}"
|
|
write_log notice "Backup routines time taken: $(echo ${backup_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')"
|
|
|
|
cleanup_old_data
|
|
|
|
if var_true "${manual}" ; then
|
|
write_log debug "Exiting due to manual mode"
|
|
exit ${master_exit_code};
|
|
else
|
|
write_log notice "Sleeping for another $(($DB_DUMP_FREQ*60-backup_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($DB_DUMP_FREQ*60-backup_total_time))))" +"%Y-%m-%d %T %Z") "
|
|
sleep $(($DB_DUMP_FREQ*60-backup_total_time))
|
|
fi
|
|
done
|