mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-21 21:33:28 +01:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78e7434a85 | ||
|
|
d1d093b87d | ||
|
|
ea00709aa1 | ||
|
|
219a6463f3 | ||
|
|
0ed89369a9 | ||
|
|
6bd534258e | ||
|
|
48bea7aeee | ||
|
|
c3179d58ba | ||
|
|
fcafa1753d | ||
|
|
d74a516967 | ||
|
|
b0a5fafc4c | ||
|
|
9051ba559a |
19
CHANGELOG.md
19
CHANGELOG.md
@@ -1,3 +1,21 @@
|
||||
## 1.16 - 2019-06-16 - <dave at tiredofit dot ca>
|
||||
|
||||
* Check to see if Database Exists before performing backup
|
||||
* Fix for MysQL/MariaDB custom ports - Credit to <spumer@github>
|
||||
|
||||
## 1.15 - 2019-05-24 - <claudioaltamura @ github>
|
||||
|
||||
* Added abaility to backup password protected Redis Hosts
|
||||
|
||||
## 1.14 - 2019-04-20 - <dave at tiredofit dot ca>
|
||||
|
||||
* Switch to using locally built mongodb-tools from tiredofit/mongo-builder due to Alpine removing precompiled packages from repositories
|
||||
|
||||
## 1.13 - 2019-03-09 - <dave at tiredofit dot ca>
|
||||
|
||||
* Fixed Postgres backup without SPLIT_DB enabled (credit MelwinKfr@github)
|
||||
* Added DB_PORT reference to properly backup Postgres with non default ports (thanks Maxximus007@github)
|
||||
|
||||
## 1.12 - 2019-03-01 - <stevetodorov at github>
|
||||
|
||||
* Fix for XZ Compression failing
|
||||
@@ -55,3 +73,4 @@
|
||||
|
||||
* Initial Release
|
||||
* Alpine:Edge
|
||||
|
||||
|
||||
18
Dockerfile
18
Dockerfile
@@ -1,6 +1,11 @@
|
||||
FROM tiredofit/mongo-builder as mongo-packages
|
||||
|
||||
FROM tiredofit/alpine:edge
|
||||
LABEL maintainer="Dave Conroy (dave at tiredofit dot ca)"
|
||||
|
||||
### Copy Mongo Packages
|
||||
COPY --from=mongo-packages / /usr/src/apk
|
||||
|
||||
### Set Environment Variables
|
||||
ENV ENABLE_CRON=FALSE \
|
||||
ENABLE_SMTP=FALSE
|
||||
@@ -17,9 +22,9 @@ LABEL maintainer="Dave Conroy (dave at tiredofit dot ca)"
|
||||
xz-dev \
|
||||
&& \
|
||||
\
|
||||
apk add --virtual .db-backup-run-deps \
|
||||
apk add -t .db-backup-run-deps \
|
||||
bzip2 \
|
||||
mongodb-tools \
|
||||
influxdb \
|
||||
mariadb-client \
|
||||
libressl \
|
||||
pigz \
|
||||
@@ -29,10 +34,15 @@ LABEL maintainer="Dave Conroy (dave at tiredofit dot ca)"
|
||||
xz \
|
||||
&& \
|
||||
apk add \
|
||||
influxdb@testing \
|
||||
pixz@testing \
|
||||
&& \
|
||||
\
|
||||
## Locally Install Mongo Package
|
||||
cd /usr/src/apk && \
|
||||
apk add -t .db-backup-mongo-deps --allow-untrusted \
|
||||
mongodb-tools*.apk \
|
||||
&& \
|
||||
\
|
||||
cd /usr/src && \
|
||||
mkdir -p pbzip2 && \
|
||||
curl -ssL https://launchpad.net/pbzip2/1.1/1.1.13/+download/pbzip2-1.1.13.tar.gz | tar xvfz - --strip=1 -C /usr/src/pbzip2 && \
|
||||
@@ -40,7 +50,7 @@ LABEL maintainer="Dave Conroy (dave at tiredofit dot ca)"
|
||||
make && \
|
||||
make install && \
|
||||
\
|
||||
# Cleanup
|
||||
### Cleanup
|
||||
rm -rf /usr/src/* && \
|
||||
apk del .db-backup-build-deps && \
|
||||
rm -rf /tmp/* /var/cache/apk/*
|
||||
|
||||
17
README.md
17
README.md
@@ -1,4 +1,4 @@
|
||||
# tiredofit/db-backup
|
||||
# hub.docker.com/r/tiredofit/db-backup
|
||||
|
||||
|
||||
[](https://hub.docker.com/r/tiredofit/db-backup)
|
||||
@@ -23,7 +23,7 @@ Currently backs up CouchDB, InfluxDB, MySQL, MongoDB Postgres, Redis, Rethink se
|
||||
* select how often to run a dump
|
||||
* select when to start the first dump, whether time of day or relative to container start time
|
||||
|
||||
This Container uses Alpine:Edge as a base.
|
||||
* This Container uses a [customized Alpine Linux base](https://hub.docker.com/r/tiredofit/alpine) which includes [s6 overlay](https://github.com/just-containers/s6-overlay) enabled for PID 1 Init capabilities, [zabbix-agent](https://zabbix.org) based on `3.4` compiled for individual container monitoring, Cron also installed along with other tools (bash,curl, less, logrotate, nano, vim) for easier management. It also supports sending to external SMTP servers
|
||||
|
||||
[Changelog](CHANGELOG.md)
|
||||
|
||||
@@ -56,7 +56,7 @@ method of installation.
|
||||
|
||||
|
||||
```bash
|
||||
docker pull tiredofit/db-backup
|
||||
docker pull tiredofit/db-backup:latest
|
||||
```
|
||||
|
||||
# Quick Start
|
||||
@@ -86,23 +86,22 @@ Along with the Environment Variables from the [Base image](https://hub.docker.co
|
||||
|
||||
| Parameter | Description |
|
||||
|-----------|-------------|
|
||||
| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, or none `NONE` - Default `GZ`
|
||||
| `DB_TYPE` | Type of DB Server to backup `couch` `influx` `mysql` `pgsql` `mongo` `redis` `rethink`
|
||||
| `DB_SERVER` | Server Hostname e.g. `mariadb`
|
||||
| `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_DUMP_DEBUG` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed.
|
||||
| `DB_DUMP_TARGET` | Where to put the dump file, should be a directory. Supports three formats |
|
||||
| | Local If the value of `DB_DUMP_TARGET` starts with a `/` character, will dump to a local path, which should be volume-mounted.
|
||||
| `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.
|
||||
| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, or none `NONE` - Default `GZ`
|
||||
| `DEBUG_MODE` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed.
|
||||
| `MD5` | Generate MD5 Sum in Directory, `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` |
|
||||
| `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` |
|
||||
|
||||
|
||||
## Maintenance
|
||||
|
||||
@@ -84,6 +84,7 @@ fi
|
||||
"redis" | "REDIS" )
|
||||
DBTYPE=redis
|
||||
DBPORT=${DB_PORT:-6379}
|
||||
[[ ( -n "${DB_PASS}" ) ]] && REDIS_PASS_STR=" -a ${DBPASS}"
|
||||
;;
|
||||
"rethink" | "RETHINK" )
|
||||
DBTYPE=rethink
|
||||
@@ -104,13 +105,13 @@ function backup_couch() {
|
||||
|
||||
function backup_mysql() {
|
||||
if [ "$SPLIT_DB" = "TRUE" ] || [ "$SPLIT_DB" = "true" ]; then
|
||||
DATABASES=`mysql -h $DBHOST -u$DBUSER -p$DBPASS --batch -e "SHOW DATABASES;" | grep -v Database|grep -v schema`
|
||||
DATABASES=`mysql -h $DBHOST -P $DBPORT -u$DBUSER -p$DBPASS --batch -e "SHOW DATABASES;" | grep -v Database|grep -v schema`
|
||||
|
||||
for db in $DATABASES; do
|
||||
if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
|
||||
echo "** [db-backup] Dumping database: $db"
|
||||
TARGET=mysql_${db}_${DBHOST}_${now}.sql
|
||||
mysqldump --max-allowed-packet=512M -h $DBHOST -u$DBUSER ${MYSQL_PASS_STR} --databases $db > ${TMPDIR}/${TARGET}
|
||||
mysqldump --max-allowed-packet=512M -h $DBHOST -P $DBPORT -u$DBUSER ${MYSQL_PASS_STR} --databases $db > ${TMPDIR}/${TARGET}
|
||||
generate_md5
|
||||
compression
|
||||
move_backup
|
||||
@@ -146,7 +147,7 @@ function backup_mongo() {
|
||||
function backup_pgsql() {
|
||||
if [ "$SPLIT_DB" = "TRUE" ] || [ "$SPLIT_DB" = "true" ]; then
|
||||
export PGPASSWORD=${DBPASS}
|
||||
DATABASES=`psql -h $DBHOST -U $DBUSER -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
|
||||
echo "** [db-backup] Dumping database: $db"
|
||||
TARGET=pgsql_${db}_${DBHOST}_${now}.sql
|
||||
@@ -157,7 +158,7 @@ function backup_pgsql() {
|
||||
done
|
||||
else
|
||||
export PGPASSWORD=${DBPASS}
|
||||
pg_dump -h ${DBHOST} -U ${DBUSER} $db > ${TMPDIR}/${TARGET}
|
||||
pg_dump -h ${DBHOST} -U ${DBUSER} -p ${DBPORT} ${DBNAME} > ${TMPDIR}/${TARGET}
|
||||
generate_md5
|
||||
compression
|
||||
move_backup
|
||||
@@ -166,13 +167,13 @@ function backup_pgsql() {
|
||||
|
||||
function backup_redis() {
|
||||
TARGET=redis_${db}_${DBHOST}_${now}.rdb
|
||||
echo bgsave | redis-cli -h ${DBHOST} -p ${DBPORT} --rdb ${TMPDIR}/${TARGET}
|
||||
echo bgsave | redis-cli -h ${DBHOST} -p ${DBPORT} ${REDIS_PASS_STR} --rdb ${TMPDIR}/${TARGET}
|
||||
echo "** [db-backup] 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} | awk '/rdb_bgsave_in_progress:0/{print "saved"}')
|
||||
ok=$(echo 'info Persistence' | redis-cli -h ${DBHOST} -p ${DBPORT} | awk '/rdb_last_bgsave_status:ok/{print "ok"}')
|
||||
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
|
||||
echo "** [db-backup] Redis Backup Complete"
|
||||
fi
|
||||
@@ -192,6 +193,78 @@ function backup_rethink() {
|
||||
move_backup
|
||||
}
|
||||
|
||||
function check_availability() {
|
||||
### Set the Database Type
|
||||
case "$DBTYPE" in
|
||||
"couch" )
|
||||
COUNTER=0
|
||||
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] 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
|
||||
echo "** [db-backup] 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
|
||||
echo "** [db-backup] 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`"
|
||||
echo "$out" | grep -E "COUNT|Enter" 2>&1 > /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
:
|
||||
break
|
||||
fi
|
||||
echo "** [db-backup] MySQL/MariaDB Server "$DBHOST" is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
done
|
||||
;;
|
||||
"pgsql" )
|
||||
# Wait until mongo logs that it's ready (or timeout after 60s)
|
||||
COUNTER=0
|
||||
export PGPASSWORD=${DBPASS}
|
||||
until pg_isready --dbname=${DBNAME} --host=${DBHOST} --port=${DBPORT} --username=${DBUSER} -q
|
||||
do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] Postgres Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"redis" )
|
||||
COUNTER=0
|
||||
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] Redis Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"rethink" )
|
||||
COUNTER=0
|
||||
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] RethinkDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
function compression() {
|
||||
case "$COMPRESSION" in
|
||||
"GZ" | "gz" | "gzip" | "GZIP")
|
||||
@@ -257,24 +330,31 @@ echo '** [db-backup] Initialized at at '$(date)
|
||||
### Take a Dump
|
||||
case "$DBTYPE" in
|
||||
"couch" )
|
||||
check_availability
|
||||
backup_couch
|
||||
;;
|
||||
"influx" )
|
||||
check_availability
|
||||
backup_influx
|
||||
;;
|
||||
"mysql" )
|
||||
check_availability
|
||||
backup_mysql
|
||||
;;
|
||||
"mongo" )
|
||||
check_availability
|
||||
backup_mongo
|
||||
;;
|
||||
"pgsql" )
|
||||
check_availability
|
||||
backup_pgsql
|
||||
;;
|
||||
"redis" )
|
||||
check_availability
|
||||
backup_redis
|
||||
;;
|
||||
"rethink" )
|
||||
check_availability
|
||||
backup_rethink
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user