Created
January 11, 2020 17:40
-
-
Save Kif11/9a0d44779a286c164b2e1b0f608c775a to your computer and use it in GitHub Desktop.
Backup and sync two mongo databases
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
#!/usr/bin/env bash | |
# Syncronize remote DB to local instance | |
remote_db="myremote-mongo.com" | |
local_db="localhost" | |
db_name="my_db_name" | |
# Get current date and store it in $date | |
printf -v date '%(%Y-%m-%d)T\n' -1 | |
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
backup_folder=${script_dir}/dump_$date | |
# Backup current DB | |
mongodump -h $remote_db --port 27017 -o $backup_folder | |
# Clear local database first | |
mongo $db_name --host localhost --eval "db.dropDatabase();" | |
# Restore backup to local DB | |
mongorestore -h $local_db $backup_folder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment