From ba940d2a446587f32391626bf864e759239b64e3 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Mon, 23 Oct 2017 18:48:27 -0200 Subject: [PATCH] =?UTF-8?q?Baseia=20scripts=20de=20migra=C3=A7=C3=A3o=20em?= =?UTF-8?q?=20dumps=20por=20banco?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapl/legacy/scripts/migra_dbs.sh | 6 +++--- sapl/legacy/scripts/migra_um_db.sh | 30 ++++++++++++++++-------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/sapl/legacy/scripts/migra_dbs.sh b/sapl/legacy/scripts/migra_dbs.sh index 729e813cb..25a78e01c 100755 --- a/sapl/legacy/scripts/migra_dbs.sh +++ b/sapl/legacy/scripts/migra_dbs.sh @@ -2,9 +2,9 @@ # rodar esse script na raiz do projeto -if [ $# -eq 2 ]; then - parallel -eta --verbose -j+0 ./sapl/legacy/scripts/migra_um_db.sh :::: <(mysql -u $1 -p$2 -e 'show databases;' | grep '^sapl_' | grep -v '_copy$') ::: $1 ::: $2 +if [ $# -ge 1 ]; then + parallel -eta --verbose -j+0 ./sapl/legacy/scripts/migra_um_db.sh :::: <(mysql -u $1 -p$2 -e 'show databases;' | grep '^sapl_') ::: $1 ::: $2 else echo "USO:" - echo " $0 [usuário mysql] [senha mysql]" + echo " $0 [senha mysql]" fi; \ No newline at end of file diff --git a/sapl/legacy/scripts/migra_um_db.sh b/sapl/legacy/scripts/migra_um_db.sh index f0644a2be..85e171792 100755 --- a/sapl/legacy/scripts/migra_um_db.sh +++ b/sapl/legacy/scripts/migra_um_db.sh @@ -1,7 +1,11 @@ #!/bin/bash # rodar esse script na raiz do projeto -if [ $# -eq 3 ]; then +if [ $# -ge 2 ]; then + + # proteje pasta com dumps de alterações acidentais + chmod -R -w ~/sapl_dumps + DATE=$(date +%Y-%m-%d) DIR=~/${DATE}_logs_migracao mkdir -p $DIR @@ -14,18 +18,16 @@ if [ $# -eq 3 ]; then echo "########################################" | tee -a $LOG echo >> $LOG - echo "--- CRIANDO BACKUP ---" | tee -a $LOG - echo >> $LOG - EXISTE=`mysql -u $2 -p$3 -N -s -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$1_copy';"` - - if [[ $EXISTE == $1_copy ]]; then - mysql -u $2 -p$3 -N -s -e "DROP DATABASE IF EXISTS $1; CREATE DATABASE $1;" && mysqldump -u $2 -p$3 $1_copy | mysql -u $2 -p$3 $1 - echo "O banco legado foi restaurado" |& tee -a $LOG - elif [[ ! $EXISTE ]]; then - mysql -u $2 -p$3 -N -s -e "CREATE DATABASE $1_copy;" - mysqldump -u $2 -p$3 $1 | mysql -u $2 -p$3 $1_copy - echo "O banco de cópia $1_copy não existia e foi criado" |& tee -a $LOG - fi + if [ $3 ]; then + # se há senha do mysql + mysql -u $2 -p "$3" -N -s -e "DROP DATABASE IF EXISTS $1; CREATE DATABASE $1;" + mysql -u $2 -p "$3" < ~/sapl_dumps/$1.sql + else + # se não há senha do mysql + mysql -u $2 -N -s -e "DROP DATABASE IF EXISTS $1; CREATE DATABASE $1;" + mysql -u $2 < ~/sapl_dumps/$1.sql + fi; + echo "O banco legado foi restaurado" |& tee -a $LOG echo >> $LOG echo "--- DJANGO MIGRATE ---" | tee -a $LOG @@ -42,5 +44,5 @@ if [ $# -eq 3 ]; then echo >> $LOG else echo "USO:" - echo " $0 [nome_database] [usuário mysql] [senha mysql]" + echo " $0 [senha mysql]" fi;