Last active
August 9, 2018 03:39
-
-
Save omegazeng/18b58dfc6995aaeed0bce70bdbb30caf to your computer and use it in GitHub Desktop.
Script to create full backups with mongodump.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# https://gist.github.com/omegazeng/18b58dfc6995aaeed0bce70bdbb30caf | |
# use admin | |
# db.createUser( | |
# { | |
# user: "backup", | |
# pwd: "YourPassword", | |
# roles: [ "backup", "restore" ] | |
# } | |
# ) | |
# | |
# Usage: | |
# MONGO_PASSWORD=YourPassword MONGO_HOST="mongo01.example.com:27017,mongo02.example.com:27017" REPL_SET_NAME=repl-set bash run-mongodump.sh | |
MONGO_USER=backup | |
#MONGO_PASSWORD=YourPassword | |
#MONGO_HOST="mongo01.example.com:27017,mongo02.example.com:27017" | |
#REPL_SET_NAME=repl-set | |
AUTH_DB=admin | |
BACKCMD=mongodump | |
BASEBACKDIR=/data/mongodb_backup | |
BACKDIR=$BASEBACKDIR/$(date "+%Y-%m-%d_%H-%M-%S") | |
KEEP_DAYS=14 # Days of a backup should kept for. | |
START=`date +%s` | |
echo "----------------------------" | |
echo | |
echo "run-mongodump.sh: MongoDB backup script" | |
echo "started: `date`" | |
echo | |
# Backup | |
$BACKCMD --host $REPL_SET_NAME/$MONGO_HOST --username $MONGO_USER --password $MONGO_PASSWORD --authenticationDatabase $AUTH_DB --gzip --out $BACKDIR | |
[[ $? == 0 ]] && echo "Databases backed up successfully to: $BACKDIR" || echo "Databases backup failed" | |
# Delete old backups | |
for DEL in `find $BASEBACKDIR -mindepth 1 -maxdepth 1 -type d -mtime +$KEEP_DAYS -printf "%P\n"` | |
do | |
echo "deleting $DEL" | |
rm -rf $BASEBACKDIR/$DEL | |
done | |
SPENT=$((`date +%s` - $START)) | |
echo | |
echo "took $SPENT seconds" | |
echo "completed: `date`" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment