Tuesday, May 20, 2014

MongoDB backup script

Last year while I was working in a project, I needed to automate thewhole backing up process from taking snapshot of the current db to saving it to AWS S3 buckets. At that time I took most of the staffs from this blog post.
mongo_logo
Couple of days ago, I started to code for making small backup scriptthat will backup to another cloud machine rather than to AWS S3.  Instead of  coding it from scratch, I reused my previously coded script. All I need to implement a bash function(save_in_cloud) which runs a simple scp command :)
The whole script look like below:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
#!/bin/sh
 
MONGODB_SHELL='mongo'
DUMP_UTILITY='mongodump'
#SET the bd name which one you want to backup
DB_NAME=''
 
#SET server path where you want to save the file
CLOUD_PATH=''
 
#SET your user name
SERVER_USER=''
 
#SET your host name or IP of the server
HOST_NAME=''
 
date_now=`date +%Y_%m_%d_%H_%M_%S`
dir_name='db_backup_'${date_now}
file_name='db_backup_'${date_now}'.bz2'
current_year=`date +%Y`
 
 
log() {
echo $1
}
 
do_cleanup(){
rm -rf 'db_backup_'${current_year}*
log 'cleaning up....'
}
 
do_backup(){
log 'snapshotting the db and creating archive' && \
${MONGODB_SHELL} admin fsync_lock.js && \
log 'db locked and creating backup'
${DUMP_UTILITY} -d ${DB_NAME} -o ${dir_name} && tar -jcf $file_name ${dir_name} && \
${MONGODB_SHELL} admin fsync_unlock.js && \
log 'data backd up and created snapshot'
}
 
save_in_cloud(){
log 'saving backup to another server...'
scp ${file_name} ${SERVER_USER}@${HOST_NAME}:${CLOUD_PATH}
log 'saved scuccessfully'
}
 
 
do_backup && save_in_cloud && do_cleanup
view rawmongo_backup_script hosted with ❤ by GitHub
I reused this script, all I did just added a new function which copy the current backup data to a remote server.  And also updated do_cleanup, now it works in any year.
The backup script depends on other two js (fsync_lock.js andfsync_unlock.js) functions which responsible for locking mongo during  db snapshots and releasing lock  after the  snapshots.

2 comments:

eu4 console commands said...

Thanks for the explanation. Assuming that my application is connected to the primary server ip and this fall. How to make my application to connect transparently secondary? Hesitate to shake this one? after all, the IP changed. Thank you.


MongoDB backup script said...

Thanks for sharing valuable information on MongoDB backup script. It makes MongoDB backup easy.