#!/bin/bash | |
MONGO_DATABASE="your_db_name" | |
APP_NAME="your_app_name" | |
MONGO_HOST="127.0.0.1" | |
MONGO_PORT="27017" | |
TIMESTAMP=`date +%F-%H%M` | |
MONGODUMP_PATH="/usr/bin/mongodump" | |
BACKUPS_DIR="/home/username/backups/$APP_NAME" | |
BACKUP_NAME="$APP_NAME-$TIMESTAMP" | |
# mongo admin --eval "printjson(db.fsyncLock())" | |
# $MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE | |
$MONGODUMP_PATH -d $MONGO_DATABASE | |
# mongo admin --eval "printjson(db.fsyncUnlock())" | |
mkdir -p $BACKUPS_DIR | |
mv dump $BACKUP_NAME | |
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME | |
rm -rf $BACKUP_NAME |
This comment has been minimized.
This comment has been minimized.
I used part of this to do it in a cron job: mongodump --host 0.0.0.0 -d mydb --username mayusername --password mypassword --out /var/dbbackups/backup_$(date +%Y%m%d) && cd /var/dbbackups && tar -zcf backup_$(date +%Y%m%d).tar.gz backup_$(date +%Y%m%d)/mydb |
This comment has been minimized.
This comment has been minimized.
Here is also a nice documentation to ensure, that the script will run only once simultaniusly: |
This comment has been minimized.
This comment has been minimized.
This is handy! I also added an option in the output to save based on hostname, should you want to organize it in a distributed fashion. BACKUP_NAME="$APP_NAME-$HOSTNAME-$TIMESTAMP" |
This comment has been minimized.
This comment has been minimized.
according to this post of mongo DB team we can use new
|
This comment has been minimized.
This comment has been minimized.
How can you make it delete dumps after 7 days ? |
This comment has been minimized.
This comment has been minimized.
Thanks man! |
This comment has been minimized.
This comment has been minimized.
Thanks, Mate!! |
This comment has been minimized.
This comment has been minimized.
Thanks for this... really handy |
This comment has been minimized.
This comment has been minimized.
Great! Thanks |
This comment has been minimized.
This comment has been minimized.
When I am running the script getting error "Failed to authenticate admin@trzprod with mechanism MONGODB-CR: AuthenticationFailed key mismatch" Please help me out this |
This comment has been minimized.
This comment has been minimized.
The command below will only save for newest 7 files.
|
This comment has been minimized.
This comment has been minimized.
That great @yueyericardo, How we can save 15-day dumps in S3, older should delete automatically. please let me know and I want to add the lines to exiting the script |
This comment has been minimized.
This comment has been minimized.
Hi, simply add this to the end of script. # only keep the newest 15 backups
cd $BACKUPS_DIR
ls --color=no -t | sed -e '1,15d' | xargs -d '\n' rm |
This comment has been minimized.
This comment has been minimized.
#!/bin/bash
MONGO_DATABASE="your_db_name"
APP_NAME="your_app_name"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/home/username/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
#=====================================================================
DAYSTORETAINBACKUP="15"
#=====================================================================
# mongo admin --eval "printjson(db.fsyncLock())"
# $MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
$MONGODUMP_PATH -d $MONGO_DATABASE
# mongo admin --eval "printjson(db.fsyncUnlock())"
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME`
#=====================================================================
find $BACKUPS_DIR -type f -mtime +$DAYSTORETAINBACKUP -exec rm {} +
echo "--------------------------------------------"
echo "Database backup complete!"
#===================================================================== i added the lines for you |
This comment has been minimized.
This comment has been minimized.
There is one little error make this line Otherwise it script broken on line 24. However, thanks for it. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Hi @serverguru666, just wondering does mongoDB authenticationType have anything to do with this error that i get when i try mongodump using your connection format - NB: The issue is that the command doesn't want me to add |
This comment has been minimized.
This comment has been minimized.
How to delete after 1 week please let me know.. |
This comment has been minimized.
Make it executable:
Schedule a Cronjob:
Enter this in a new line:
Also See: mongo-sync