├── mongodb-backup.sh └── readme.md /mongodb-backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Michael Mottola 4 | # 5 | # December 18, 2011 6 | # 7 | # Creates backup files (bson) of all MongoDb databases on a given server. 8 | # Default behaviour dumps the mongo database and tars the output into a file 9 | # named after the current date. ex: 2011-12-19.tar.gz 10 | # 11 | 12 | ### Set server settings 13 | HOST="localhost" 14 | PORT="27017" # default mongoDb port is 27017 15 | USERNAME="" 16 | PASSWORD="" 17 | 18 | # Set where database backups will be stored 19 | # keyword DATE gets replaced by the current date, you can use it in either path below 20 | BACKUP_PATH="/path/to/backup/directory" # do not include trailing slash 21 | FILE_NAME="DATE" #defaults to [currentdate].tar.gz ex: 2011-12-19.tar.gz 22 | 23 | 24 | ################################################################################## 25 | # Should not have to edit below this line unless you require special functionality 26 | # or wish to make improvements to the script 27 | ################################################################################## 28 | 29 | # Auto detect unix bin paths, enter these manually if script fails to auto detect 30 | MONGO_DUMP_BIN_PATH="$(which mongodump)" 31 | TAR_BIN_PATH="$(which tar)" 32 | 33 | # Get todays date to use in filename of backup output 34 | TODAYS_DATE=`date "+%Y-%m-%d"` 35 | 36 | # replace DATE with todays date in the backup path 37 | BACKUP_PATH="${BACKUP_PATH//DATE/$TODAYS_DATE}" 38 | 39 | # Create BACKUP_PATH directory if it does not exist 40 | [ ! -d $BACKUP_PATH ] && mkdir -p $BACKUP_PATH || : 41 | 42 | # Ensure directory exists before dumping to it 43 | if [ -d "$BACKUP_PATH" ]; then 44 | 45 | cd $BACKUP_PATH 46 | 47 | # initialize temp backup directory 48 | TMP_BACKUP_DIR="mongodb-$TODAYS_DATE" 49 | 50 | echo; echo "=> Backing up Mongo Server: $HOST:$PORT"; echo -n ' '; 51 | 52 | # run dump on mongoDB 53 | if [ "$USERNAME" != "" -a "$PASSWORD" != "" ]; then 54 | $MONGO_DUMP_BIN_PATH --host $HOST:$PORT -u $USERNAME -p $PASSWORD --out $TMP_BACKUP_DIR >> /dev/null 55 | else 56 | $MONGO_DUMP_BIN_PATH --host $HOST:$PORT --out $TMP_BACKUP_DIR >> /dev/null 57 | fi 58 | 59 | # check to see if mongoDb was dumped correctly 60 | if [ -d "$TMP_BACKUP_DIR" ]; then 61 | 62 | # if file name is set to nothing then make it todays date 63 | if [ "$FILE_NAME" == "" ]; then 64 | FILE_NAME="$TODAYS_DATE" 65 | fi 66 | 67 | # replace DATE with todays date in the filename 68 | FILE_NAME="${FILE_NAME//DATE/$TODAYS_DATE}" 69 | 70 | # turn dumped files into a single tar file 71 | $TAR_BIN_PATH --remove-files -czf $FILE_NAME.tar.gz $TMP_BACKUP_DIR >> /dev/null 72 | 73 | # verify that the file was created 74 | if [ -f "$FILE_NAME.tar.gz" ]; then 75 | echo "=> Success: `du -sh $FILE_NAME.tar.gz`"; echo; 76 | 77 | # forcely remove if files still exist and tar was made successfully 78 | # this is done because the --remove-files flag on tar does not always work 79 | if [ -d "$BACKUP_PATH/$TMP_BACKUP_DIR" ]; then 80 | rm -rf "$BACKUP_PATH/$TMP_BACKUP_DIR" 81 | fi 82 | else 83 | echo "!!!=> Failed to create backup file: $BACKUP_PATH/$FILE_NAME.tar.gz"; echo; 84 | fi 85 | else 86 | echo; echo "!!!=> Failed to backup mongoDB"; echo; 87 | fi 88 | else 89 | 90 | echo "!!!=> Failed to create backup path: $BACKUP_PATH" 91 | 92 | fi 93 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Mongo DB Shell Backup 2 | 3 | Shell script for backing up Mongo Databases on a given server. 4 | You can have your Mongo Database backed up and compressed in a directory and filename of your choice. 5 | 6 | ## Configuration 7 | 8 | At a minimum there are only two lines in the file you will need to edit to make this script work for you. 9 | If you server is not on localhost or requires a username and password you will need to correct that information 10 | at the top of the file. 11 | 12 | ### Defaults 13 | 14 | * HOST: localhost 15 | * PORT: 27017 16 | * USERNAME: blank 17 | * PASSWORD: blank 18 | 19 | ### Setting Path and File 20 | 21 | By default (as the script comes, and if choosen FILE_NAME is empty), the script will generate a file 22 | with the current date as the filename. 23 | 24 | When the word 'DATE' appears in the backup path or file name it is replaced by the current date. 25 | This allows you to put the date the backup was made in the file or a directory itself, 26 | very useful for organizing daily backups when running from a cron job. 27 | 28 | 29 | * BACKUP_PATH (Line #20): This path is where your backups will be stored (omit trailing slash) 30 | * FILE_NAME (Line #21): This the filename of the tar file that is generated (tar.gz will be appended) 31 | 32 | Examples 33 | 34 | # create a file called mongo-db.tar.gz in a directory the current date (daily backups) 35 | BACKUP_PATH="/opt/db-backups/daily/DATE" 36 | FILE_NAME="mongo-db" 37 | 38 | # create a file with the date in the filename 39 | BACKUP_PATH="/opt/db-backups" 40 | FILE_NAME="mongodb.DATE" #ex: mongodb.2011-12-19.tar.gz 41 | 42 | ### Script Output 43 | 44 | On successfully backup the script will inform you of the filesize and filename of the backup 45 | 46 | => Backing up Mongo Server: localhost:27017 47 | connected to: localhost:27017 48 | => Success: 4.6M mongo-db.tar.gz 49 | 50 | 51 | ## Restoring from Backup 52 | 53 | Restore a backup using the `mongorestore` command. 54 | 55 | mongorestore [options] [directory or filename to restore from] 56 | 57 | Examples 58 | mongorestore /opt/mongo-backups/myawesome_project_production/ 59 | mongorestore /opt/mongo-backups/myawesome_project_production/users.bson 60 | 61 | For more information, check out the mongoDB site: 62 | 63 | http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongodumpandmongorestore 64 | 65 | 66 | # License (MIT) 67 | 68 | Copyright (c) 2011 Michael Mottola 69 | 70 | Permission is hereby granted, free of charge, to any person obtaining 71 | a copy of this software and associated documentation files (the 72 | "Software"), to deal in the Software without restriction, including 73 | without limitation the rights to use, copy, modify, merge, publish, 74 | distribute, sublicense, and/or sell copies of the Software, and to 75 | permit persons to whom the Software is furnished to do so, subject to 76 | the following conditions: 77 | 78 | The above copyright notice and this permission notice shall be 79 | included in all copies or substantial portions of the Software. 80 | 81 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 82 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 83 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 84 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 85 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 86 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 87 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 88 | --------------------------------------------------------------------------------