Skip to content

Instantly share code, notes, and snippets.

@estum
Last active April 22, 2022 10:28
Show Gist options
  • Save estum/fba5b81ebd882a422cfc4bd4c30cf6f8 to your computer and use it in GitHub Desktop.
Save estum/fba5b81ebd882a422cfc4bd4c30cf6f8 to your computer and use it in GitHub Desktop.
Bash Script to dump remote db & restore on local
#!/usr/bin/env bash
##
# @section Variables
SOURCE_DB=
TARGET_DB=
REMOTE_USER=${REMOTE_USER:-"postgres"}
SSH_URI=
SSH_OPTIONS=${SSH_OPTIONS:-""}
PG_DUMP_OPTIONS=${PG_DUMP_OPTIONS:-"--no-owner --no-acl --format=c --compress=9"}
PG_RESTORE_OPTIONS=${PG_RESTORE_OPTIONS:-"--no-owner --no-privileges --clean --if-exists --jobs=4 --create"}
VERBOSE=false
##
# @section Functions
function prd::getopt {
if [[ $(uname) -eq "Darwin" ]]; then echo "/usr/local/opt/gnu-getopt/bin/getopt"; else echo "getopt"; fi
}
function pgd::usage {
cat <<-EOF
Usage: pull_remote_db [--help] [--vebose] [OPTIONS] [user@]hostname
OPTIONS
-h, --help Show this help message & exit
-v, --verbose Verbose output
-u, --user <USERNAME> Remote database username
(default: "postgres")
-s, --source <DB_NAME> Remote source database name
-t, --target <DB_NAME> Target local database name,
(default: "%{SOURCE_DB}_dev")
--ssh-options <FLAGS> Options for ssh command
--pg_dump-options <FLAGS> Options for pg_dump command
(default: "--no-owner --no-acl --format=c --compress=9")
--pg_restore-options <FLAGS> Options for pg_resotre command
(default: "--no-owner --no-privileges --clean --if-exists --jobs=4 --create")
EOF
}
function pgd::print_vars {
cat <<-EOF
Variables:
SOURCE_DB ${SOURCE_DB}
TARGET_DB ${TARGET_DB}
REMOTE_USER ${REMOTE_USER}
SSH_URI ${SSH_URI}
SSH_OPTIONS ${SSH_OPTIONS}
PG_DUMP_OPTIONS ${PG_DUMP_OPTIONS}
PG_RESTORE_OPTIONS ${PG_RESTORE_OPTIONS}
LOCAL_DUMP ${LOCAL_DUMP}
EOF
}
function prd::dump_remote_db {
echo "Dumping remote db ${SOURCE_DB}..."
ssh "${SSH_URI}" ${SSH_OPTIONS} "pg_dump ${PG_DUMP_OPTIONS} --user=${REMOTE_USER} ${SOURCE_DB}" > "${LOCAL_DUMP}" && \
test -s "${LOCAL_DUMP}" && echo "Dumped to `${LOCAL_DUMP}`!"
return $?
}
# @arg pg_restore_options
function prd::restore_local_db {
echo "Restoring local db ${TARGET_DB}..."
pg_restore ${PG_RESTORE_OPTIONS} -d ${TARGET_DB} "${LOCAL_DUMP}"
}
##
# @section Main
TEMP=`$(prd::getopt) -o hvl:s:t:u: --long help,verbose,source:,target:,user:,ssh-options:,pg_dump-options:,pg_restore-options: \
-n 'pull_remote_db' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while true; do
case "$1" in
-h|--help) pgd::usage && exit 0 ;;
-v|--verbose) VERBOSE=true; shift ;;
-s|--source) SOURCE_DB="$2"; shift 2 ;;
-t|--target) TARGET_DB="$2"; shift 2 ;;
-u|--user) REMOTE_USER="$2"; shift 2 ;;
--ssh-options) SSH_OPTIONS="$2"; shift 2 ;;
--pg_dump-options) PG_DUMP_OPTIONS="$2"; shift 2 ;;
--pg_restore-options) PG_RESTORE_OPTIONS="$2"; shift 2;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [[ -n "$1" ]]; then
SSH_URI="$1"
shift
else
echo -e "Error: Missing required argument with [user@]hostname\n" >&2
pgd::usage
exit 1
fi
TMPDIR=$(mktemp -d)
trap "rm -rf $TMPDIR" EXIT
LOCAL_DUMP="${TMPDIR}/${SOURCE_DB}.dump"
TARGET_DB=${TARGET_DB:-"${SOURCE_DB}_dev"}
[[ $VERBOSE -eq "true" ]] && pgd::print_vars
prd::dump_remote_db && prd::restore_local_db && echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment