Created
February 14, 2018 10:51
-
-
Save larskarbo/5659897b452b0215bed936055dcfd145 to your computer and use it in GitHub Desktop.
Script to pull remote database and overwrite local
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
function exit_script() { | |
echo $1 | |
ssh -S db-sync-socket -O exit $REMOTE_CREDENTIALS 2> /dev/null | |
exit 1 | |
} | |
function print_usage() { | |
echo "Usage: -d databasename -m host [-h]" | |
exit 0 | |
} | |
# Get All Variables | |
# ------------------------------------------------------------------------------------------------------------ | |
while getopts :l:r:m:h OPTION; do | |
case $OPTION in | |
l) LOCAL_DB=$OPTARG ;; | |
r) REMOTE_DB=$OPTARG ;; | |
m) HOST=$OPTARG ;; | |
h) print_usage ;; | |
\?) exit_script "Invalid option -$OPTARG. Please run '-h' for help" ;; | |
:) exit_script "Option -$OPTARG requires an argument. Please run '-h' for help" ;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# Sanity checks | |
# ------------------------------------------------------------------------------------------------------------ | |
echo "Validating all variables have value..." | |
SCRIPT_VARS="REMOTE_DB LOCAL_DB HOST" | |
for SCRIPT_VAR in $SCRIPT_VARS; do | |
[[ -z ${!SCRIPT_VAR} ]] && | |
exit_script "ERROR: Variable $SCRIPT_VAR must have a value." | |
done | |
ssh -f $HOST -L 27018:localhost:27017 -N | |
mongo $LOCAL_DB --eval 'db.dropDatabase();' | |
mongo $LOCAL_DB --eval "db.copyDatabase(\"$REMOTE_DB\", \"$LOCAL_DB\", 'localhost:27018');" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment