11#! /bin/bash
22
3+ exitWithMsg () {
4+ echo $2
5+ exit $1
6+ }
7+
38# from : github:builtinnya/dotenv-shell-loader
49DOTENV_SHELL_LOADER_SAVED_OPTS=$( set +o)
510set -o allexport
611if [ ! -z " ${BACKUP_CONFIG_ENVFILE} " ]; then
712 if [ ! -f " ${BACKUP_CONFIG_ENVFILE} " ]; then
8- echo " Value of variable BACKUP_CONFIG_ENVFILE is not a file (${BACKUP_CONFIG_ENVFILE} )"
9- exit 1
13+ exitWithMsg 201 " Value of variable BACKUP_CONFIG_ENVFILE is not a file (${BACKUP_CONFIG_ENVFILE} )"
1014 else
1115 [ -f " ${BACKUP_CONFIG_ENVFILE} " ] && source " ${BACKUP_CONFIG_ENVFILE} "
1216 fi
@@ -18,33 +22,27 @@ eval "$DOTENV_SHELL_LOADER_SAVED_OPTS"
1822unset DOTENV_SHELL_LOADER_SAVED_OPTS
1923
2024if ! [ -x " $( command -v mysql) " ]; then
21- echo ' Error: mysql is not installed !, apt-get install -y mysql-client' >&2
22- exit 1
25+ exitWithMsg 203 ' Error: mysql is not installed !, apt-get install -y mysql-client' >&2
2326fi
2427
2528if ! [ -x " $( command -v mysqldump) " ]; then
26- echo ' Error: mysqldump is not installed !, apt-get install -y mysql-client' >&2
27- exit 1
29+ exitWithMsg 203 ' Error: mysqldump is not installed !, apt-get install -y mysql-client' >&2
2830fi
2931
3032if [ -z " ${BACKUP_DIR} " ]; then
31- echo " Empty Variable BACKUP_DIR"
32- exit 1
33+ exitWithMsg 200 " Empty Variable BACKUP_DIR"
3334else
3435 if [ ! -d " ${BACKUP_DIR} " ]; then
35- echo " Value of variable BACKUP_DIR is not a directory (${BACKUP_DIR} )"
36- exit 1
36+ exitWithMsg 202 " Value of variable BACKUP_DIR is not a directory (${BACKUP_DIR} )"
3737 fi
3838fi
3939
4040if [ -z " ${MYSQL_HOST} " ]; then
41- echo " Empty Variable MYSQL_HOST"
42- exit 1
41+ exitWithMsg 200 " Empty Variable MYSQL_HOST"
4342fi
4443
4544if [ -z " ${MYSQL_USER} " ]; then
46- echo " Empty Variable MYSQL_USER"
47- exit 1
45+ exitWithMsg 200 " Empty Variable MYSQL_USER"
4846fi
4947
5048
7775# Get result
7876DB_LIST=` mysql ${MYSQL_CONN} -ANe" ${DB_LIST_SQL} " `
7977
78+ if [ " $? " -ne 0 ]; then
79+ exitWithMsg 204 " Databases listing failed"
80+ fi
81+
82+ if [ -z " ${DB_LIST} " ]; then
83+ exitWithMsg 206 " No databases to backup"
84+ fi
85+
8086for DB in $DB_LIST ; do # Concat ignore command
8187 DBS=" ${DBS} ${DB} "
8288done
8389
84- VIEW_LIST_SQL=" SET SESSION group_concat_max_len = 1000000;SELECT GROUP_CONCAT(concat(':!\` ',table_schema,'\` .\` ',table_name,'\` ') SEPARATOR '') FROM information_schema.views"
90+ VIEW_LIST_SQL=" SET SESSION group_concat_max_len = 1000000;SELECT IFNULL(GROUP_CONCAT(concat(':!\` ',table_schema,'\` .\` ',table_name,'\` ') SEPARATOR ''),'') FROM information_schema.views"
91+
92+ # If ${SKIP_DATABASES} is not empty, create a where chain
93+ if [ ! -z " ${SKIP_DATABASES} " ]; then
94+ VIEW_LIST_SQL=" ${VIEW_LIST_SQL} WHERE table_schema NOT IN ("
95+ # Split on ,
96+ SKIP_DATABASES=$( echo -e " ${SKIP_DATABASES} " | tr " ," " \n" )
97+ for DB in ${SKIP_DATABASES} ; do
98+ VIEW_LIST_SQL=" ${VIEW_LIST_SQL} '${DB} '," ;
99+ done
100+ VIEW_LIST_SQL=" ${VIEW_LIST_SQL: : -1} "
101+ VIEW_LIST_SQL=" ${VIEW_LIST_SQL} );"
102+ else
103+ VIEW_LIST_SQL=" ;"
104+ fi
105+
85106# Get result
86107VIEWS_LIST=` mysql ${MYSQL_CONN} -ANe" ${VIEW_LIST_SQL} " `
87108
109+ if [ " $? " -ne 0 ]; then
110+ exitWithMsg 204 " Views listing failed"
111+ fi
112+
88113VIEW_IGNORE_ARG=" "
89114# Split on :!
90115VIEWS=$( echo -e " ${VIEWS_LIST} " | tr " :!" " \n" )
@@ -99,21 +124,38 @@ VIEW_IGNORE_ARG=${VIEW_IGNORE_ARG//\`/}
99124echo " Structure..."
100125mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=FALSE --events=FALSE --no-data ${VIEW_IGNORE_ARG} --databases ${DBS} > ${BACKUP_DIR} /structure.sql
101126
127+ if [ " $? " -ne 0 ]; then
128+ exitWithMsg 205 " Structure dump failed"
129+ fi
130+
102131echo " Data ..."
103132mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=FALSE --events=FALSE --no-create-info ${VIEW_IGNORE_ARG} --databases ${DBS} > ${BACKUP_DIR} /database.sql
104133
105- echo " Users ..."
106- mysqldump ${MYSQLDUMP_DEFAULTS} mysql --no-create-info --complete-insert --tables user db > ${BACKUP_DIR} /users.sql
134+ if [ " $? " -ne 0 ]; then
135+ exitWithMsg 205 " Data dump failed"
136+ fi
107137
108138echo " Routines ..."
109139mysqldump ${MYSQLDUMP_DEFAULTS} --routines=TRUE --triggers=FALSE --events=FALSE --no-create-info --no-data --no-create-db --databases ${DBS} > ${BACKUP_DIR} /routines.sql
110140
141+ if [ " $? " -ne 0 ]; then
142+ exitWithMsg 205 " Routines dump failed"
143+ fi
144+
111145echo " Triggers ..."
112146mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=TRUE --events=FALSE --no-create-info --no-data --no-create-db --databases ${DBS} > ${BACKUP_DIR} /triggers.sql
113147
148+ if [ " $? " -ne 0 ]; then
149+ exitWithMsg 205 " Triggers dump failed"
150+ fi
151+
114152echo " Events ..."
115153mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=FALSE --events=TRUE --no-create-info --no-data --no-create-db --databases ${DBS} > ${BACKUP_DIR} /events.sql
116154
155+ if [ " $? " -ne 0 ]; then
156+ exitWithMsg 205 " Events dump failed"
157+ fi
158+
117159echo " Views ..."
118160VIEWS_SHOW_SQL=" "
119161for VIEW in $VIEWS ; do # Concat SHOW CREATE VIEW command
@@ -122,6 +164,10 @@ done
122164# echo -e "${VIEWS_SHOW_SQL}"
123165echo ${VIEWS_SHOW_SQL} | sed ' s/;/\\G/g' | mysql ${MYSQL_CONN} > ${BACKUP_DIR} /views.sql
124166
167+ if [ " $? " -ne 0 ]; then
168+ exitWithMsg 205 " Views dump failed"
169+ fi
170+
125171# Keeps lines starting with Create
126172sed -i ' /Create/!d' ${BACKUP_DIR} /views.sql
127173# Removes 'Create View'
@@ -135,16 +181,30 @@ sed -i 's/$/;/' ${BACKUP_DIR}/views.sql
135181# Replace double ;; by ;
136182sed -i ' s/;;/;/' ${BACKUP_DIR} /views.sql
137183
184+ echo " Users ..."
185+ mysqldump ${MYSQLDUMP_DEFAULTS} mysql --no-create-info --complete-insert --tables user db > ${BACKUP_DIR} /users.sql
186+
187+ if [ " $? " -ne 0 ]; then
188+ exitWithMsg 205 " Users dump failed"
189+ fi
190+
138191echo " Grants ..."
139192# Needs refactor
140193GRANTS_SQL=" select distinct concat( \" SHOW GRANTS FOR '\" ,user,\" '@'\" ,host,\" ';\" ) from mysql.user WHERE user != 'root';"
141194GRANTS_LIST=` mysql ${MYSQL_CONN} -ANe" ${GRANTS_SQL} " `
142195echo ${GRANTS_LIST} | mysql --default-character-set=utf8 --skip-comments ${MYSQL_CONN} | sed ' s/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 --/;/--/{x;p;x;}' > ${BACKUP_DIR} /grants.sql
196+
197+ if [ " $? " -ne 0 ]; then
198+ exitWithMsg 205 " Grants dump failed"
199+ fi
200+
143201# Removes double backslashes > \\
144202sed -i -e ' s/\\\\//g' ${BACKUP_DIR} /grants.sql
145203# echo -e ${GRANTS_SQL}
146204
147205echo " Backup done !"
206+
148207if [ ! -z " ${ON_SUCCESS} " ]; then
149- ` echo ${ON_SUCCESS} `
208+ ` echo ${ON_SUCCESS} `
150209fi
210+ exit 0
0 commit comments