Skip to content

Commit 537367c

Browse files
authored
Added unit tests (#1)
* Added Unit tests * Updated travis config * Added empty database test case * Added badge to README.md, added sys to SKIP_DATABASES * Added views skip databases
1 parent 91e8bc1 commit 537367c

File tree

15 files changed

+378
-21
lines changed

15 files changed

+378
-21
lines changed

.env-example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ MYSQL_USER=root
55
MYSQL_PASS=toor
66

77
# (Optional, use recommended example below ) Databases to skip with no space between the args separated by a ,
8-
SKIP_DATABASES=mysql,information_schema,performance_schema,phpmyadmin
8+
SKIP_DATABASES=mysql,sys,information_schema,performance_schema,phpmyadmin
99

1010
# (Optional) No not use if you are a goat
1111
# EXPERT_ARGS=--default-character-set=utf8 --extended-insert=FALSE --single-transaction --skip-comments --skip-dump-date --hex-blob --tz-utc

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.env
2-
*.sql
2+
*.sql
3+
shunit*
4+
!samples/**/*

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: bash
2+
cache: bundler
3+
dependencies:
4+
cache_directories:
5+
- "~/.apt-cache"
6+
pre:
7+
- sudo rm -rf /var/cache/apt/archives && sudo ln -s ~/.apt-cache /var/cache/apt/archives && mkdir -p ~/.apt-cache/partial
8+
services:
9+
- mysql
10+
addons:
11+
apt:
12+
sources:
13+
- mysql-5.7-trusty
14+
packages:
15+
- mysql-server
16+
- mysql-client
17+
before_script:
18+
- curl -L "http://downloads.sourceforge.net/shunit2/shunit2-2.0.3.tgz" | tar zx
19+
- chmod +x ./shunit2-2.0.3/src/shell/shunit2
20+
script:
21+
- bash tests.sh
22+
before_install:
23+
- sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('testbench'), host='%', password_last_changed=FROM_UNIXTIME(1523829600) where User='root'; update user set plugin='mysql_native_password'; delete from user where User != 'root' OR host != '%'; delete from user where User = 'sys'; FLUSH PRIVILEGES;"
24+
- sudo service mysql restart

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- Added optional `BACKUP_CONFIG_ENVFILE` variable to set the env file location
1212
- Updated .env-example (commented out some optional variables)
1313
- Added check if `BACKUP_DIR` exists
14+
- Added exit codes
15+
- Added unit tests
16+
- Updated .gitignore
17+
- Bug fix (no views)
18+
19+
### Will be done
20+
- Skipping users & grants
21+
1422

1523
## [1.0.0] - 2018-04-07
1624
### Added

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# sql-backup
22
[![Maintainability](https://api.codeclimate.com/v1/badges/9af0b964df176436608d/maintainability)](https://codeclimate.com/github/williamdes/sql-backup/maintainability)
33

4+
[![Build Status](https://travis-ci.org/williamdes/sql-backup.svg?branch=master)](https://travis-ci.org/williamdes/sql-backup)
5+
46
Backup your MySQL server ( data, users, grants, views, triggers, routines, events )
57

68
## Install
@@ -64,7 +66,7 @@ BACKUP_DIR=/sql_backup
6466
MYSQL_HOST=localhost
6567
MYSQL_USER=root
6668
MYSQL_PASS=root
67-
SKIP_DATABASES=mysql,information_schema,performance_schema,phpmyadmin
69+
SKIP_DATABASES=mysql,sys,information_schema,performance_schema,phpmyadmin
6870
```
6971

7072
## Files

backup.sh

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#!/bin/bash
22

3+
exitWithMsg() {
4+
echo $2
5+
exit $1
6+
}
7+
38
# from : github:builtinnya/dotenv-shell-loader
49
DOTENV_SHELL_LOADER_SAVED_OPTS=$(set +o)
510
set -o allexport
611
if [ ! -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"
1822
unset DOTENV_SHELL_LOADER_SAVED_OPTS
1923

2024
if ! [ -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
2326
fi
2427

2528
if ! [ -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
2830
fi
2931

3032
if [ -z "${BACKUP_DIR}" ]; then
31-
echo "Empty Variable BACKUP_DIR"
32-
exit 1
33+
exitWithMsg 200 "Empty Variable BACKUP_DIR"
3334
else
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
3838
fi
3939

4040
if [ -z "${MYSQL_HOST}" ]; then
41-
echo "Empty Variable MYSQL_HOST"
42-
exit 1
41+
exitWithMsg 200 "Empty Variable MYSQL_HOST"
4342
fi
4443

4544
if [ -z "${MYSQL_USER}" ]; then
46-
echo "Empty Variable MYSQL_USER"
47-
exit 1
45+
exitWithMsg 200 "Empty Variable MYSQL_USER"
4846
fi
4947

5048

@@ -77,14 +75,41 @@ fi
7775
# Get result
7876
DB_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+
8086
for DB in $DB_LIST; do # Concat ignore command
8187
DBS="${DBS} ${DB}"
8288
done
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
86107
VIEWS_LIST=`mysql ${MYSQL_CONN} -ANe"${VIEW_LIST_SQL}"`
87108

109+
if [ "$?" -ne 0 ]; then
110+
exitWithMsg 204 "Views listing failed"
111+
fi
112+
88113
VIEW_IGNORE_ARG=""
89114
# Split on :!
90115
VIEWS=$(echo -e "${VIEWS_LIST}" | tr ":!" "\n")
@@ -99,21 +124,38 @@ VIEW_IGNORE_ARG=${VIEW_IGNORE_ARG//\`/}
99124
echo "Structure..."
100125
mysqldump ${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+
102131
echo "Data ..."
103132
mysqldump ${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

108138
echo "Routines ..."
109139
mysqldump ${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+
111145
echo "Triggers ..."
112146
mysqldump ${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+
114152
echo "Events ..."
115153
mysqldump ${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+
117159
echo "Views ..."
118160
VIEWS_SHOW_SQL=""
119161
for VIEW in $VIEWS; do # Concat SHOW CREATE VIEW command
@@ -122,6 +164,10 @@ done
122164
# echo -e "${VIEWS_SHOW_SQL}"
123165
echo ${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
126172
sed -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 ;
136182
sed -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+
138191
echo "Grants ..."
139192
# Needs refactor
140193
GRANTS_SQL="select distinct concat( \"SHOW GRANTS FOR '\",user,\"'@'\",host,\"';\" ) from mysql.user WHERE user != 'root';"
141194
GRANTS_LIST=`mysql ${MYSQL_CONN} -ANe"${GRANTS_SQL}"`
142195
echo ${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 > \\
144202
sed -i -e 's/\\\\//g' ${BACKUP_DIR}/grants.sql
145203
# echo -e ${GRANTS_SQL}
146204

147205
echo "Backup done !"
206+
148207
if [ ! -z "${ON_SUCCESS}" ]; then
149-
`echo ${ON_SUCCESS}`
208+
`echo ${ON_SUCCESS}`
150209
fi
210+
exit 0

samples/empty/database.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
3+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
4+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
5+
/*!40101 SET NAMES utf8 */;
6+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
7+
/*!40103 SET TIME_ZONE='+00:00' */;
8+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
9+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
10+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
11+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
12+
13+
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `testbench` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
14+
15+
USE `testbench`;
16+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
17+
18+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
19+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
20+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
21+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
22+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
23+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
24+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
25+

samples/empty/events.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
3+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
4+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
5+
/*!40101 SET NAMES utf8 */;
6+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
7+
/*!40103 SET TIME_ZONE='+00:00' */;
8+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
9+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
10+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
11+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
12+
13+
USE `testbench`;
14+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
15+
16+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
17+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
18+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
19+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
20+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
21+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
22+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
23+

samples/empty/grants.sql

Whitespace-only changes.

samples/empty/routines.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
3+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
4+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
5+
/*!40101 SET NAMES utf8 */;
6+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
7+
/*!40103 SET TIME_ZONE='+00:00' */;
8+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
9+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
10+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
11+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
12+
13+
USE `testbench`;
14+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
15+
16+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
17+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
18+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
19+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
20+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
21+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
22+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
23+

0 commit comments

Comments
 (0)