Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit 96c32ec921f66c14531935f94a6196b28f402c59 @controlz committed Dec 20, 2011
Showing with 179 additions and 0 deletions.
  1. +92 −0 mongodb-backup.sh
  2. +87 −0 readme.md
@@ -0,0 +1,92 @@
+#!/bin/bash
+#
+# Michael Mottola
+# <mikemottola@gmail.com>
+# December 18, 2011
+#
+# Creates backup files (bson) of all MongoDb databases on a given server.
+# Default behaviour dumps the mongo database and tars the output into a file
+# named after the current date. ex: 2011-12-19.tar.gz
+#
+
+### Set server settings
+HOST="localhost"
+PORT="27017" # default mongoDb port is 27017
+USERNAME=""
+PASSWORD=""
+
+# Set where database backups will be stored
+# keyword DATE gets replaced by the current date, you can use it in either path below
+BACKUP_PATH="/path/to/backup/directory" # do not include trailing slash
+FILE_NAME="DATE" #defaults to [currentdate].tar.gz ex: 2011-12-19.tar.gz
+
+
+##################################################################################
+# Should not have to edit below this line unless you require special functionality
+# or wish to make improvements to the script
+##################################################################################
+
+# Auto detect unix bin paths, enter these manually if script fails to auto detect
+MONGO_DUMP_BIN_PATH="$(which mongodump)"
+TAR_BIN_PATH="$(which tar)"
+
+# Get todays date to use in filename of backup output
+TODAYS_DATE=`date "+%Y-%m-%d"`
+
+# replace DATE with todays date in the backup path
+BACKUP_PATH="${BACKUP_PATH//DATE/$TODAYS_DATE}"
+
+# Create BACKUP_PATH directory if it does not exist
+[ ! -d $BACKUP_PATH ] && mkdir -p $BACKUP_PATH || :
+
+# Ensure directory exists before dumping to it
+if [ -d "$BACKUP_PATH" ]; then
+
+ cd $BACKUP_PATH
+
+ # initialize temp backup directory
+ TMP_BACKUP_DIR="mongodb-$TODAYS_DATE"
+
+ echo; echo "=> Backing up Mongo Server: $HOST:$PORT"; echo -n ' ';
+
+ # run dump on mongoDB
+ if [ "$USERNAME" != "" -a "$PASSWORD" != "" ]; then
+ $MONGO_DUMP_BIN_PATH --host $HOST:$PORT -u $USERNAME -p $PASSWORD --out $TMP_BACKUP_DIR >> /dev/null
+ else
+ $MONGO_DUMP_BIN_PATH --host $HOST:$PORT --out $TMP_BACKUP_DIR >> /dev/null
+ fi
+
+ # check to see if mongoDb was dumped correctly
+ if [ -d "$TMP_BACKUP_DIR" ]; then
+
+ # if file name is set to nothing then make it todays date
+ if [ "$FILE_NAME" == "" ]; then
+ FILE_NAME="$TODAYS_DATE"
+ fi
+
+ # replace DATE with todays date in the filename
+ FILE_NAME="${FILE_NAME//DATE/$TODAYS_DATE}"
+
+ # turn dumped files into a single tar file
+ $TAR_BIN_PATH --remove-files -czf $FILE_NAME.tar.gz $TMP_BACKUP_DIR >> /dev/null
+
+ # verify that the file was created
+ if [ -f "$FILE_NAME.tar.gz" ]; then
+ echo "=> Success: `du -sh $FILE_NAME.tar.gz`"; echo;
+
+ # forcely remove if files still exist and tar was made successfully
+ # this is done because the --remove-files flag on tar does not always work
+ if [ -d "$BACKUP_PATH/$TMP_BACKUP_DIR" ]; then
+ rm -rf "$BACKUP_PATH/$TMP_BACKUP_DIR"
+ fi
+ else
+ echo "!!!=> Failed to create backup file: $BACKUP_PATH/$FILE_NAME.tar.gz"; echo;
+ fi
+ else
+ echo; echo "!!!=> Failed to backup mongoDB"; echo;
+ fi
+else
+
+ echo "!!!=> Failed to create backup path: $BACKUP_PATH"
+
+fi
@@ -0,0 +1,87 @@
+# Mongo DB Shell Backup
+
+Shell script for backing up Mongo Databases on a given server.
+You can have your Mongo Database backed up and compressed in a directory and filename of your choice.
+
+## Configuration
+
+At a minimum there are only two lines in the file you will need to edit to make this script work for you.
+If you server is not on localhost or requires a username and password you will need to correct that information
+at the top of the file.
+
+### Defaults
+
+* HOST: localhost
+* PORT: 27017
+* USERNAME: blank
+* PASSWORD: blank
+
+### Setting Path and File
+
+By default (as the script comes, and if choosen FILE_NAME is empty), the script will generate a file
+with the current date as the filename.
+
+When the word 'DATE' appears in the backup path or file name it is replaced by the current date.
+This allows you to put the date the backup was made in the file or a directory itself,
+very useful for organizing daily backups when running from a cron job.
+
+
+* BACKUP_PATH (Line #20): This path is where your backups will be stored (omit trailing slash)
+* FILE_NAME (Line #21): This the filename of the tar file that is generated (tar.gz will be appended)
+
+Examples
+
+ # create a file called mongo-db.tar.gz in a directory the current date (daily backups)
+ BACKUP_PATH="/opt/db-backups/daily/DATE"
+ FILE_NAME="mongo-db"
+
+ # create a file with the date in the filename
+ BACKUP_PATH="/opt/db-backups"
+ FILE_NAME="mongodb.DATE" #ex: mongodb.2011-12-19.tar.gz
+
+### Script Output
+
+On successfully backup the script will inform you of the filesize and filename of the backup
+
+ => Backing up Mongo Server: localhost:27017
+ connected to: localhost:27017
+ => Success: 4.6M mongo-db.tar.gz
+
+
+## Restoring from Backup
+
+Restore a backup using the `mongorestore` command.
+
+ mongorestore [options] [directory or filename to restore from]
+
+ Examples
+ mongorestore /opt/mongo-backups/myawesome_project_production/
+ mongorestore /opt/mongo-backups/myawesome_project_production/users.bson
+
+For more information, check out the mongoDB site:
+
+http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongodumpandmongorestore
+
+
+# License (MIT)
+
+Copyright (c) 2011 Michael Mottola <mikemottola@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit 96c32ec

Please sign in to comment.