By separation of duty, I suggest to create the user for backup and restoration on MongoDB.
1. Create the user if not created
shell> mongo
> use admin;
> db.createUser(
{ user: "<username>",
pwd: "<password>",
roles: [
{ role: "backup" },
{ role: "restore" }
]
}
)
Enquiry the user after creation.
> use admin;
> db.getUser("<username>")
2. Create the backup script:
Remark: The below script will housekeep the backup copy more than one day.
shell> vi backup_onlinefull_compress_mongodb.sh
## Declare variables
vstartdate=`date +%d-%m-%y_%H:%M`
vdate=`date +%d%m_%H%M`
vhost=$1
vmongodbport=$2
vbackuppathlog=/backup/$vmongodbport/mongod
vbackuppath=/backup/$vmongodbport/mongod
log=$(echo $vdate"_"$vhost"_"$vmongodbport"_full_backup_compress.log")
## Check if the user has input enough parameter
if [ ! $# -eq 2 ]; then
echo ""
echo "This is the operation script for Backup MongoDB Instance."
echo "Error: Wrong Input!"
echo ""
echo "Example: ./backup_onlinefull_compress_mongodb.sh host01 27017"
exit 1;
fi
#password file
pwd=$(cat passwordfile)
log_file=$vbackuppathlog/$log
echo $vstartdate
echo $vhost
echo $config_file
echo $socket_file
echo $log_file
find $vbackuppathlog/*$vmongodbport*.log -type f -mtime +2 -exec rm {} +
find $vbackuppath -mindepth 1 -maxdepth 1 -type d -mtime +0 -exec rm -r {} +
vbackupdir=$vbackuppath/`date +%y%m%d_%H%M`
mkdir -p $vbackupdir
echo "Host: " $vhost >> $log_file
echo "Date: " $vstartdate>> $log_file
echo "mongodb Port: " $vmongodbport >> $log_file
mongodump --archive="$vbackupdir/mongodump_backup_$vmongodbport_`date +%d%m_%H%M`.gz" --oplog --gzip -u <username> -p$pwd --authenticationDatabase=admin --port=$vmongodbport &>> $log_file
cp $log_file $vbackupdir
# Set mailing list and email content
MAILLIST="user01@dummy.com"
HOSTNAME=$(hostname)
BODY=$vbackuppathlog/body.txt
echo "Host: " $vhost >> $BODY
echo "Date: " $vstartdate>> $BODY
echo "mongodb Port: " $vmongodbport >> $BODY
# Send Email - MongoDB Backup Report
MAILSUB="[$HOSTNAME Port: $vmongodbport] - MongoDB Backup Report ($vstartdate)"
mailx -a $log_file -s "$MAILSUB" "$MAILLIST" < $BODY
rm $BODY
3. setup cron job
Example: start the backup job at 01:00 everyday, the backup script is located at $HOME.
shell> crontab -e
## Daily backup for MongoDB
0 1 * * * ($HOME/backup_onlinefull_compress_mongodb.sh host01 27017 > $HOME/run_backup_onlinefull_compress_mongodb.log)
No comments:
Post a Comment