Skip to content

Instantly share code, notes, and snippets.

@dnileshp
Last active August 7, 2017 09:40
Show Gist options
  • Save dnileshp/412b9006d51939104370aa28fdff12cf to your computer and use it in GitHub Desktop.
Save dnileshp/412b9006d51939104370aa28fdff12cf to your computer and use it in GitHub Desktop.
Backup Mongodb to google storage script
#!/bin/bash
#Force file syncronization and lock writes
mongo admin --eval "printjson(db.fsyncLock())"
MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_HOST="IPADDRESS"
MONGO_PORT="27017"
MONGO_DATABASE="admin"
TIMESTAMP=`date +%F-%H%M`
# Create backup
mongodump --authenticationDatabase admin --username 'gtadb' --password 'gtaAcc0unts&' --out mongodb-$HOSTNAME-$TIME
STAMP
# Add timestamp to backup
tar -cvf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP
# Upload to S3
#s3cmd put mongodb-$HOSTNAME-$TIMESTAMP.tar s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/mongodblive-$HOSTNAME-$TIMESTAMP.t
ar
#Upload to google storage
gsutil cp mongodb-$HOSTNAME-$TIMESTAMP.tar gs://gta_portal_databackup/gta_backup/
#remove original copy
rm -rf mongodb-$HOSTNAME-$TIMESTAMP
rm mongodb-$HOSTNAME-$TIMESTAMP.tar
#Unlock databases writes
mongo admin --eval "printjson(db.fsyncUnlock())"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment