89c83f02fedca7aea893155972deab76c17b12c7
[osm/RO.git] / RO / osm_ro / database_utils / dump_db.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24
25 LICENSE_HEAD='/**
26 * Copyright 2017 Telefonica Investigacion y Desarrollo, S.A.U.
27 * This file is part of openmano
28 * All Rights Reserved.
29 *
30 * Licensed under the Apache License, Version 2.0 (the "License"); you may
31 * not use this file except in compliance with the License. You may obtain
32 * a copy of the License at
33 *
34 * http://www.apache.org/licenses/LICENSE-2.0
35 *
36 * Unless required by applicable law or agreed to in writing, software
37 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
38 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
39 * License for the specific language governing permissions and limitations
40 * under the License.
41 *
42 * For those usages not covered by the Apache License, Version 2.0 please
43 * contact with: nfvlabs@tid.es
44 **/
45 '
46
47 DBUSER="mano"
48 DBPASS=""
49 DBHOST="localhost"
50 DBPORT="3306"
51 DBNAME="mano_db"
52
53 # Detect paths
54 MYSQL=$(which mysql)
55 AWK=$(which awk)
56 GREP=$(which grep)
57 DIRNAME=`dirname $(readlink -f $0)`
58
59 function usage(){
60 echo -e "Usage: $0 OPTIONS"
61 echo -e " Dumps openmano database content"
62 echo -e " OPTIONS"
63 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
64 echo -e " -p PASS database password. 'No password' by default. Prompts if DB access fails"
65 echo -e " -P PORT database port. '$DBPORT' by default"
66 echo -e " -h HOST database host. '$DBHOST' by default"
67 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
68 echo -e " --help shows this help"
69 }
70
71 while getopts ":u:p:P:h:-:" o; do
72 case "${o}" in
73 u)
74 DBUSER="$OPTARG"
75 ;;
76 p)
77 DBPASS="$OPTARG"
78 ;;
79 P)
80 DBPORT="$OPTARG"
81 ;;
82 d)
83 DBNAME="$OPTARG"
84 ;;
85 h)
86 DBHOST="$OPTARG"
87 ;;
88 -)
89 [ "${OPTARG}" == "help" ] && usage && exit 0
90 echo "Invalid option: --$OPTARG" >&2 && usage >&2
91 exit 1
92 ;;
93 \?)
94 echo "Invalid option: -$OPTARG" >&2 && usage >&2
95 exit 1
96 ;;
97 :)
98 echo "Option -$OPTARG requires an argument." >&2 && usage >&2
99 exit 1
100 ;;
101 *)
102 usage >&2
103 exit -1
104 ;;
105 esac
106 done
107 shift $((OPTIND-1))
108
109 #check and ask for database user password
110 DBUSER_="-u$DBUSER"
111 DBPASS_=""
112 [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
113 DBHOST_="-h$DBHOST"
114 DBPORT_="-P$DBPORT"
115 while ! echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME >/dev/null 2>&1
116 do
117 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
118 [ -z "$logintry" ] && echo -e "\nProvide database name and credentials"
119 read -e -p "mysql database name($DBNAME): " KK
120 [ -n "$KK" ] && DBNAME="$KK"
121 read -e -p "mysql user($DBUSER): " KK
122 [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER"
123 read -e -s -p "mysql password: " DBPASS
124 [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
125 [ -z "$DBPASS" ] && DBPASS_=""
126 logintry="yes"
127 echo
128 done
129
130
131 #echo structure, including the content of schema_version
132 echo "$LICENSE_HEAD" > ${DIRNAME}/${DBNAME}_structure.sql
133 mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-data --add-drop-table --add-drop-database --routines --databases $DBNAME >> ${DIRNAME}/${DBNAME}_structure.sql
134 echo -e "\n\n\n\n" >> ${DIRNAME}/${DBNAME}_structure.sql
135 mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-create-info $DBNAME --tables schema_version 2>/dev/null >> ${DIRNAME}/${DBNAME}_structure.sql
136 echo " ${DIRNAME}/${DBNAME}_structure.sql"
137
138 #echo only data
139 echo "$LICENSE_HEAD" > ${DIRNAME}/${DBNAME}_data.sql #copy my own header
140 mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-create-info $DBNAME >> ${DIRNAME}/${DBNAME}_data.sql
141 echo " ${DIRNAME}/${DBNAME}_data.sql"
142
143 #echo all
144 echo "$LICENSE_HEAD" > ${DIRNAME}/${DBNAME}_all.sql #copy my own header
145 mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --add-drop-table --add-drop-database --routines --databases $DBNAME >> ${DIRNAME}/${DBNAME}_all.sql
146 echo " ${DIRNAME}/${DBNAME}_all.sql"
147