MongoDB backup script

Last year while I was working in a project, I needed to automate the whole 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.


Couple of days ago, I started to code for making small backup script that 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:

#SET the bd name which one you want to backup
#SET server path where you want to save the file
#SET your user name
#SET your host name or IP of the server
date_now=`date +%Y_%m_%d_%H_%M_%S`
current_year=`date +%Y`
log() {
echo $1
rm -rf 'db_backup_'${current_year}*
log 'cleaning up….'
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'
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 raw
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 and fsync_unlock.js) functions which responsible for locking mongo during  db snapshots and releasing lock  after the  snapshots.

Happy Coding 🙂

PHP and MongoDB web development beginners guide review

As I am working with mongodb for different application development,the more I explore mongo the more I am amazed.
I got a review copy from Packt couple of weeks ago and started to read PHP and MongoDB Web Development Beginner’s Guide.
Chapter 1:As Nosql is a new technology yet,people talk more than using it in their production.And in the first chapter author described what is NoSql,Types of NoSQl and why mongdb.Ttraditional and very effective way to start exploring a technology.The important part of this chapter is mongodb concepts section.There is a mongodb is quiet different than any other traditional db and here there is a difference in basic terminology too.Author described those point with example.
Chapter2:As its a beginners guide author chose blog application development and here the mongo querying comes through returning article from mongo back-end.Interesting part was,author focused on mongodb details implementation and feature.Like Upsert,Update and save.
chapter3:Building a session manager,here a session manager was built with the blog implementation.

chapter4:Aggregation queries one of the most important chapter of the book.For db administrating,building complex business model application developers have to gain mastery in db he is using for his application.In this chapter MapReduce was explained and implemented also found different essential querying based on count(),distinct.
The mapreduce is very essential concepts the examples are real life problems solving using mapreduce,I found it very useful.

chapter5:A very simple version of web analytics was built and why we can choose mongodb for different analytics solution was described.Mapreduce implementation for analytics is the fun part and reader will learn to aggregation in different use cases for his analytics.
Chapter 6: Focuses on Mongodb with mysql.when to use,how it fits.mongodb as a data archive system.
Chapter7 : This chapter is about Gridfs,mongodbs file storage solution.A simple application is built to show its power and author also mentioned when should not mongodb.
chapter8:Building location aware application.How to store geo location data using mongodb is described,interesting part of geospatial indexing,this topic well described with couple of real life use case.Also covered performing location query
Chapter 9 and 10:Security and administration section.Useful section is implementation indexing in mongodb,indexing in embeded document fields.Optimization rules will be very help full section to boost the  mongo performance and the end of the chapter replication is described.Chapter 10 is all about mongo administration using GUI application like rock mongo and PHPmongo.

So overall this book is a good reference/training book who wants to learn mongodb using PHP for first time,beside that author explained many under the hood topics.
Clear instruction and several pop up quizes made the chapters enjoyable to read and learn.What I expected bit more about scaling monogo,partitioning,sharding different topics.May be as the book is written for the Beginner these topics has been avoided.

Getting started MongoDB with PHP.

Web development industry is moving so fast and new solutions like NoSql is getting popular day by day.Most of the web developer and software engineers are familiar with db like Mysql.And there are specific use cases when to sue  MongoDb and when to use Mysql.

You can check this interesting discussion  from  here  to be clear.

If you want to dig more details,you can checkout this post

In this post I will code a very small PHP app,its very simple and only to get familiar with Mongo.

There are several steps like these:

1.Download and start the Mongodb from shell.

2. Download the mongo extension for PHP and enable it.

3.Declaring the mongo connection details and Creating a PHP form to collect user data.

I am assuming that in your system Apache and PHP is running already.

Step 1:Download and start the Mongodb from shell.

According to your OS and system configuration you have to download MongoDB from here.

Extract the downloaded package,go to the   terminal and naviagate your shell to the mongodb package.

Suppose  the pacakge is in Desktop/blog_post/mongodb****/

And then have to start mongoDB server by starting service mongod

so the commands will be like:

cd Desktop/blog_post/mongodb****/bin

Will see something this:

2. Download the mongo extension for PHP and enable it.

Next step is to download mongo extension for PHP.You can download it from here.
For unix based system it is very straightforward and for windows system you have to know your PHP compiler version and thread safety(check your phpinfo).
And then have to paste it to the extension directory and write into the php.ini file for enabling it.
like this:


Restart your Apache and check the phpinfo(),you will see a section like below:

3.Declaring the mongo connection:
It is simple,all you have to create a new instance of Mongo Class,it will work as a connection.
It is like:

    $connection = new Mongo();
    $db = $connection->selectDB('blog_post');

 catch (Exception $e){
     echo 'Caught exception: ',  $e->getMessage(), "\n";

Now you can create a simple form to get data,and then process make a document before inserting into the db.
I have created a 10 mins app(it took 10 min to code the form with process it for inserting).
Interface of the app is very simple:

After inserting you can check the mongo shell for the entry.For that you have to go to the mongo directory inside bin and have to run mongo service.

cd Desktop/blog_post/mongodb****/bin

In this shell you can play with mongo,to check we have to run command like below:

use blog_post
show collections

The output will be like below:

That’s it,you dont have to create a db,collections(tables) like other solutions and most interesting part its shell where you can play,discover new features which are not available in traditional databases.

To know more about mongoDb,I would suggest following books:

1.Little MongoDb Book.

2.MongoDB in Action

MongoDB command Reference is here

If you are lazy ( ! ) like me,you can fork  my basic app from here.