mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-24 06:28:16 +01:00
Release 3.0.0 - See CHANGELOG.md
This commit is contained in:
@@ -1,469 +1,62 @@
|
||||
#!/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"
|
||||
|
||||
date >/dev/null
|
||||
|
||||
if [ "$1" != "NOW" ]; then
|
||||
sleep 10
|
||||
fi
|
||||
|
||||
### Sanity Test
|
||||
sanity_var DB_TYPE "Database Type"
|
||||
sanity_var DB_HOST "Database Host"
|
||||
|
||||
### Set the Database Type
|
||||
dbtype=${DB_TYPE}
|
||||
|
||||
case "$dbtype" in
|
||||
"couch" | "couchdb" | "COUCH" | "COUCHDB" )
|
||||
dbtype=couch
|
||||
dbport=${DB_PORT:-5984}
|
||||
file_env 'DB_USER'
|
||||
file_env 'DB_PASS'
|
||||
case "${1,,}" in
|
||||
"now" | "manual" )
|
||||
DB_DUMP_BEGIN=+0
|
||||
manual=TRUE
|
||||
;;
|
||||
"influx" | "influxdb" | "INFLUX" | "INFLUXDB" )
|
||||
dbtype=influx
|
||||
dbport=${DB_PORT:-8088}
|
||||
file_env 'DB_USER'
|
||||
file_env 'DB_PASS'
|
||||
;;
|
||||
"mongo" | "mongodb" | "MONGO" | "MONGODB" )
|
||||
dbtype=mongo
|
||||
dbport=${DB_PORT:-27017}
|
||||
[[ ( -n "${DB_USER}" ) || ( -n "${DB_USER_FILE}" ) ]] && file_env 'DB_USER'
|
||||
[[ ( -n "${DB_PASS}" ) || ( -n "${DB_PASS_FILE}" ) ]] && file_env 'DB_PASS'
|
||||
;;
|
||||
"mysql" | "MYSQL" | "mariadb" | "MARIADB")
|
||||
dbtype=mysql
|
||||
dbport=${DB_PORT:-3306}
|
||||
[[ ( -n "${DB_PASS}" ) || ( -n "${DB_PASS_FILE}" ) ]] && file_env 'DB_PASS'
|
||||
MYSQL_MAX_ALLOWED_PACKET=${MYSQL_MAX_ALLOWED_PACKET:-"512M"}
|
||||
;;
|
||||
"mssql" | "MSSQL" | "microsoftsql" | "MICROSOFTSQL")
|
||||
apkArch="$(apk --print-arch)"; \
|
||||
case "$apkArch" in
|
||||
x86_64) mssql=true ;;
|
||||
*) print_error "MSSQL cannot operate on $apkArch processor!" ; exit 1 ;;
|
||||
esac
|
||||
dbtype=mssql
|
||||
dbport=${DB_PORT:-1433}
|
||||
;;
|
||||
"postgres" | "postgresql" | "pgsql" | "POSTGRES" | "POSTGRESQL" | "PGSQL" )
|
||||
dbtype=pgsql
|
||||
dbport=${DB_PORT:-5432}
|
||||
[[ ( -n "${DB_PASS}" ) || ( -n "${DB_PASS_FILE}" ) ]] && file_env 'DB_PASS'
|
||||
;;
|
||||
"redis" | "REDIS" )
|
||||
dbtype=redis
|
||||
dbport=${DB_PORT:-6379}
|
||||
[[ ( -n "${DB_PASS}" || ( -n "${DB_PASS_FILE}" ) ) ]] && file_env 'DB_PASS'
|
||||
;;
|
||||
"sqlite" | "sqlite3" | "SQLITE" | "SQLITE3" )
|
||||
dbtype=sqlite3
|
||||
;;
|
||||
esac
|
||||
|
||||
### Set Defaults
|
||||
BACKUP_LOCATION=${BACKUP_LOCATION:-"FILESYSTEM"}
|
||||
COMPRESSION=${COMPRESSION:-"GZ"}
|
||||
COMPRESSION_LEVEL=${COMPRESSION_LEVEL:-"3"}
|
||||
DB_DUMP_BEGIN=${DB_DUMP_BEGIN:-+0}
|
||||
DB_DUMP_FREQ=${DB_DUMP_FREQ:-1440}
|
||||
DB_DUMP_TARGET=${DB_DUMP_TARGET:-"/backup"}
|
||||
dbhost=${DB_HOST}
|
||||
dbname=${DB_NAME}
|
||||
dbpass=${DB_PASS}
|
||||
dbuser=${DB_USER}
|
||||
MD5=${MD5:-TRUE}
|
||||
PARALLEL_COMPRESSION=${PARALLEL_COMPRESSION:-"TRUE"}
|
||||
SIZE_VALUE=${SIZE_VALUE:-"bytes"}
|
||||
SPLIT_DB=${SPLIT_DB:-"FALSE"}
|
||||
TEMP_LOCATION=${TEMP_LOCATION:-"/tmp/backups"}
|
||||
|
||||
if [ "$BACKUP_LOCATION" = "S3" ] || [ "$BACKUP_LOCATION" = "s3" ] || [ "$BACKUP_LOCATION" = "MINIO" ] || [ "$BACKUP_LOCATION" = "minio" ] ; then
|
||||
S3_PROTOCOL=${S3_PROTOCOL:-"https"}
|
||||
sanity_var S3_BUCKET "S3 Bucket"
|
||||
sanity_var S3_KEY_ID "S3 Key ID"
|
||||
sanity_var S3_KEY_SECRET "S3 Key Secret"
|
||||
sanity_var S3_PATH "S3 Path"
|
||||
sanity_var S3_REGION "S3 Region"
|
||||
file_env 'S3_KEY_ID'
|
||||
file_env 'S3_KEY_SECRET'
|
||||
fi
|
||||
|
||||
if [ "$1" = "NOW" ]; then
|
||||
DB_DUMP_BEGIN=+0
|
||||
MANUAL=TRUE
|
||||
fi
|
||||
|
||||
### Set Compression Options
|
||||
if var_true "${PARALLEL_COMPRESSION}" ; then
|
||||
bzip="pbzip2 -${COMPRESSION_LEVEL}"
|
||||
gzip="pigz -${COMPRESSION_LEVEL}"
|
||||
xzip="pixz -${COMPRESSION_LEVEL}"
|
||||
zstd="zstd --rm -${COMPRESSION_LEVEL}"
|
||||
else
|
||||
bzip="bzip2 -${COMPRESSION_LEVEL}"
|
||||
gzip="gzip -${COMPRESSION_LEVEL}"
|
||||
xzip="xz -${COMPRESSION_LEVEL} "
|
||||
zstd="zstd --rm -${COMPRESSION_LEVEL}"
|
||||
fi
|
||||
|
||||
### Set the Database Authentication Details
|
||||
case "$dbtype" in
|
||||
"mongo" )
|
||||
[[ ( -n "${DB_USER}" ) ]] && MONGO_USER_STR=" --username ${dbuser}"
|
||||
[[ ( -n "${DB_PASS}" ) ]] && MONGO_PASS_STR=" --password ${dbpass}"
|
||||
[[ ( -n "${DB_NAME}" ) ]] && MONGO_DB_STR=" --db ${dbname}"
|
||||
[[ ( -n "${DB_AUTH}" ) ]] && MONGO_AUTH_STR=" --authenticationDatabase ${DB_AUTH}"
|
||||
;;
|
||||
"mysql" )
|
||||
[[ ( -n "${DB_PASS}" ) ]] && export MYSQL_PWD=${dbpass}
|
||||
;;
|
||||
"postgres" )
|
||||
[[ ( -n "${DB_PASS}" ) ]] && POSTGRES_PASS_STR="PGPASSWORD=${dbpass}"
|
||||
;;
|
||||
"redis" )
|
||||
[[ ( -n "${DB_PASS}" ) ]] && REDIS_PASS_STR=" -a ${dbpass}"
|
||||
;;
|
||||
esac
|
||||
|
||||
### Functions
|
||||
backup_couch() {
|
||||
target=couch_${dbname}_${dbhost}_${now}.txt
|
||||
compression
|
||||
curl -X GET http://${dbhost}:${dbport}/${dbname}/_all_docs?include_docs=true ${dumpoutput} | $dumpoutput > ${TEMP_LOCATION}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
}
|
||||
|
||||
backup_influx() {
|
||||
if [ "${COMPRESSION}" = "NONE" ] || [ "${COMPRESSION}" = "none" ] || [ "${COMPRESSION}" = "FALSE" ] || [ "${COMPRESSION}" = "false" ] ; then
|
||||
:
|
||||
else
|
||||
print_notice "Compressing InfluxDB backup with gzip"
|
||||
influx_compression="-portable"
|
||||
fi
|
||||
for DB in ${DB_NAME}; do
|
||||
target=influx_${DB}_${dbhost}_${now}
|
||||
influxd backup ${influx_compression} -database $DB -host ${dbhost}:${dbport} ${TEMP_LOCATION}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
done
|
||||
}
|
||||
|
||||
backup_mongo() {
|
||||
if [ "${COMPRESSION}" = "NONE" ] || [ "${COMPRESSION}" = "none" ] || [ "${COMPRESSION}" = "FALSE" ] || [ "${COMPRESSION}" = "false" ] ; then
|
||||
target=${dbtype}_${dbname}_${dbhost}_${now}.archive
|
||||
else
|
||||
print_notice "Compressing MongoDB backup with gzip"
|
||||
target=${dbtype}_${dbname}_${dbhost}_${now}.archivegz
|
||||
mongo_compression="--gzip"
|
||||
fi
|
||||
mongodump --archive=${TEMP_LOCATION}/${target} ${mongo_compression} --host ${dbhost} --port ${dbport} ${MONGO_USER_STR}${MONGO_PASS_STR}${MONGO_AUTH_STR}${MONGO_DB_STR} ${EXTRA_OPTS}
|
||||
exit_code=$?
|
||||
cd ${TEMP_LOCATION}
|
||||
generate_md5
|
||||
move_backup
|
||||
}
|
||||
|
||||
backup_mssql() {
|
||||
target=mssql_${dbname}_${dbhost}_${now}.bak
|
||||
/opt/mssql-tools/bin/sqlcmd -E -C -S ${dbhost}\,${dbport} -U ${dbuser} -P ${dbpass} –Q "BACKUP DATABASE \[${dbname}\] TO DISK = N'${TEMP_LOCATION}/${target}' WITH NOFORMAT, NOINIT, NAME = '${dbname}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
|
||||
}
|
||||
|
||||
backup_mysql() {
|
||||
if var_true "${SPLIT_DB}" ; then
|
||||
DATABASES=$(mysql -h ${dbhost} -P $dbport -u$dbuser --batch -e "SHOW DATABASES;" | grep -v Database | grep -v schema)
|
||||
|
||||
for db in $DATABASES; do
|
||||
if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
|
||||
print_notice "Dumping MariaDB database: $db"
|
||||
target=mysql_${db}_${dbhost}_${now}.sql
|
||||
compression
|
||||
mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h $dbhost -P $dbport -u$dbuser ${EXTRA_OPTS} --databases $db | $dumpoutput > ${TEMP_LOCATION}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
fi
|
||||
done
|
||||
else
|
||||
compression
|
||||
mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -A -h $dbhost -P $dbport -u$dbuser ${EXTRA_OPTS} | $dumpoutput > ${TEMP_LOCATION}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
fi
|
||||
}
|
||||
|
||||
backup_pgsql() {
|
||||
if var_true "${SPLIT_DB}" ; then
|
||||
export PGPASSWORD=${dbpass}
|
||||
authdb=${DB_USER}
|
||||
[ -n "${DB_NAME}" ] && authdb=${DB_NAME}
|
||||
DATABASES=$(psql -h $dbhost -U $dbuser -p ${dbport} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' )
|
||||
for db in $DATABASES; do
|
||||
print_info "Dumping database: $db"
|
||||
target=pgsql_${db}_${dbhost}_${now}.sql
|
||||
compression
|
||||
pg_dump -h ${dbhost} -p ${dbport} -U ${dbuser} $db ${EXTRA_OPTS} | $dumpoutput > ${TEMP_LOCATION}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
done
|
||||
else
|
||||
export PGPASSWORD=${dbpass}
|
||||
compression
|
||||
pg_dump -h ${dbhost} -U ${dbuser} -p ${dbport} ${dbname} ${EXTRA_OPTS} | $dumpoutput > ${TEMP_LOCATION}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
fi
|
||||
}
|
||||
|
||||
backup_redis() {
|
||||
target=redis_${db}_${dbhost}_${now}.rdb
|
||||
echo bgsave | redis-cli -h ${dbhost} -p ${dbport} ${REDIS_PASS_STR} --rdb ${TEMP_LOCATION}/${target} ${EXTRA_OPTS}
|
||||
print_info "Dumping Redis - Flushing Redis Cache First"
|
||||
sleep 10
|
||||
try=5
|
||||
while [ $try -gt 0 ] ; do
|
||||
saved=$(echo 'info Persistence' | redis-cli -h ${dbhost} -p ${dbport} ${REDIS_PASS_STR} | awk '/rdb_bgsave_in_progress:0/{print "saved"}')
|
||||
ok=$(echo 'info Persistence' | redis-cli -h ${dbhost} -p ${dbport} ${REDIS_PASS_STR} | awk '/rdb_last_bgsave_status:ok/{print "ok"}')
|
||||
if [[ "$saved" = "saved" ]] && [[ "$ok" = "ok" ]]; then
|
||||
print_info "Redis Backup Complete"
|
||||
break
|
||||
fi
|
||||
try=$((try - 1))
|
||||
print_info "Redis Busy - Waiting and retrying in 5 seconds"
|
||||
* )
|
||||
sleep 5
|
||||
done
|
||||
target_original=${target}
|
||||
compression
|
||||
$dumpoutput "${TEMP_LOCATION}/${target_original}"
|
||||
generate_md5
|
||||
move_backup
|
||||
}
|
||||
|
||||
backup_sqlite3() {
|
||||
db=$(basename "$dbhost")
|
||||
db="${db%.*}"
|
||||
target=sqlite3_${db}_${now}.sqlite3
|
||||
compression
|
||||
|
||||
print_info "Dumping sqlite3 database: ${dbhost}"
|
||||
sqlite3 "${dbhost}" ".backup '${TEMP_LOCATION}/backup.sqlite3'"
|
||||
exit_code=$?
|
||||
|
||||
cat "${TEMP_LOCATION}/backup.sqlite3" | $dumpoutput > "${TEMP_LOCATION}/${target}"
|
||||
|
||||
generate_md5
|
||||
move_backup
|
||||
}
|
||||
|
||||
check_availability() {
|
||||
### Set the Database Type
|
||||
case "$dbtype" in
|
||||
"couch" )
|
||||
COUNTER=0
|
||||
while ! (nc -z ${dbhost} ${dbport}) ; do
|
||||
sleep 5
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "CouchDB Host '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"influx" )
|
||||
COUNTER=0
|
||||
while ! (nc -z ${dbhost} ${dbport}) ; do
|
||||
sleep 5
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "InfluxDB Host '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"mongo" )
|
||||
COUNTER=0
|
||||
while ! (nc -z ${dbhost} ${dbport}) ; do
|
||||
sleep 5
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "Mongo Host '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"mysql" )
|
||||
COUNTER=0
|
||||
export MYSQL_PWD=${dbpass}
|
||||
while ! (mysqladmin -u"${dbuser}" -P"${dbport}" -h"${dbhost}" status > /dev/null 2>&1) ; do
|
||||
sleep 5
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "MySQL/MariaDB Server '${dbhost}' is not accessible, retrying.. (${COUNTER} seconds so far)"
|
||||
done
|
||||
;;
|
||||
"mssql" )
|
||||
COUNTER=0
|
||||
while ! (nc -z ${dbhost} ${dbport}) ; do
|
||||
sleep 5
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "MSSQL Host '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"pgsql" )
|
||||
COUNTER=0
|
||||
export PGPASSWORD=${dbpass}
|
||||
until pg_isready --dbname=${dbname} --host=${dbhost} --port=${dbport} --username=${dbuser} -q
|
||||
do
|
||||
sleep 5
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "Postgres Host '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"redis" )
|
||||
COUNTER=0
|
||||
while ! (nc -z "${dbhost}" "${dbport}") ; do
|
||||
sleep 5
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "Redis Host '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"sqlite3" )
|
||||
if [[ ! -e "${dbhost}" ]]; then
|
||||
print_error "File '${dbhost}' does not exist."
|
||||
exit_code=2
|
||||
exit $exit_code
|
||||
elif [[ ! -f "${dbhost}" ]]; then
|
||||
print_error "File '${dbhost}' is not a file."
|
||||
exit_code=2
|
||||
exit $exit_code
|
||||
elif [[ ! -r "${dbhost}" ]]; then
|
||||
print_error "File '${dbhost}' is not readable."
|
||||
exit_code=2
|
||||
exit $exit_code
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
compression() {
|
||||
case "$COMPRESSION" in
|
||||
"GZ" | "gz" | "gzip" | "GZIP")
|
||||
print_notice "Compressing backup with gzip"
|
||||
target=${target}.gz
|
||||
dumpoutput="$gzip "
|
||||
;;
|
||||
"BZ" | "bz" | "bzip2" | "BZIP2" | "bzip" | "BZIP" | "bz2" | "BZ2")
|
||||
print_notice "Compressing backup with bzip2"
|
||||
target=${target}.bz2
|
||||
dumpoutput="$bzip "
|
||||
;;
|
||||
"XZ" | "xz" | "XZIP" | "xzip" )
|
||||
print_notice "Compressing backup with xzip"
|
||||
target=${target}.xz
|
||||
dumpoutput="$xzip "
|
||||
;;
|
||||
"ZSTD" | "zstd" | "ZST" | "zst" )
|
||||
print_notice "Compressing backup with zstd"
|
||||
target=${target}.zst
|
||||
dumpoutput="$zstd "
|
||||
;;
|
||||
"NONE" | "none" | "FALSE" | "false")
|
||||
dumpoutput="cat "
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
generate_md5() {
|
||||
if var_true "$MD5" ; then
|
||||
print_notice "Generating MD5 for ${target}"
|
||||
cd ${TEMP_LOCATION}
|
||||
md5sum "${target}" > "${target}".md5
|
||||
MD5VALUE=$(md5sum "${target}" | awk '{ print $1}')
|
||||
fi
|
||||
}
|
||||
|
||||
move_backup() {
|
||||
case "$SIZE_VALUE" in
|
||||
"b" | "bytes" )
|
||||
SIZE_VALUE=1
|
||||
|
||||
;;
|
||||
"[kK]" | "[kK][bB]" | "kilobytes" | "[mM]" | "[mM][bB]" | "megabytes" )
|
||||
SIZE_VALUE="-h"
|
||||
;;
|
||||
*)
|
||||
SIZE_VALUE=1
|
||||
;;
|
||||
esac
|
||||
if [ "$SIZE_VALUE" = "1" ] ; then
|
||||
FILESIZE=$(stat -c%s "${TEMP_LOCATION}/${target}")
|
||||
print_notice "Backup of ${target} created with the size of ${FILESIZE} bytes"
|
||||
else
|
||||
FILESIZE=$(du -h "${TEMP_LOCATION}/${target}" | awk '{ print $1}')
|
||||
print_notice "Backup of ${target} created with the size of ${FILESIZE}"
|
||||
fi
|
||||
|
||||
case "${BACKUP_LOCATION}" in
|
||||
"FILE" | "file" | "filesystem" | "FILESYSTEM" )
|
||||
mkdir -p "${DB_DUMP_TARGET}"
|
||||
mv ${TEMP_LOCATION}/*.md5 "${DB_DUMP_TARGET}"/
|
||||
mv ${TEMP_LOCATION}/"${target}" "${DB_DUMP_TARGET}"/"${target}"
|
||||
;;
|
||||
"S3" | "s3" | "MINIO" | "minio" )
|
||||
export AWS_ACCESS_KEY_ID=${S3_KEY_ID}
|
||||
export AWS_SECRET_ACCESS_KEY=${S3_KEY_SECRET}
|
||||
export AWS_DEFAULT_REGION=${S3_REGION}
|
||||
|
||||
[[ ( -n "${S3_HOST}" ) ]] && PARAM_AWS_ENDPOINT_URL=" --endpoint-url ${S3_PROTOCOL}://${S3_HOST}"
|
||||
|
||||
aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_LOCATION}/${target} s3://${S3_BUCKET}/${S3_PATH}/${target}
|
||||
|
||||
rm -rf ${TEMP_LOCATION}/*.md5
|
||||
rm -rf ${TEMP_LOCATION}/"${target}"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
;;
|
||||
esac
|
||||
|
||||
bootstrap_compression
|
||||
bootstrap_variables
|
||||
|
||||
### Container Startup
|
||||
print_debug "Backup routines Initialized on $(date)"
|
||||
|
||||
### Wait for Next time to start backup
|
||||
if [ "$1" != "NOW" ]; then
|
||||
current_time=$(date +"%s")
|
||||
today=$(date +"%Y%m%d")
|
||||
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))
|
||||
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
|
||||
waittime=$(($target_time - $current_time))
|
||||
fi
|
||||
print_debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}"
|
||||
print_notice "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")"
|
||||
sleep $waittime
|
||||
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
|
||||
# make sure the directory exists
|
||||
mkdir -p $TEMP_LOCATION
|
||||
|
||||
### Define Target name
|
||||
while true; do
|
||||
mkdir -p "${TEMP_LOCATION}"
|
||||
backup_start_time=$(date +"%s")
|
||||
print_debug "Backup start time: ${backup_start_time} - $(date -d @${backup_start_time} +"%Y-%m-%d %T %Z")"
|
||||
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
|
||||
### Take a Dump
|
||||
case "${dbtype,,}" in
|
||||
"couch" )
|
||||
check_availability
|
||||
backup_couch
|
||||
@@ -498,45 +91,50 @@ print_debug "Backup routines Initialized on $(date)"
|
||||
;;
|
||||
esac
|
||||
|
||||
### Zabbix
|
||||
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 "$(date -r "${DB_DUMP_TARGET}"/"${target}" +'%s')"
|
||||
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_notice "Cleaning up old backups"
|
||||
### 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
|
||||
|
||||
if [ -n "$POST_SCRIPT" ] ; then
|
||||
print_notice "Found POST_SCRIPT environment variable. Executing"
|
||||
eval "${POST_SCRIPT}"
|
||||
### 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_timme}" "${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 Custom Filesystem Scripts to Execute"
|
||||
### 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 DATE BACKUP_FILENAME FILESIZE MD5_VALUE
|
||||
chmod +x "${f}"
|
||||
${f} "${exit_code}" "${dbtype}" "${dbhost}" "${dbname}" "${now_date}" "${now_time}" "${target}" "${FILESIZE}" "${MD5VALUE}"
|
||||
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
|
||||
|
||||
### Go back to Sleep until next Backup time
|
||||
if var_true $MANUAL ; then
|
||||
exit 0;
|
||||
if var_true "${manual}" ; then
|
||||
print_debug "Exitting due to manual mode"
|
||||
exit ${exit_code};
|
||||
else
|
||||
backup_finish_time=$(date +"%s")
|
||||
backup_total_time=$(echo $((backup_finish_time-backup_start_time)))
|
||||
print_debug "Backup finish time: ${backup_finish_time} - $(date -d @${backup_start_time} +"%Y-%m-%d %T %Z")"
|
||||
print_debug "Backup time elapsed: $(echo ${backup_total_time} | awk '{printf "Hours: *%d* Minutes: *%02d* Seconds: *%02d*", $1/3600, ($1/60)%60, $1%60}')"
|
||||
### 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
|
||||
|
||||
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user