Created
January 12, 2024 18:18
-
-
Save jancel/bb3e4eaf85ea39a2cf6aadfa12af84fe to your computer and use it in GitHub Desktop.
Short script that will copy a postgres database to somewhere given URLs (Can change username and database name)
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 | |
: "${SOURCE:?Please provide a SOURCE which should be a database url}" | |
: "${TARGET:?Please provide a TARGET which should be a database url}" | |
dump_path=dump | |
if [ -d "$dump_path" ]; then | |
echo "/$dump_path already exists. Please remove ./$dump_path before running this script." | |
exit 1 | |
fi | |
echo "Dumping database from $SOURCE to $TARGET via $dump_path" | |
pg_dump \ | |
--clean --if-exists \ | |
--file ${dump_path} \ | |
--format=directory \ | |
--jobs 5 \ | |
--no-acl \ | |
--no-owner \ | |
${SOURCE} | |
echo "Restoring database from $dump_path to $TARGET" | |
pg_restore \ | |
--clean --if-exists \ | |
--dbname=${TARGET} \ | |
--format=directory \ | |
--jobs=5 \ | |
--no-acl \ | |
--no-owner \ | |
$dump_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment