Skip to content

Instantly share code, notes, and snippets.

@luizkowalski
Forked from roberto-filho/importar-base-go.sh
Created May 16, 2013 14:38
Show Gist options
  • Save luizkowalski/5592203 to your computer and use it in GitHub Desktop.
Save luizkowalski/5592203 to your computer and use it in GitHub Desktop.
#!/bin/bash
oldbase="gruber"
newbase="gruberteste"
nomearquivo="$oldbase-$(date +%d%m%Y-%H%M%S).sql"
outputfile="/home/gts/sql-base-fiscal/$nomearquivo"
httpserver="/srv/http/"
echo "==========================================="
echo "Fazendo dump da base \"$oldbase\""
echo "==========================================="
pg_dump $oldbase -U postgres -f $outputfile
#chown $outputfile
chmod 655 $outputfile
echo "===================================================="
echo "Arquivo de dump \"$outputfile\" criado."
echo "===================================================="
if [ "$1" = "--reload" ]; then
# Mata todas as sessoes daquela database
psql -U postgres -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 postgres -c "DROP DATABASE $newbase"
psql -U postgres -c "CREATE DATABASE $newbase"
psql -d $newbase -U postgres -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