Skip to content

Instantly share code, notes, and snippets.

@roberto-filho
Last active December 17, 2015 10:08
Show Gist options
  • Save roberto-filho/5592185 to your computer and use it in GitHub Desktop.
Save roberto-filho/5592185 to your computer and use it in GitHub Desktop.
Script de importação do banco de dados
#!/bin/bash
oldbase="gruber"
newbase="gruberteste"
nomearquivo="$oldbase-$(date +%d%m%Y-%H%M%S).sql"
outputfile="/home/gts/sql-base-fiscal/$nomearquivo"
httpserver="/var/www/"
db_user="admin"
echo "==========================================="
echo "Fazendo dump da base \"$oldbase\""
echo "==========================================="
pg_dump $oldbase -U $db_user -f $outputfile
#chown $outputfile
chmod 775 $outputfile
echo "===================================================="
echo "Arquivo de dump \"$outputfile\" criado."
echo "===================================================="
if [ "$1" = "--reload" ]; then
# Mata todas as sessoes daquela database
psql -U $db_user -c "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE procpid <> pg_backend_pid() AND datname = '$newbase';"
# Recria a base e depois roda o dump nela
psql -U $db_user -c "DROP DATABASE $newbase"
psql -U $db_user -c "CREATE DATABASE $newbase"
psql -d $newbase -U $db_user -f $outputfile
# psql --user=postgres --command="DROP DATABASE $newbase; CREATE DATABASE $newbase;"
echo "DATABASE [$oldbase] IMPORTADA PARA [$newbase] COM SUCESSO"
fi
# Copia o dump para o servidor
cp $outputfile $httpserver
echo "Arquivo [$outputfile] copiado com sucesso em [$httpserver]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment