Skip to content

Commit 78e7434

Browse files
committed
Check for host availability before backup
1 parent d1d093b commit 78e7434

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.16 - 2019-06-16 - <dave at tiredofit dot ca>
2+
3+
* Check to see if Database Exists before performing backup
4+
* Fix for MysQL/MariaDB custom ports - Credit to <spumer@github>
5+
16
## 1.15 - 2019-05-24 - <claudioaltamura @ github>
27

38
* Added abaility to backup password protected Redis Hosts

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ COPY --from=mongo-packages / /usr/src/apk
3636
apk add \
3737
pixz@testing \
3838
&& \
39-
39+
\
4040
## Locally Install Mongo Package
4141
cd /usr/src/apk && \
4242
apk add -t .db-backup-mongo-deps --allow-untrusted \

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# tiredofit/db-backup
1+
# hub.docker.com/r/tiredofit/db-backup
22

33

44
[![Build Status](https://img.shields.io/docker/build/tiredofit/db-backup.svg)](https://hub.docker.com/r/tiredofit/db-backup)
@@ -23,7 +23,7 @@ Currently backs up CouchDB, InfluxDB, MySQL, MongoDB Postgres, Redis, Rethink se
2323
* select how often to run a dump
2424
* select when to start the first dump, whether time of day or relative to container start time
2525

26-
This Container uses Alpine:Edge as a base.
26+
* 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
2727

2828
[Changelog](CHANGELOG.md)
2929

@@ -56,7 +56,7 @@ method of installation.
5656

5757

5858
```bash
59-
docker pull tiredofit/db-backup
59+
docker pull tiredofit/db-backup:latest
6060
```
6161

6262
# Quick Start
@@ -92,6 +92,7 @@ Along with the Environment Variables from the [Base image](https://hub.docker.co
9292
| `DB_NAME` | Schema Name e.g. `database`
9393
| `DB_USER` | username for the database - use `root` to backup all MySQL of them.
9494
| `DB_PASS` | (optional if DB doesn't require it) password for the database
95+
| `DB_PORT` | (optional) Set port to connect to DB_HOST. Defaults are provided
9596
| `DB_DUMP_FREQ` | How often to do a dump, in minutes. Defaults to 1440 minutes, or once per day.
9697
| `DB_DUMP_BEGIN` | What time to do the first dump. Defaults to immediate. Must be in one of two formats
9798
| | Absolute HHMM, e.g. `2330` or `0415`

install/etc/s6/services/10-db-backup/run

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,78 @@ function backup_rethink() {
193193
move_backup
194194
}
195195

196+
function check_availability() {
197+
### Set the Database Type
198+
case "$DBTYPE" in
199+
"couch" )
200+
COUNTER=0
201+
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
202+
sleep 5
203+
let COUNTER+=5
204+
echo "** [db-backup] CouchDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
205+
done
206+
;;
207+
"influx" )
208+
COUNTER=0
209+
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
210+
sleep 5
211+
let COUNTER+=5
212+
echo "** [db-backup] InfluxDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
213+
done
214+
;;
215+
"mongo" )
216+
COUNTER=0
217+
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
218+
sleep 5
219+
let COUNTER+=5
220+
echo "** [db-backup] Mongo Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
221+
done
222+
;;
223+
"mysql" )
224+
COUNTER=0
225+
while true; do
226+
mysqlcmd='mysql -u'${DBUSER}' -P '${DBPORT}' -h '${DBHOST}' -p'${DBPASS}
227+
out="`$mysqlcmd -e "SELECT COUNT(*) FROM information_schema.FILES;" 2>&1`"
228+
echo "$out" | grep -E "COUNT|Enter" 2>&1 > /dev/null
229+
if [ $? -eq 0 ]; then
230+
:
231+
break
232+
fi
233+
echo "** [db-backup] MySQL/MariaDB Server "$DBHOST" is not accessible, retrying.. ($COUNTER seconds so far)"
234+
sleep 5
235+
let COUNTER+=5
236+
done
237+
;;
238+
"pgsql" )
239+
# Wait until mongo logs that it's ready (or timeout after 60s)
240+
COUNTER=0
241+
export PGPASSWORD=${DBPASS}
242+
until pg_isready --dbname=${DBNAME} --host=${DBHOST} --port=${DBPORT} --username=${DBUSER} -q
243+
do
244+
sleep 5
245+
let COUNTER+=5
246+
echo "** [db-backup] Postgres Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
247+
done
248+
;;
249+
"redis" )
250+
COUNTER=0
251+
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
252+
sleep 5
253+
let COUNTER+=5
254+
echo "** [db-backup] Redis Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
255+
done
256+
;;
257+
"rethink" )
258+
COUNTER=0
259+
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
260+
sleep 5
261+
let COUNTER+=5
262+
echo "** [db-backup] RethinkDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
263+
done
264+
;;
265+
esac
266+
}
267+
196268
function compression() {
197269
case "$COMPRESSION" in
198270
"GZ" | "gz" | "gzip" | "GZIP")
@@ -258,24 +330,31 @@ echo '** [db-backup] Initialized at at '$(date)
258330
### Take a Dump
259331
case "$DBTYPE" in
260332
"couch" )
333+
check_availability
261334
backup_couch
262335
;;
263336
"influx" )
337+
check_availability
264338
backup_influx
265339
;;
266340
"mysql" )
341+
check_availability
267342
backup_mysql
268343
;;
269344
"mongo" )
345+
check_availability
270346
backup_mongo
271347
;;
272348
"pgsql" )
349+
check_availability
273350
backup_pgsql
274351
;;
275352
"redis" )
353+
check_availability
276354
backup_redis
277355
;;
278356
"rethink" )
357+
check_availability
279358
backup_rethink
280359
;;
281360
esac

0 commit comments

Comments
 (0)