Skip to content

Commit e0cd727

Browse files
committed
♻️ Rework dump that is dumps one database per time, this allows dump to fail per DB and not break whole process. Also add support for $MYSQL_IGNORE_DBS ENV variable, which can exclude problematic databases from export
Related: #44808
1 parent 2436261 commit e0cd727

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

entrypoint.sh

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/bash
2-
set -eo pipefail
32

43
backup_tool="/google-cloud-sdk/bin/gsutil"
54
backup_options="-m rsync -r"
@@ -15,7 +14,7 @@ FORCE=""
1514
if [ ! -z "$MYSQL_DUMP_FORCE" ]; then
1615
FORCE="--force"
1716
else
18-
set -eo pipefail # if we do not force, we want clean exit codes on mysqldump command
17+
set -euo pipefail # if we do not force, we want clean exit codes on mysqldump command
1918
fi
2019

2120
if [ ! -z "$GOOGLE_APPLICATION_CREDENTIALS" ]; then
@@ -25,12 +24,17 @@ fi
2524
mkdir -p /tmp/backup/
2625
rm -rf -- /tmp/backup/*
2726

28-
candidates=$(echo "show databases" | mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -h "$MYSQL_HOST" -P "$MYSQL_PORT" | grep -Ev "^(Database|sys|performance_schema|information_schema)$")
29-
30-
mysqldump -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" --databases $candidates --single-transaction -h "$MYSQL_HOST" -P "$MYSQL_PORT" --result-file=/tmp/backup/dump.sql --verbose $FORCE
31-
echo $?
27+
for i in `echo "show databases" | mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -h "$MYSQL_HOST" -P "$MYSQL_PORT" | grep -Ev "^(Database|sys|performance_schema|information_schema|$MYSQL_IGNORE_DBS)$"`; do
28+
echo $i
29+
mysqldump -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" --databases $i --single-transaction -h "$MYSQL_HOST" -P "$MYSQL_PORT" --result-file=/tmp/backup/${i}_dump.sql --verbose $FORCE || echo "${i} database backup has failed!" >> /tmp/fails
30+
gzip -v /tmp/backup/${i}_dump.sql || true
31+
done
3232

3333
echo "export done, now gzip output and transfer it to GCS"
3434

35-
gzip -v /tmp/backup/dump.sql
3635
$backup_tool $backup_options /tmp/backup/ gs://$GS_URL/
36+
37+
if [ -f /tmp/fails ]; then
38+
cat /tmp/fails
39+
exit 1
40+
fi

0 commit comments

Comments
 (0)