-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_backup.sh
executable file
·99 lines (90 loc) · 2.87 KB
/
database_backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
dumpDatabaseSchema() { # db_name, db_username, db_password, dir_path
local db_name=$1
local db_username=$2
local db_password=$3
local dir_path=$4
local db_schema_path="${dir_path}/database_${db_name}_schema.sql"
local res_status=1
local res1_str=$(mysqldump -u "${db_username}" -p"${db_password}" "${db_name}" --no-data --skip-add-drop-table 1> "${db_schema_path}" 2>/dev/null)
local res1_status=$?
local res2_str=$(sed -i 's/CREATE TABLE/CREATE TABLE IF NOT EXISTS/g' "${db_schema_path}")
local res2_status=$?
local res3_str=$(sed -i 's/ AUTO_INCREMENT=[0-9]*//g' "${db_schema_path}")
local res3_status=$?
if [ $res1_status -eq 0 ] && [ $res2_status -eq 0 ] && [ $res3_status -eq 0 ]; then
res_status=0
fi
echo -n "${res_status}"
}
dumpDatabaseTables() { # db_name, db_username, db_password, dir_path
local db_name=$1
local db_username=$2
local db_password=$3
local dir_path=$4
local res_status=1
local res1_str=$(mysql -u "${db_username}" -p"${db_password}" -N -B -e "SHOW TABLES FROM ${db_name};" 2>/dev/null)
local res1_status=$?
local res2_str=""
local res2_status=0
if [ $res1_status -eq 0 ]; then
local tables=$res1_str;
for table in $tables;
do
local table_dump_path="${dir_path}/table_${table}.sql"
local res2_str=$(mysqldump -u "${db_username}" -p"${db_password}" "${db_name}" "${table}" --no-create-info 1> "${table_dump_path}" 2>/dev/null)
local res2_status=$?
if [ $res2_status -ne 0 ]; then
break;
fi
done;
fi
if [ $res1_status -eq 0 ] && [ $res2_status -eq 0 ]; then
res_status=0
fi
echo -n "${res_status}"
}
# parse Args
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--db-name)
dbName="$2"
shift # past argument
shift # past value
;;
--db-username)
dbUsername="$2"
shift # past argument
shift # past value
;;
--db-password)
dbPassword="$2"
shift # past argument
shift # past value
;;
--dir-path)
dirPath="$2"
shift # past argument
shift # past value
;;
-*|--*)
echo "Unknown option $1"
action=""
break
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
#
exit_code=0
res_db=$(dumpDatabaseSchema "${dbName}" "${dbUsername}" "${dbPassword}" "${dirPath}")
res_tbs=$(dumpDatabaseTables "${dbName}" "${dbUsername}" "${dbPassword}" "${dirPath}")
if [ $res_db -ne 0 ] || [ $res_tbs -ne 0 ]; then
exit_code=1
fi
exit $exit_code