| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2015 Telefónica Investigación 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 | DBUSER="mano" |
| 26 | DBPASS="" |
| 27 | DBHOST="localhost" |
| 28 | DBPORT="3306" |
| 29 | DBNAME="mano_db" |
| 30 | |
| 31 | # Detect paths |
| 32 | MYSQL=$(which mysql) |
| 33 | AWK=$(which awk) |
| 34 | GREP=$(which grep) |
| 35 | DIRNAME=`dirname $0` |
| 36 | |
| 37 | function usage(){ |
| 38 | echo -e "Usage: $0 OPTIONS" |
| 39 | echo -e " Dumps openmano database content" |
| 40 | echo -e " OPTIONS" |
| 41 | echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails" |
| 42 | echo -e " -p PASS database password. 'No password' by default. Prompts if DB access fails" |
| 43 | echo -e " -P PORT database port. '$DBPORT' by default" |
| 44 | echo -e " -h HOST database host. '$DBHOST' by default" |
| 45 | echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails" |
| 46 | echo -e " --help shows this help" |
| 47 | } |
| 48 | |
| 49 | while getopts ":u:p:P:h:-:" o; do |
| 50 | case "${o}" in |
| 51 | u) |
| 52 | DBUSER="$OPTARG" |
| 53 | ;; |
| 54 | p) |
| 55 | DBPASS="$OPTARG" |
| 56 | ;; |
| 57 | P) |
| 58 | DBPORT="$OPTARG" |
| 59 | ;; |
| 60 | d) |
| 61 | DBNAME="$OPTARG" |
| 62 | ;; |
| 63 | h) |
| 64 | DBHOST="$OPTARG" |
| 65 | ;; |
| 66 | -) |
| 67 | [ "${OPTARG}" == "help" ] && usage && exit 0 |
| 68 | echo "Invalid option: --$OPTARG" >&2 && usage >&2 |
| 69 | exit 1 |
| 70 | ;; |
| 71 | \?) |
| 72 | echo "Invalid option: -$OPTARG" >&2 && usage >&2 |
| 73 | exit 1 |
| 74 | ;; |
| 75 | :) |
| 76 | echo "Option -$OPTARG requires an argument." >&2 && usage >&2 |
| 77 | exit 1 |
| 78 | ;; |
| 79 | *) |
| 80 | usage >&2 |
| 81 | exit -1 |
| 82 | ;; |
| 83 | esac |
| 84 | done |
| 85 | shift $((OPTIND-1)) |
| 86 | |
| 87 | #check and ask for database user password |
| 88 | DBUSER_="-u$DBUSER" |
| 89 | DBPASS_="" |
| 90 | [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS" |
| 91 | DBHOST_="-h$DBHOST" |
| 92 | DBPORT_="-P$DBPORT" |
| 93 | while ! echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME >/dev/null 2>&1 |
| 94 | do |
| 95 | [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)" |
| 96 | [ -z "$logintry" ] && echo -e "\nProvide database name and credentials" |
| 97 | read -e -p "mysql database name($DBNAME): " KK |
| 98 | [ -n "$KK" ] && DBNAME="$KK" |
| 99 | read -e -p "mysql user($DBUSER): " KK |
| 100 | [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER" |
| 101 | read -e -s -p "mysql password: " DBPASS |
| 102 | [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS" |
| 103 | [ -z "$DBPASS" ] && DBPASS_="" |
| 104 | logintry="yes" |
| 105 | echo |
| 106 | done |
| 107 | |
| 108 | |
| 109 | #echo structure, including the content of schema_version |
| 110 | mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-data --add-drop-table --add-drop-database --routines --databases $DBNAME > ${DIRNAME}/${DBNAME}_structure.sql |
| 111 | echo -e "\n\n\n\n" >> ${DIRNAME}/${DBNAME}_structure.sql |
| 112 | mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-create-info $DBNAME --tables schema_version 2>/dev/null >> ${DIRNAME}/${DBNAME}_structure.sql |
| 113 | echo " ${DIRNAME}/${DBNAME}_structure.sql" |
| 114 | |
| 115 | #echo only data |
| 116 | mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-create-info $DBNAME > ${DIRNAME}/${DBNAME}_data.sql |
| 117 | echo " ${DIRNAME}/${DBNAME}_data.sql" |
| 118 | |
| 119 | #echo all |
| 120 | mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --add-drop-table --add-drop-database --routines --databases $DBNAME > ${DIRNAME}/${DBNAME}_all.sql |
| 121 | echo " ${DIRNAME}/${DBNAME}_all.sql" |
| 122 | |