Created
December 5, 2013 04:37
-
-
Save joker-x/7800191 to your computer and use it in GitHub Desktop.
Clona la base de datos $1 con el nombre $2 en el mismo servidor sql sin utilizar mysqldump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Clona la base de datos $1 con el nombre $2 | |
set -e | |
# Comprobaciones | |
[ $# -ne 2 ] && echo "ERROR: Número de argumentos" && exit 1 | |
[ ! -f ~/.my.cnf ] && echo "ERROR: ~/.my.cnf no definido" && exit 1 | |
[ "$1" == "$2" ] && echo "ERROR: No puedes clonar sobre ti mismo" && exit 1 | |
BBDD=$(mysql $1 -s -e 'SHOW DATABASES;') | |
[ -z $(echo "$BBDD" | grep $1) ] && echo "ERROR: La base de datos $1 no existe o no tienes permiso de acceso" && exit 1 | |
if [ -z $(echo "$BBDD" | grep $2) ]; then | |
read -p "La base de datos $2 no existe. ¿Desea crearla? (S/N) " CONFIRMA | |
if [[ "$CONFIRMA" == "S" || "$CONFIRMA" == "s" ]]; then | |
mysql -e "CREATE DATABASE $2 CHARACTER SET utf8 COLLATE utf8_general_ci;" | |
else | |
echo "Saliendo..." && exit 1 | |
fi | |
else | |
read -p "La base de datos $2 existe. ¿Desea sobreescribirla? (S/N) " CONFIRMA | |
if [[ "$CONFIRMA" == "S" || "$CONFIRMA" == "s" ]]; then | |
mysql -e "DROP DATABASE $2;" | |
mysql -e "CREATE DATABASE $2 CHARACTER SET utf8 COLLATE utf8_general_ci;" | |
else | |
echo "Saliendo..." && exit 1 | |
fi | |
fi | |
TABLAS=$(mysql $1 -s -e 'SHOW TABLES;') | |
for TABLA in $TABLAS | |
do | |
echo "Clonando tabla $TABLA" | |
mysql -e "CREATE TABLE \`$2\`.\`$TABLA\` LIKE \`$1\`.\`$TABLA\`;"; | |
mysql -e "SET sql_mode='NO_AUTO_VALUE_ON_ZERO';INSERT INTO \`$2\`.\`$TABLA\` SELECT * FROM \`$1\`.\`$TABLA\`;" | |
done; | |
echo "La base de datos $1 ha sido clonada en $2 con éxito" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment