-
Notifications
You must be signed in to change notification settings - Fork 5
/
dbdump
executable file
·90 lines (75 loc) · 2.54 KB
/
dbdump
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
#!/bin/bash
###########################################################
# A script to find, dump, and gzip a database for the #
# current project. #
# Current supported project types: #
# * Symfony 1.x #
############################################################
#-- Exit codes
ERR_NO_CONFIG=1
ERR_DB_EXPORT=2
#-- Global variables
ORIG_PATH=$PWD
SYMFONY1_DB_FILE="config/databases.yml"
SYMFONY2_DB_FILE="app/config/parameters.yml"
DATABASE_CONFIGS="$SYMFONY1_DB_FILE $SYMFONY2_DB_FILE"
function dump_database {
local dst="$1"
mysqldump -h ${HOST} --opt -u ${USERNAME} -p${PASSWORD} --databases ${DBNAME} | gzip > "$dst"
return $ret
}
function find_dir_for_file {
local search="$1"
while [ ! -f "${search}" -a "/" != "$PWD" ]; do
cd ..
done
if [ -f "${search}" ]; then
echo "$PWD"
return 0
fi
return 1
}
function parse_symfony_1 {
local db_config="$1"
DBNAME=`sed -ne"s/^[^#].*dbname=\([^;' ]*\).*/\1/p" ${db_config} | head -n1`
USERNAME=`sed -ne"s/^[^#].*username:[' ]*\([^;' ]*\).*/\1/p" ${db_config} | head -n1`
PASSWORD=`sed -ne"s/^[^#].*password:[' ]*\([^;' ]*\).*/\1/p" ${db_config} | head -n1`
HOST=`sed -ne"s/^[^#].*host=\([^;'\'' ]*\).*/\1/p" ${db_config} | head -n1`
}
function parse_symfony_2 {
local db_config="$1"
DBNAME=`sed -ne"s/^[^#].*database_name:[' ]*\([^' ]*\).*/\1/p" ${db_config} | head -n1`
USERNAME=`sed -ne"s/^[^#].*database_user:[' ]*\([^' ]*\).*/\1/p" ${db_config} | head -n1`
PASSWORD=`sed -ne"s/^[^#].*database_password:[' ]*\([^' ]*\).*/\1/p" ${db_config} | head -n1`
HOST=`sed -ne"s/^[^#].*database_host:[' ]\([^' ]*\).*/\1/p" ${db_config} | head -n1`
}
#----------#
#-- MAIN --#
#----------#
DATE=`date '+%Y-%m-%d-%H%M'`
#-- Find the db file
for f in $DATABASE_CONFIGS; do
project_dir=$(find_dir_for_file "$f")
if [ "0" == "$?" ]; then
db_config="$project_dir/$f"
break;
fi
done
if [ -z "$db_config" ]; then
echo "Could not find a database config file."
exit $ERR_NO_CONFIG
fi
if [ "$db_config" != "${db_config/${SYMFONY1_DB_FILE}/}" ]; then
parse_symfony_1 "$db_config"
database_dst="${project_dir}/data/${DBNAME}-${DATE}.sql.gz"
elif [ "$db_config" != "${db_config/${SYMFONY2_DB_FILE}/}" ]; then
parse_symfony_2 "$db_config"
database_dst="${project_dir}/app/Resources/${DBNAME}-${DATE}.sql.gz"
fi
dump_database "$database_dst"
ret=$?
if [ 0 -ne $ret ]; then
echo "Error exporting Database."
exit $ERR_DB_EXPORT
fi
echo "Database backed up to: ${database_dst}"