mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-21 21:33:28 +01:00
141 lines
4.8 KiB
Plaintext
Executable File
141 lines
4.8 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_compression
|
|
bootstrap_variables
|
|
|
|
case "${1,,}" in
|
|
"now" | "manual" )
|
|
DB_DUMP_BEGIN=+0
|
|
manual=TRUE
|
|
;;
|
|
* )
|
|
sleep 5
|
|
;;
|
|
esac
|
|
|
|
### Container Startup
|
|
print_debug "Backup routines Initialized on $(date)"
|
|
|
|
### Wait for Next time to start backup
|
|
case "${1,,}" in
|
|
"now" | "manual" )
|
|
:
|
|
;;
|
|
* )
|
|
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
|
|
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")"
|
|
sleep $waittime
|
|
;;
|
|
esac
|
|
|
|
### Commence Backup
|
|
while true; do
|
|
mkdir -p "${TEMP_LOCATION}"
|
|
backup_start_time=$(date +"%s")
|
|
now=$(date +"%Y%m%d-%H%M%S")
|
|
now_time=$(date +"%H:%M:%S")
|
|
now_date=$(date +"%Y-%m-%d")
|
|
target=${dbtype}_${dbname}_${dbhost}_${now}.sql
|
|
|
|
### Take a Dump
|
|
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)))
|
|
|
|
print_info "Backup finish time: $(date -d @${backup_finish_time} +"%Y-%m-%d %T %Z")"
|
|
print_notice "Backup time elapsed: $(echo ${backup_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')"
|
|
|
|
### Zabbix / Monitoring stats
|
|
if var_true "${CONTAINER_ENABLE_MONITORING}" ; then
|
|
print_notice "Sending Backup Statistics to Zabbix"
|
|
silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.size -o "$(stat -c%s "${DB_DUMP_TARGET}"/"${target}")"
|
|
silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.datetime -o "$(date -r "${DB_DUMP_TARGET}"/"${target}" +'%s')"
|
|
silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.status -o "${exit_code}"
|
|
silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.backup_duration -o "$(echo $((backup_finish_time-backup_start_time)))"
|
|
fi
|
|
|
|
### Automatic Cleanup
|
|
if [ -n "${DB_CLEANUP_TIME}" ]; then
|
|
print_info "Cleaning up old backups"
|
|
mkdir -p "${DB_DUMP_TARGET}"
|
|
find "${DB_DUMP_TARGET}"/ -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm {} \;
|
|
fi
|
|
|
|
### Post Script Support
|
|
if [ -n "${POST_SCRIPT}" ] ; then
|
|
print_notice "Found POST_SCRIPT environment variable. Executing '${POST_SCRIPT}"
|
|
eval "${POST_SCRIPT}" "${exit_code}" "${dbtype}" "${dbhost}" "${dbname}" "${backup_start_time}" "${backup_finish_time}" "${backup_total_time}" "${target}" "${FILESIZE}" "${checksum_value}"
|
|
fi
|
|
|
|
### Post Backup Custom Script Support
|
|
if [ -d "/assets/custom-scripts/" ] ; then
|
|
print_notice "Found Post Backup Custom Script to execute"
|
|
for f in $(find /assets/custom-scripts/ -name \*.sh -type f); do
|
|
print_notice "Running Script: '${f}'"
|
|
## script EXIT_CODE DB_TYPE DB_HOST DB_NAME STARTEPOCH FINISHEPOCH DURATIONEPOCH BACKUP_FILENAME FILESIZE CHECKSUMVALUE
|
|
${f} "${exit_code}" "${dbtype}" "${dbhost}" "${dbname}" "${backup_start_timme}" "${backup_finish_time}" "${backup_total_time}" "${target}" "${FILESIZE}" "${checksum_value}"
|
|
done
|
|
fi
|
|
|
|
if var_true "${manual}" ; then
|
|
print_debug "Exitting due to manual mode"
|
|
exit ${exit_code};
|
|
else
|
|
### Go back to sleep until next backup time
|
|
sleep $(($DB_DUMP_FREQ*60-backup_total_time))
|
|
print_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") "
|
|
fi
|
|
done
|