Skip to content

Instantly share code, notes, and snippets.

@farhanpatel
Forked from lazarofl/a_mongodb_to_s3_backup.sh
Last active September 2, 2015 18:24
Show Gist options
  • Save farhanpatel/82c6cdce20b324a2465b to your computer and use it in GitHub Desktop.
Save farhanpatel/82c6cdce20b324a2465b to your computer and use it in GitHub Desktop.
MongoDB Automatic Backup to Amazon S3 with Crontab and s3cmd.Red Hat Linux on Amazon EC2
#!/bin/bash
MONGODUMP_PATH="/opt/local/bin/mongodump"
MONGO_HOST="localhost"
MONGO_PORT="27017"
MONGO_DATABASE="paigeeworld_main"
TIMESTAMP=`date +%F-%H%M`
S3_BUCKET_NAME="paigeeworld-backup"
S3_BUCKET_PATH="mongodb-backups-joyent"
# Create backup
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
# Add timestamp to backup
mv dump mongodb-$HOSTNAME-$TIMESTAMP
tar cf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP
rm -rf mongodb-$HOSTNAME-$TIMESTAMP
# Upload to S3
s3cmd put mongodb-$HOSTNAME-$TIMESTAMP.tar s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/mongodb-$HOSTNAME-$TIMESTAMP.tar
#every day at 01h 00m
00 01 * * * /bin/bash /home/ec2-user/mongodb_to_s3_backup.sh
sudo yum --enablerepo epel install s3cmd
#to configure s3cmd
s3cmd --configure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment