mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-21 13:23:12 +01:00
Release 2.1.0 - See CHANGELOG.md
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
## 2.1.0 2020-08-29 <dave at tiredofit dot ca>
|
||||
|
||||
### Added
|
||||
- Add Exit Code variable to be used for custom scripts - See README.md for placement
|
||||
- Add POST_SCRIPT environment variable to execute command instead of relying on custom script
|
||||
|
||||
|
||||
## 2.0.0 2020-06-17 <dave at tiredofit dot ca>
|
||||
|
||||
### Added
|
||||
|
||||
140
README.md
140
README.md
@@ -1,12 +1,11 @@
|
||||
# hub.docker.com/r/tiredofit/db-backup
|
||||
|
||||
|
||||
[](https://hub.docker.com/r/tiredofit/db-backup)
|
||||
[](https://hub.docker.com/r/tiredofit/db-backup)
|
||||
[](https://hub.docker.com/r/tiredofit/db-backup)
|
||||
[](https://microbadger.com/images/tiredofit/db-backup)
|
||||
|
||||
# Introduction
|
||||
## Introduction
|
||||
|
||||
This will build a container for backing up multiple type of DB Servers
|
||||
|
||||
@@ -27,40 +26,49 @@ Currently backs up CouchDB, InfluxDB, MySQL, MongoDB, Postgres, Redis servers.
|
||||
|
||||
[Changelog](CHANGELOG.md)
|
||||
|
||||
# Authors
|
||||
## Authors
|
||||
|
||||
- [Dave Conroy](https://github.com/tiredofit)
|
||||
|
||||
# Table of Contents
|
||||
## Table of Contents
|
||||
|
||||
- [Introduction](#introduction)
|
||||
- [Changelog](CHANGELOG.md)
|
||||
- [Authors](#authors)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [Prerequisites](#prerequisites)
|
||||
- [Installation](#installation)
|
||||
- [Quick Start](#quick-start)
|
||||
- [Quick Start](#quick-start)
|
||||
- [Configuration](#configuration)
|
||||
- [Data Volumes](#data-volumes)
|
||||
- [Environment Variables](#environmentvariables)
|
||||
- [Data-Volumes](#data-volumes)
|
||||
- [Environment Variables](#environment-variables)
|
||||
- [Maintenance](#maintenance)
|
||||
- [Shell Access](#shell-access)
|
||||
- [Shell Access](#shell-access)
|
||||
- [Custom Scripts](#custom-scripts)
|
||||
- [Example Post Script](#example-post-script)
|
||||
- [$1=EXIT_CODE (After running backup routine)](#1exit_code-after-running-backup-routine)
|
||||
- [$2=DB_TYPE (Type of Backup)](#2db_type-type-of-backup)
|
||||
- [$3=DB_HOST (Backup Host)](#3db_host-backup-host)
|
||||
- [#4=DB_NAME (Name of Database backed up](#4db_name-name-of-database-backed-up)
|
||||
- [$5=DATE (Date of Backup)](#5date-date-of-backup)
|
||||
- [$6=TIME (Time of Backup)](#6time-time-of-backup)
|
||||
- [$7=BACKUP_FILENAME (Filename of Backup)](#7backup_filename-filename-of-backup)
|
||||
- [$8=FILESIZE (Filesize of backup)](#8filesize-filesize-of-backup)
|
||||
- [$9=MD5_RESULT (MD5Sum if enabled)](#9md5_result-md5sum-if-enabled)
|
||||
|
||||
# Prerequisites
|
||||
## Prerequisites
|
||||
|
||||
You must have a working DB server or container available for this to work properly, it does not provide server functionality!
|
||||
|
||||
|
||||
# Installation
|
||||
## Installation
|
||||
|
||||
Automated builds of the image are available on [Docker Hub](https://hub.docker.com/r/tiredofit/db-backup) and is the recommended
|
||||
method of installation.
|
||||
|
||||
|
||||
```bash
|
||||
docker pull tiredofit/db-backup:latest
|
||||
```
|
||||
|
||||
# Quick Start
|
||||
### Quick Start
|
||||
|
||||
* The quickest way to get started is using [docker-compose](https://docs.docker.com/compose/). See the examples folder for a working [docker-compose.yml](examples/docker-compose.yml) that can be modified for development or production use.
|
||||
|
||||
@@ -69,46 +77,45 @@ docker pull tiredofit/db-backup:latest
|
||||
|
||||
> **NOTE**: If you are using this with a docker-compose file along with a seperate SQL container, take care not to set the variables to backup immediately, more so have it delay execution for a minute, otherwise you will get a failed first backup.
|
||||
|
||||
# Configuration
|
||||
## Configuration
|
||||
|
||||
## Data-Volumes
|
||||
### Data-Volumes
|
||||
|
||||
The following directories are used for configuration and can be mapped for persistent storage.
|
||||
|
||||
| Directory | Description |
|
||||
|-----------|-------------|
|
||||
| `/backup` | Backups |
|
||||
| `/assets/custom-scripts` | *Optional* Put custom scripts in this directory to execute after backup operations
|
||||
| Directory | Description |
|
||||
| ------------------------ | ---------------------------------------------------------------------------------- |
|
||||
| `/backup` | Backups |
|
||||
| `/assets/custom-scripts` | *Optional* Put custom scripts in this directory to execute after backup operations |
|
||||
|
||||
|
||||
## Environment Variables
|
||||
### Environment Variables
|
||||
|
||||
*If you are trying to backup a database that doesn't have a user or a password (you should!) make sure you set `CONTAINER_ENABLE_DOCKER_SECRETS=FALSE`*
|
||||
|
||||
Along with the Environment Variables from the [Base image](https://hub.docker.com/r/tiredofit/alpine), below is the complete list of available options that can be used to customize your installation.
|
||||
|
||||
|
||||
| Parameter | Description |
|
||||
|-----------|-------------|
|
||||
| `BACKUP_LOCATION` | Backup to `FILESYSTEM` or `S3` compatible services like S3, Minio, Wasabi - Default `FILESYSTEM`
|
||||
| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, ZSTD `ZSTD` or none `NONE` - Default `GZ`
|
||||
| `COMPRESSION_LEVEL` | Numberical value of what level of compression to use, most allow `1` to `9` except for `ZSTD` which allows for `1` to `19` - Default `3` |
|
||||
| `DB_TYPE` | Type of DB Server to backup `couch` `influx` `mysql` `pgsql` `mongo` `redis` |
|
||||
| `DB_HOST` | Server Hostname e.g. `mariadb`
|
||||
| `DB_NAME` | Schema Name e.g. `database`
|
||||
| `DB_USER` | username for the database - use `root` to backup all MySQL of them.
|
||||
| `DB_PASS` | (optional if DB doesn't require it) password for the database
|
||||
| `DB_PORT` | (optional) Set port to connect to DB_HOST. Defaults are provided
|
||||
| `DB_DUMP_FREQ` | How often to do a dump, in minutes. Defaults to 1440 minutes, or once per day.
|
||||
| `DB_DUMP_BEGIN` | What time to do the first dump. Defaults to immediate. Must be in one of two formats
|
||||
| | Absolute HHMM, e.g. `2330` or `0415`
|
||||
| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half
|
||||
| `DB_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when dump freqency fires). 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything.
|
||||
| `DEBUG_MODE` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed.
|
||||
| `EXTRA_OPTS` | If you need to pass extra arguments to the backup command, add them here e.g. "--extra-command"
|
||||
| `MD5` | Generate MD5 Sum in Directory, `TRUE` or `FALSE` - Default `TRUE`
|
||||
| `PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` - Default `TRUE` |
|
||||
| `SPLIT_DB` | If using root as username and multiple DBs on system, set to TRUE to create Seperate DB Backups instead of all in one. - Default `FALSE` |
|
||||
| Parameter | Description |
|
||||
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `BACKUP_LOCATION` | Backup to `FILESYSTEM` or `S3` compatible services like S3, Minio, Wasabi - Default `FILESYSTEM` |
|
||||
| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, ZSTD `ZSTD` or none `NONE` - Default `GZ` |
|
||||
| `COMPRESSION_LEVEL` | Numberical value of what level of compression to use, most allow `1` to `9` except for `ZSTD` which allows for `1` to `19` - Default `3` |
|
||||
| `DB_TYPE` | Type of DB Server to backup `couch` `influx` `mysql` `pgsql` `mongo` `redis` |
|
||||
| `DB_HOST` | Server Hostname e.g. `mariadb` |
|
||||
| `DB_NAME` | Schema Name e.g. `database` |
|
||||
| `DB_USER` | username for the database - use `root` to backup all MySQL of them. |
|
||||
| `DB_PASS` | (optional if DB doesn't require it) password for the database |
|
||||
| `DB_PORT` | (optional) Set port to connect to DB_HOST. Defaults are provided |
|
||||
| `DB_DUMP_FREQ` | How often to do a dump, in minutes. Defaults to 1440 minutes, or once per day. |
|
||||
| `DB_DUMP_BEGIN` | What time to do the first dump. Defaults to immediate. Must be in one of two formats |
|
||||
| | Absolute HHMM, e.g. `2330` or `0415` |
|
||||
| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half |
|
||||
| `DB_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when dump freqency fires). 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. |
|
||||
| `DEBUG_MODE` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed. |
|
||||
| `EXTRA_OPTS` | If you need to pass extra arguments to the backup command, add them here e.g. "--extra-command" |
|
||||
| `MD5` | Generate MD5 Sum in Directory, `TRUE` or `FALSE` - Default `TRUE` |
|
||||
| `PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` - Default `TRUE` |
|
||||
| `POST_SCRIPT` | Fill this variable in with a command to execute post the script backing up | |
|
||||
| `SPLIT_DB` | If using root as username and multiple DBs on system, set to TRUE to create Seperate DB Backups instead of all in one. - Default `FALSE` |
|
||||
|
||||
When using compression with MongoDB, only `GZ` compression is possible.
|
||||
|
||||
@@ -116,22 +123,21 @@ When using compression with MongoDB, only `GZ` compression is possible.
|
||||
|
||||
If `BACKUP_LOCATION` = `S3` then the following options are used.
|
||||
|
||||
| Parameter | Description |
|
||||
|-----------|-------------|
|
||||
| `S3_BUCKET` | S3 Bucket name e.g. 'mybucket' |
|
||||
| `S3_HOSTNAME` | Hostname of S3 Server e.g "s3.amazonaws.com" - You can also include a port if necessary
|
||||
| `S3_KEY_ID` | S3 Key ID |
|
||||
| `S3_KEY_SECRET` | S3 Key Secret |
|
||||
| `S3_PATH` | S3 Pathname to save to e.g. '`backup`' |
|
||||
| `S3_PROTOCOL` | Use either `http` or `https` to access service - Default `https` |
|
||||
| `S3_URI_STYLE` | Choose either `VIRTUALHOST` or `PATH` style - Default `VIRTUALHOST`
|
||||
| Parameter | Description |
|
||||
| --------------- | --------------------------------------------------------------------------------------- |
|
||||
| `S3_BUCKET` | S3 Bucket name e.g. 'mybucket' |
|
||||
| `S3_HOSTNAME` | Hostname of S3 Server e.g "s3.amazonaws.com" - You can also include a port if necessary |
|
||||
| `S3_KEY_ID` | S3 Key ID |
|
||||
| `S3_KEY_SECRET` | S3 Key Secret |
|
||||
| `S3_PATH` | S3 Pathname to save to e.g. '`backup`' |
|
||||
| `S3_PROTOCOL` | Use either `http` or `https` to access service - Default `https` |
|
||||
| `S3_URI_STYLE` | Choose either `VIRTUALHOST` or `PATH` style - Default `VIRTUALHOST` |
|
||||
|
||||
## Maintenance
|
||||
|
||||
Manual Backups can be performed by entering the container and typing `backup-now`
|
||||
|
||||
|
||||
#### Shell Access
|
||||
### Shell Access
|
||||
|
||||
For debugging and maintenance purposes you may want access the containers shell.
|
||||
|
||||
@@ -144,23 +150,25 @@ docker exec -it (whatever your container name is e.g.) db-backup bash
|
||||
If you want to execute a custom script at the end of backup, you can drop bash scripts with the extension of `.sh` in this directory. See the following example to utilize:
|
||||
|
||||
````bash
|
||||
$ cat post-script.sh
|
||||
$ cat post-script.sh
|
||||
##!/bin/bash
|
||||
|
||||
## Example Post Script
|
||||
## $1=DB_TYPE (Type of Backup)
|
||||
## $2=DB_HOST (Backup Host)
|
||||
## #3=DB_NAME (Name of Database backed up
|
||||
## $4=DATE (Date of Backup)
|
||||
## $5=TIME (Time of Backup)
|
||||
## $6=BACKUP_FILENAME (Filename of Backup)
|
||||
## $7=FILESIZE (Filesize of backup)
|
||||
## $8=MD5_RESULT (MD5Sum if enabled)
|
||||
## $1=EXIT_CODE (After running backup routine)
|
||||
## $2=DB_TYPE (Type of Backup)
|
||||
## $3=DB_HOST (Backup Host)
|
||||
## #4=DB_NAME (Name of Database backed up
|
||||
## $5=DATE (Date of Backup)
|
||||
## $6=TIME (Time of Backup)
|
||||
## $7=BACKUP_FILENAME (Filename of Backup)
|
||||
## $8=FILESIZE (Filesize of backup)
|
||||
## $9=MD5_RESULT (MD5Sum if enabled)
|
||||
|
||||
echo "${1} Backup Completed on ${2} for ${3} on ${4} ${5}. Filename: ${6} Size: ${7} bytes MD5: ${8}"
|
||||
echo "${1} ${2} Backup Completed on ${3} for ${4} on ${5} ${6}. Filename: ${7} Size: ${8} bytes MD5: ${9}"
|
||||
````
|
||||
|
||||
Outputs the following on the console:
|
||||
|
||||
`mysql Backup Completed on example-db for example on 2020-04-22 05:19:10. Filename: mysql_example_example-db_20200422-051910.sql.bz2 Size: 7795 bytes MD5: 952fbaafa30437494fdf3989a662cd40`
|
||||
`0 mysql Backup Completed on example-db for example on 2020-04-22 05:19:10. Filename: mysql_example_example-db_20200422-051910.sql.bz2 Size: 7795 bytes MD5: 952fbaafa30437494fdf3989a662cd40`
|
||||
|
||||
If you wish to change the size value from bytes to megabytes set environment variable `SIZE_VALUE=megabytes`
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
##!/bin/bash
|
||||
|
||||
## Example Post Script
|
||||
## $1=DB_TYPE (Type of Backup)
|
||||
## $2=DB_HOST (Backup Host)
|
||||
## #3=DB_NAME (Name of Database backed up
|
||||
## $4=DATE (Date of Backup)
|
||||
## $5=TIME (Time of Backup)
|
||||
## $6=BACKUP_FILENAME (Filename of Backup)
|
||||
## $7=FILESIZE (Filesize of backup)
|
||||
## $8=MD5_RESULT (MD5Sum if enabled)
|
||||
## $1=EXIT_CODE (After running backup routine)
|
||||
## $2=DB_TYPE (Type of Backup)
|
||||
## $3=DB_HOST (Backup Host)
|
||||
## #4=DB_NAME (Name of Database backed up
|
||||
## $5=DATE (Date of Backup)
|
||||
## $6=TIME (Time of Backup)
|
||||
## $7=BACKUP_FILENAME (Filename of Backup)
|
||||
## $8=FILESIZE (Filesize of backup)
|
||||
## $9=MD5_RESULT (MD5Sum if enabled)
|
||||
|
||||
echo "${1} Backup Completed on ${2} for ${3} on ${4} ${5}. Filename: ${6} Size: ${7} bytes MD5: ${8}"
|
||||
echo "${1} ${2} Backup Completed on ${3} for ${4} on ${5} ${6}. Filename: ${7} Size: ${8} bytes MD5: ${9}"
|
||||
|
||||
@@ -35,7 +35,7 @@ SIZE_VALUE=${SIZE_VALUE:-"bytes"}
|
||||
SPLIT_DB=${SPLIT_DB:-FALSE}
|
||||
tmpdir=/tmp/backups
|
||||
|
||||
if [ "BACKUP_TYPE" = "S3" ] || [ "BACKUP_TYPE" = "s3" ] || [ "BACKUP_TYPE" = "MINIO" ] || [ "BACKUP_TYPE" = "minio" ] ; then
|
||||
if [ "$BACKUP_TYPE" = "S3" ] || [ "$BACKUP_TYPE" = "s3" ] || [ "$BACKUP_TYPE" = "MINIO" ] || [ "$BACKUP_TYPE" = "minio" ] ; then
|
||||
S3_PROTOCOL=${S3_PROTOCOL:-"https"}
|
||||
sanity_var S3_HOST "S3 Host"
|
||||
sanity_var S3_BUCKET "S3 Bucket"
|
||||
@@ -53,7 +53,7 @@ if [ "$1" = "NOW" ]; then
|
||||
fi
|
||||
|
||||
### Set Compression Options
|
||||
if var_true $PARALLEL_COMPRESSION ; then
|
||||
if var_true "$PARALLEL_COMPRESSION" ; then
|
||||
bzip="pbzip2 -${COMPRESSION_LEVEL}"
|
||||
gzip="pigz -${COMPRESSION_LEVEL}"
|
||||
xzip="pixz -${COMPRESSION_LEVEL}"
|
||||
@@ -104,13 +104,14 @@ backup_couch() {
|
||||
target=couch_${dbname}_${dbhost}_${now}.txt
|
||||
compression
|
||||
curl -X GET http://${dbhost}:${dbport}/${dbname}/_all_docs?include_docs=true ${dumpoutput} | $dumpoutput > ${tmpdir}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
}
|
||||
|
||||
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`
|
||||
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
|
||||
@@ -118,6 +119,7 @@ backup_mysql() {
|
||||
target=mysql_${db}_${dbhost}_${now}.sql
|
||||
compression
|
||||
mysqldump --max-allowed-packet=512M -h $dbhost -P $dbport -u$dbuser ${EXTRA_OPTS} --databases $db | $dumpoutput > ${tmpdir}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
fi
|
||||
@@ -125,6 +127,7 @@ backup_mysql() {
|
||||
else
|
||||
compression
|
||||
mysqldump --max-allowed-packet=512M -A -h $dbhost -P $dbport -u$dbuser ${EXTRA_OPTS} | $dumpoutput > ${tmpdir}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
fi
|
||||
@@ -139,6 +142,7 @@ backup_influx() {
|
||||
fi
|
||||
for DB in $DB_NAME; do
|
||||
influxd backup ${influx_compression} -database $DB -host ${dbhost}:${dbport} ${tmpdir}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
done
|
||||
@@ -152,7 +156,8 @@ backup_mongo() {
|
||||
target=${dbtype}_${dbname}_${dbhost}_${now}.archivegz
|
||||
mongo_compression="--gzip"
|
||||
fi
|
||||
mongodump --archive=${tmpdir}/${target} ${mongo_compression} --host ${dbhost} --port ${dbport} ${MONGO_USER_STR}${MONGO_PASS_STR}${MONGO_DB_STR} ${EXTRA_OPTS}
|
||||
mongodump --archive=${tmpdir}/${target} ${mongo_compression} --host ${dbhost} --port ${dbport} ${MONGO_USER_STR}${MONGO_PASS_STR}${MONGO_DB_STR} ${EXTRA_OPTS}
|
||||
exit_code=$?
|
||||
cd ${tmpdir}
|
||||
generate_md5
|
||||
move_backup
|
||||
@@ -161,12 +166,13 @@ backup_mongo() {
|
||||
backup_pgsql() {
|
||||
if var_true $SPLIT_DB ; then
|
||||
export PGPASSWORD=${dbpass}
|
||||
DATABASES=`psql -h $dbhost -U $dbuser -p ${dbport} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' `
|
||||
DATABASES=$(psql -h $dbhost -U $dbuser -p ${dbport} -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 > ${tmpdir}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
done
|
||||
@@ -174,6 +180,7 @@ backup_pgsql() {
|
||||
export PGPASSWORD=${dbpass}
|
||||
compression
|
||||
pg_dump -h ${dbhost} -U ${dbuser} -p ${dbport} ${dbname} ${EXTRA_OPTS} | $dumpoutput > ${tmpdir}/${target}
|
||||
exit_code=$?
|
||||
generate_md5
|
||||
move_backup
|
||||
fi
|
||||
@@ -207,60 +214,60 @@ check_availability() {
|
||||
COUNTER=0
|
||||
while ! (nc -z ${dbhost} ${dbport}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
print_warn "CouchDB Host '"$dbhost"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
(( 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
|
||||
let COUNTER+=5
|
||||
print_warn "InfluxDB Host '"$dbhost"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
(( 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
|
||||
let COUNTER+=5
|
||||
print_warn "Mongo Host '"$dbhost"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "Mongo Host '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"mysql" )
|
||||
COUNTER=0
|
||||
while true; do
|
||||
mysqlcmd='mysql -u'${dbuser}' -P '${dbport}' -h '${dbhost}' -p'${dbpass}
|
||||
out="`$mysqlcmd -e "SELECT COUNT(*) FROM information_schema.FILES;" 2>&1`"
|
||||
out="$($mysqlcmd -e "SELECT COUNT(*) FROM information_schema.FILES;" 2>&1)"
|
||||
echo "$out" | grep -E "COUNT|Enter" 2>&1 > /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
:
|
||||
break
|
||||
fi
|
||||
print_warn "MySQL/MariaDB Server "$dbhost" is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
print_warn "MySQL/MariaDB Server '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
(( COUNTER+=5 ))
|
||||
done
|
||||
;;
|
||||
"pgsql" )
|
||||
COUNTER=0
|
||||
export PGPASSWORD=${dbpass}
|
||||
export PGPASSWORD=${dbpass}
|
||||
until pg_isready --dbname=${dbname} --host=${dbhost} --port=${dbport} --username=${dbuser} -q
|
||||
do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
print_warn "Postgres Host '"$dbhost"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
(( 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
|
||||
while ! (nc -z "${dbhost}" "${dbport}") ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
print_warn "Redis Host '"$dbhost"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
(( COUNTER+=5 ))
|
||||
print_warn "Redis Host '${dbhost}' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
esac
|
||||
esac
|
||||
}
|
||||
|
||||
compression() {
|
||||
@@ -292,11 +299,11 @@ compression() {
|
||||
}
|
||||
|
||||
generate_md5() {
|
||||
if var_true $MD5 ; then
|
||||
if var_true "$MD5" ; then
|
||||
print_notice "Generating MD5 for ${target}"
|
||||
cd $tmpdir
|
||||
md5sum ${target} > ${target}.md5
|
||||
MD5VALUE=$(md5sum ${target} | awk '{ print $1}')
|
||||
md5sum "${target}" > "${target}".md5
|
||||
MD5VALUE=$(md5sum "${target}" | awk '{ print $1}')
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -320,27 +327,27 @@ move_backup() {
|
||||
FILESIZE=$(du -h "${tmpdir}/${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 ${tmpdir}/*.md5 ${DB_DUMP_TARGET}/
|
||||
mv ${tmpdir}/${target} ${DB_DUMP_TARGET}/${target}
|
||||
mkdir -p "${DB_DUMP_TARGET}"
|
||||
mv ${tmpdir}/*.md5 "${DB_DUMP_TARGET}"/
|
||||
mv ${tmpdir}/"${target}" "${DB_DUMP_TARGET}"/"${target}"
|
||||
;;
|
||||
"S3" | "s3" | "MINIO" | "minio" )
|
||||
s3_content_type="application/octet-stream"
|
||||
if [ "$S3_URI_STYLE" = "VIRTUALHOST" ] || [ "$S3_URI_STYLE" = "VHOST" ] [ "$S3_URI_STYLE" = "virtualhost" ] [ "$S3_URI_STYLE" = "vhost" ] ; then
|
||||
if [ "$S3_URI_STYLE" = "VIRTUALHOST" ] || [ "$S3_URI_STYLE" = "VHOST" ] || [ "$S3_URI_STYLE" = "virtualhost" ] || [ "$S3_URI_STYLE" = "vhost" ] ; then
|
||||
s3_url="${S3_BUCKET}.${S3_HOST}"
|
||||
else
|
||||
s3_url="${S3_HOST}/${S3_BUCKET}"
|
||||
fi
|
||||
|
||||
if var_true $MD5 ; then
|
||||
|
||||
if var_true "$MD5" ; then
|
||||
s3_date="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
|
||||
s3_md5="$(libressl md5 -binary < "${tmpdir}/${target}.md5" | base64)"
|
||||
sig="$(printf "PUT\n$s3_md5\n${s3_content_type}\n$s3_date\n/$S3_BUCKET/$S3_PATH/${target}.md5" | libressl sha1 -binary -hmac "${S3_KEY_SECRET}" | base64)"
|
||||
print_debug "Uploading ${target}.md5 to S3"
|
||||
curl -T "${tmpdir}/${target}.md5" ${S3_PROTOCOL}://${s3_url}/${S3_PATH}/${target}.md5 \
|
||||
curl -T "${tmpdir}/${target}.md5" "${S3_PROTOCOL}"://"${s3_url}"/"${S3_PATH}"/"${target}".md5 \
|
||||
-H "Date: $date" \
|
||||
-H "Authorization: AWS ${S3_KEY_ID}:$sig" \
|
||||
-H "Content-Type: ${s3_content_type}" \
|
||||
@@ -351,21 +358,21 @@ move_backup() {
|
||||
s3_md5="$(libressl md5 -binary < "${tmpdir}/${target}" | base64)"
|
||||
sig="$(printf "PUT\n$s3_md5\n${s3_content_type}\n$s3_date\n/$S3_BUCKET/$S3_PATH/${target}" | libressl sha1 -binary -hmac "${S3_KEY_SECRET}" | base64)"
|
||||
print_debug "Uploading ${target} to S3"
|
||||
curl -T ${tmpdir}/${target} ${S3_PROTOCOL}://${s3_url}/${S3_PATH}/${target} \
|
||||
curl -T ${tmpdir}/"${target}" "${S3_PROTOCOL}"://"${s3_url}"/"${S3_PATH}"/"${target}" \
|
||||
-H "Date: $s3_date" \
|
||||
-H "Authorization: AWS ${S3_KEY_ID}:$sig" \
|
||||
-H "Content-Type: ${s3_content_type}" \
|
||||
-H "Content-MD5: ${s3_md5}"
|
||||
|
||||
rm -rf ${tmpdir}/*.md5
|
||||
rm -rf ${tmpdir}/${target}
|
||||
rm -rf ${tmpdir}/"${target}"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
|
||||
### Container Startup
|
||||
print_info "Backup routines Initialized on `date`"
|
||||
print_debug "Backup routines Initialized on $(date)"
|
||||
|
||||
### Wait for Next time to start backup
|
||||
current_time=$(date +"%s")
|
||||
@@ -381,6 +388,7 @@ print_info "Backup routines Initialized on `date`"
|
||||
waittime=$(($target_time - $current_time))
|
||||
fi
|
||||
|
||||
print_notice "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")"
|
||||
sleep $waittime
|
||||
|
||||
|
||||
@@ -424,16 +432,16 @@ print_info "Backup routines Initialized on `date`"
|
||||
esac
|
||||
|
||||
### Zabbix
|
||||
if var_true $ENABLE_ZABBIX ; then
|
||||
if var_true "$ENABLE_ZABBIX" ; 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.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')"
|
||||
fi
|
||||
|
||||
### Automatic Cleanup
|
||||
if [[ -n "$DB_CLEANUP_TIME" ]]; then
|
||||
print_notice "Cleaning up old backups"
|
||||
find $DB_DUMP_TARGET/ -mmin +$DB_CLEANUP_TIME -iname "*" -exec rm {} \;
|
||||
find "${DB_DUMP_TARGET}"/ -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm {} \;
|
||||
fi
|
||||
|
||||
### Post Backup Custom Script Support
|
||||
@@ -441,11 +449,11 @@ print_info "Backup routines Initialized on `date`"
|
||||
print_notice "Found Custom Scripts to Execute"
|
||||
for f in $(find /assets/custom-scripts/ -name \*.sh -type f); do
|
||||
print_notice "Running Script ${f}"
|
||||
## script DB_TYPE DB_HOST DB_NAME DATE BACKUP_FILENAME FILESIZE MD5_VALUE
|
||||
chmod +x ${f}
|
||||
${f} "${dbtype}" "${dbhost}" "${dbname}" "${now_date}" "${now_time}" "${target}" "${FILESIZE}" "${MD5VALUE}"
|
||||
## 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}"
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
### Go back to Sleep until next Backup time
|
||||
if var_true $MANUAL ; then
|
||||
|
||||
Reference in New Issue
Block a user