Skip to content

Instantly share code, notes, and snippets.

@victorfsf
Created July 21, 2016 12:00
Show Gist options
  • Save victorfsf/78af1f924288639d6b43a5c6e8d88f25 to your computer and use it in GitHub Desktop.
Save victorfsf/78af1f924288639d6b43a5c6e8d88f25 to your computer and use it in GitHub Desktop.
# variables
dbname=$1
dumpfile=$2
dbbk=$dbname"_bk"
# defaults
usebk=${3:-false}
host=${4:-localhost}
port=${5:-5432}
username=${6:-postgres}
dropDatabase() {
echo "Dropping database: '"$1"'..."
dropdb $1
echo "Database dropped!\n"
}
createBackupDB() {
echo "Creating backup database: '"$1"'..."
createdb $1
echo "Database created!\n"
}
createDB() {
echo "Creating database: '"$1"'..."
createdb $1
echo "Database created!\n"
}
restoreFromDump() {
echo "Restoring database: '"$1"'..."
pg_restore --host $3 --port $4 --username $5 --dbname $1 --no-password --verbose $2
echo "Database restored!\n"
}
createDBFromTemplate() {
echo "Creating database '"$1"' from template '"$2"'..."
createdb -T $2 $1
echo "Database created!\n"
}
if [ $usebk = true ]
then
dropDatabase $dbbk
dropDatabase $dbname
createBackupDB $dbbk
restoreFromDump $dbbk $dumpfile $host $port $username
createDBFromTemplate $dbname $dbbk
else
dropDatabase $dbname
createDB $dbname
restoreFromDump $dbname $dumpfile $host $port $username
fi
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment