Skip to content

Instantly share code, notes, and snippets.

@mrichie
Forked from wahyusumartha/MongoDB Backup Script
Created October 17, 2012 01:42
Show Gist options
  • Save mrichie/3903262 to your computer and use it in GitHub Desktop.
Save mrichie/3903262 to your computer and use it in GitHub Desktop.
MongoDB Backup Script
#!/bin/bash
#!/usr/bin/env ruby
#########################################
# Define source and destinations
MONGO_DBS=""
BACKUP_TMP=~/tmp
BACKUP_DEST=~/backups
MONGODUMP_BIN=/Users/wahyusumartha/Documents/mongodb-db/bin/mongodump
TAR_BIN=/usr/bin/tar
HOST=xxxxx
USERNAME=xxxxx
PASSWORD=xxxxx
DATABASE=xxxx
DROPBOX_UPLOADER=dropbox-upload.rb
#######################################
BACKUPFILE_DATE=`date +%Y%m%d-%H%M`
# _do_store_archive <Database> <Dump_Dir> <Dest_Dir> <Dest_File>
function _do_store_archive {
mkdir -p $3
cd $2
tar -cvzf $3/$4 dump
}
# _do_backup <Database name>
function _do_backup {
UNIQ_DIR="$BACKUP_TMP/$1"`date "+%s"`
mkdir -p $UNIQ_DIR/dump
echo "dumping Mongo Database $1"
if [ "all" = "$1" ];
then
$MONGODUMP_BIN -h $HOST -u $USERNAME -p $PASSWORD -o $UNIQ_DIR/dump
else
$MONGODUMP_BIN -h $HOST -u $USERNAME -p $PASSWORD -d $1 -o $UNIQ_DIR/dump
fi
KEY="database-$BACKUPFILE_DATE.tgz"
echo "Archiving Mongo Database to $BACKUP_DEST/$1/$KEY"
DEST_DIR=$BACKUP_DEST/$1
_do_store_archive $1 $UNIQ_DIR $DEST_DIR $KEY
ruby $DROPBOX_UPLOADER $DEST_DIR/$KEY
rm -rf $UNIQ_DIR
}
# check to see if individual databases have been specified, otherwise backup the whole server
# to all
if ["" = "$MONGO_DBS"];
then
MONGO_DB=$DATABASE
_do_backup $MONGO_DB
else
for MONGO_DB in $MONGO_DBS; do
_do_backup $MONGO_DB
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment