Skip to content

Instantly share code, notes, and snippets.

@sydlawrence
Created March 12, 2013 10:29
Show Gist options
  • Save sydlawrence/5141854 to your computer and use it in GitHub Desktop.
Save sydlawrence/5141854 to your computer and use it in GitHub Desktop.
simple s3 mongo backup using s3cmd
#!/bin/bash
#Force file syncronization and lock writes
mongo admin --eval "printjson(db.fsyncLock())"
MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_HOST="127.0.0.1" #replace with your server ip
MONGO_PORT="27017"
MONGO_DATABASE="instawall" #replace with your database name
TIMESTAMP=`date +%F-%H%M`
S3_BUCKET_NAME="wmasbackups" #replace with your bucket name on Amazon S3
S3_BUCKET_PATH="mongodb-backups"
# Create backup
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
# Add timestamp to backup
mv dump mongodb-instawall-$HOSTNAME-$TIMESTAMP
tar cf mongodb-instawall-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP
# Upload to S3
s3cmd put --recursive mongodb-instawall-$HOSTNAME-$TIMESTAMP/ s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/mongodb-instawall-$HOSTNAME-$TIMESTAMP/
#Unlock database 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