Created
April 24, 2019 13:25
-
-
Save gvko/8ceccfc77fc37358c9ae874019101233 to your computer and use it in GitHub Desktop.
Script for dumping a Postgres DB, in a format that can re-create an identical copy into an empty DB.
This file contains 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 | |
# Terminate script on first error | |
#set -e | |
while getopts ":d:h:p:u:P:" opt; do | |
case $opt in | |
d) DB_NAME="$OPTARG" | |
;; | |
h) HOST="$OPTARG" | |
;; | |
p) PASS="$OPTARG" | |
;; | |
u) USER="$OPTARG" | |
;; | |
P) PORT="$OPTARG" | |
;; | |
\?) echo "Invalid option $OPTARG" >&2 | |
;; | |
esac | |
done | |
# Exit if database name is not specified | |
[ -z "$DB_NAME" ] && echo "ERROR: database name must be specified (-d <name>)" && echo && exit 1 | |
# Exit if host is not specified | |
[ -z "$HOST" ] && echo "ERROR: a host must be specified (-h <name>)" && echo && exit 1 | |
# Exit if password is not specified | |
[ -z "$PASS" ] && echo "ERROR: a password must be specified (-p <value>)" && echo && exit 1 | |
# Exit if password is not specified | |
[ -z "$USER" ] && echo "ERROR: a username must be specified (-u <name>)" && echo && exit 1 | |
[ -z "$PORT" ] && PORT=5432 | |
NOW=$(date +"%Y-%m-%dT%H-%M-%S") | |
PGPASSWORD=$PASS pg_dump -Fc -h $HOST -p $PORT -U $USER $DB_NAME > "$NOW"_"$DB_NAME".dump |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment