| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| tierno | 9a61c6b | 2016-09-08 10:57:02 +0200 | [diff] [blame] | 5 | # This file is part of openvim |
| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 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 | LICENSE_HEAD='/** |
| 25 | * Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| tierno | 9a61c6b | 2016-09-08 10:57:02 +0200 | [diff] [blame] | 26 | * This file is part of openvim |
| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 27 | * 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 | |
| 47 | DBUSER="vim" |
| 48 | DBPASS="" |
| 49 | DBHOST="localhost" |
| 50 | DBPORT="3306" |
| 51 | DBNAME="vim_db" |
| 52 | |
| 53 | # Detect paths |
| 54 | MYSQL=$(which mysql) |
| 55 | AWK=$(which awk) |
| 56 | GREP=$(which grep) |
| 57 | DIRNAME=`dirname $0` |
| 58 | |
| 59 | function 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 | |
| 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 | |