Last active
August 24, 2016 07:43
-
-
Save umar-siddiqui/353ff6e3db276b917347d55f7bb3c11b to your computer and use it in GitHub Desktop.
Script for fetching DB on server and restoring locally.
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 | |
## Parameter 1 :: Database name on server | |
## Parameter 2 :: Server Domain name | |
## Parameter 3 :: Capistrano deployment script | |
## eg ./drop_restore_db.sh havells_resustain_io havells.resustain.io security | |
dbname=$1 | |
instancedomain=$2 | |
envi=$3 | |
echo "#################### [INFO] Executing Command :: cap $envi deploy:mongo_backup\"[$dbname]\"" | |
dumpstack=$(cap $envi deploy:mongo_backup"[$dbname]") | |
echo $dumpstack | |
foldername=$(echo $dumpstack | egrep -m 1 "[0-9]{2}-[0-9]{2}-[0-9]{4}-[0-9]{2}:[0-9]{2}:[0-9]{2}") | |
foldername=$(echo $foldername | sed -n 's/.*\([0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1/p') | |
if [ -z $foldername ]; then | |
exit 1 | |
fi | |
echo "#################### [INFO] Fetching Dump From :: azureuser@$instancedomain:/home/azureuser/dbDumps/$foldername" | |
scp -rvC azureuser@$instancedomain:/home/azureuser/dbDumps/$foldername ./ | |
COUNTER=0 | |
while [ ! -d $foldername ]; do | |
let COUNTER=COUNTER+1 | |
echo "#################### [INFO] Attempt number $COUNTER to fetch Dump From :: azureuser@$instancedomain:/home/azureuser/dbDumps/$foldername" | |
scp -rvC azureuser@$instancedomain:/home/azureuser/dbDumps/$foldername ./ | |
done | |
echo "#################### [INFO] Dropping local database $dbname" | |
mongo $dbname --eval "db.dropDatabase()" | |
echo "#################### [INFO] Restoring database $dbname from dump $foldername from server azureuser@$instancedomain" | |
mongorestore $foldername | |
echo "#################### [INFO] Cleanup" | |
rm -rf $foldername | |
echo "####################################################" | |
echo "#################### DONE ##########################" | |
echo "####################################################" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sagarkt Y hv u added quotes outside the square brackets ?? i guess it is syntactically incorrect