Last active
November 18, 2019 05:08
-
-
Save songpon/dfb367597745da383fc5016870483b58 to your computer and use it in GitHub Desktop.
restore db from remoteserver
This file contains hidden or 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 | |
FROM_DB="db1" | |
TO_DB="db2" | |
DBUSER="dbuser" | |
DBHOST="192.168.1.99" | |
pg_dump -h $DBUSER -U $DBUSER $FROM_DB | gzip > /tmp/dump.sql.gz | |
psql -h $DBUSER -U $DBUSER postgres -c "SELECT | |
pg_terminate_backend(pid) | |
FROM | |
pg_stat_activity | |
WHERE | |
-- don't kill my own connection! | |
pid <> pg_backend_pid() | |
-- don't kill the connections to other databases | |
AND datname in ('$TO_DB') | |
;" | |
dropdb -h $DBUSER -U $DBUSER $TO_DB | |
createdb -h $DBUSER -U $DBUSER $TO_DB | |
gunzip -c /tmp/dump.sql.gz | psql -q -h $DBUSER -U $DBUSER $TO_DB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment