Implement mongodb3.0.5 database automatic backup in Linux system

Source: Internet
Author: User
Tags mongodump

In the last two days, the data in the MongoDB database should be backed up regularly due to business needs.

After checking a lot of data, I found that MongoDB does not seem to have its own scheduled backup function, so we have to transfer the target to the Linux system's scheduled tasks, then learn and use Crontab, and successfully scheduled backup.

Reference Document: http://blog.itpub.net/519536/viewspace-659881/

http://www.linuxdiyf.com/viewarticle.php?id=4344

http://blog.csdn.net/love__coder/article/details/6890997

Http://www.cnblogs.com/cosiray/archive/2012/03/09/2387361.html


My implementation process is largely two-step, one under the root user, and one for the other non-root users. Because the test machine on the MongoDB and so on are used by the root user, so the first stumbled on the direct operation.

One, under the root user:

1. Environment: Redhet 6.3

MongoDB installation directory:/mongodb305/mongodb305/bin

Mongdb in the database admin, user name admin, password admin


2, First wrote a MongoDB backup script test.sh, and in the root directory to create a directory admin:

   [Email protected] ~]# VI test.sh
   [Email protected] ~]# mkdir/root/admin

3, the contents of the test.sh file are as follows: (Here are two commands, the first command after the semicolon can not be less, at least I do not add a semicolon at the time of operation)

   Rm-rf/root/admin;   /mongodb305/mongodb305/bin/mongodump--port 27017-u admin-p admin-d admin-o  /root/admin   ~   ~   ~   ~   ~--   INSERT--

4. Then set the scheduled task:

  [Email protected] ~]# CD/ETC/CRON.D [  [email protected] cron.d]# ls   0hourly  raid-check  sysstat  [ [Email protected] cron.d]# VI test

5, test file content as follows: (here is set to automatically back up every five minutes)

  */5 * * * * root/root/test.sh

6. Then execute the crontab command:

  [Email protected] cron.d]# crontab test

7, so you can, then ll see/root/admin can see every 5 minutes, admin This directory and the contents of the inside will change once

Second, non-root users:

1. Environment: MongoDB Directory:/home/tuzongxun/mongodb306/mongodb305/bin

2. Create admin directory, create script test.sh and edit:

   [Email protected] ~]# mkdir/root/admin
   [Email protected] ~]# VI test.sh

3. The contents of test.sh file are as follows:

   Rm-rf/home/tuzongxun/admin;   /home/tuzongxun/mongodb306/mongodb305/bin/mongodump--port 27017-u admin-p admin-d admin-o  /home/tuzongxun/ Admin ~ ~ ~ ~--   INSERT--

4. Then set the scheduled task:

  [Email protected] ~]# CD/ETC/CRON.D  [[email protected] cron.d]# ls   0hourly  raid-check  sysstat test  [email protected] cron.d]# VI test111

5, test111 file content as follows: (here is set to automatically back up every five minutes)

   */5 * * * * tuzongxun/home/tuzongxun/test.sh

6, the need to note here is: If the database used here is just a root user operation, then there are some places need to modify, otherwise the task will not be executed.

For example: 1,/tmp MONGO the beginning of those files, as well as the MONGODB data storage directory journal directory and j._0 files; change user permissions, otherwise you will not be able to start MongoDB with non-root users

2, in/etc to change the Cron.allow file, in the inside to add users need to use, such as: Tuzongxun;

7. Execute crontab Command:

   [Email protected] cron.d]# crontab test111

Three, more than two operations, the test.sh file is not an executable file by default, you need to give executable permissions.

Before using the crontab timer operation, in order to verify that the test.sh file is performing correctly and efficiently, you can do it manually once, and then use the scheduled task again if you are sure that it is not a problem.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implement mongodb3.0.5 database automatic backup in Linux system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.