blob: 1a7a4b6d5cf69304885eda50eb6ada391a059d92 [file] [log] [blame]
tiernof7aa8c42016-09-06 16:43:04 +02001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
tierno9a61c6b2016-09-08 10:57:02 +02005# This file is part of openvim
tiernof7aa8c42016-09-06 16:43:04 +02006# 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
24LICENSE_HEAD='/**
25* Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
tierno9a61c6b2016-09-08 10:57:02 +020026* This file is part of openvim
tiernof7aa8c42016-09-06 16:43:04 +020027* All Rights Reserved.
28*
29* Licensed under the Apache License, Version 2.0 (the "License"); you may
30* not use this file except in compliance with the License. You may obtain
31* a copy of the License at
32*
33* http://www.apache.org/licenses/LICENSE-2.0
34*
35* Unless required by applicable law or agreed to in writing, software
36* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
37* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
38* License for the specific language governing permissions and limitations
39* under the License.
40*
41* For those usages not covered by the Apache License, Version 2.0 please
42* contact with: nfvlabs@tid.es
43**/
44'
45
46
47DBUSER="vim"
48DBPASS=""
49DBHOST="localhost"
50DBPORT="3306"
51DBNAME="vim_db"
52
53# Detect paths
54MYSQL=$(which mysql)
55AWK=$(which awk)
56GREP=$(which grep)
57DIRNAME=`dirname $0`
58
59function usage(){
60 echo -e "Usage: $0 OPTIONS"
61 echo -e " Dumps openvim 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
71while 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
106done
107shift $((OPTIND-1))
108
109#check and ask for database user password
110DBUSER_="-u$DBUSER"
111DBPASS_=""
112[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
113DBHOST_="-h$DBHOST"
114DBPORT_="-P$DBPORT"
115while ! echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME >/dev/null 2>&1
116do
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
128done
129
130
131#echo structure, including the content of schema_version
132echo "$LICENSE_HEAD" > ${DIRNAME}/${DBNAME}_structure.sql
133mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-data --add-drop-table --add-drop-database --routines --databases $DBNAME >> ${DIRNAME}/${DBNAME}_structure.sql
134echo -e "\n\n\n\n" >> ${DIRNAME}/${DBNAME}_structure.sql
135mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-create-info $DBNAME --tables schema_version 2>/dev/null >> ${DIRNAME}/${DBNAME}_structure.sql
136echo " ${DIRNAME}/${DBNAME}_structure.sql"
137
138#echo only data
139echo "$LICENSE_HEAD" > ${DIRNAME}/${DBNAME}_data.sql #copy my own header
140mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-create-info $DBNAME >> ${DIRNAME}/${DBNAME}_data.sql
141echo " ${DIRNAME}/${DBNAME}_data.sql"
142
143#echo all
144echo "$LICENSE_HEAD" > ${DIRNAME}/${DBNAME}_all.sql #copy my own header
145mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --add-drop-table --add-drop-database --routines --databases $DBNAME >> ${DIRNAME}/${DBNAME}_all.sql
146echo " ${DIRNAME}/${DBNAME}_all.sql"
147