In linux, mongodb3.0.5 and linuxmongodb3.0.5 are automatically backed up.
In the last two days, data in the mongodb database should be backed up regularly due to the company's business needs.
After checking a lot of information, I found that mongodb does not seem to have its own scheduled backup function, so I had to transfer the target to the scheduled task of the linux system, so I learned and used crontab, and scheduled backup is successful.
Reference: 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 divided into two steps. One is under the root user, and the other is not the root user. Because mongodb on the test machine is used as the root user, you can directly perform operations in a confused manner at the beginning.
1. root User:
1. Environment: redhet 6.3
Mongodb installation directory:/mongodb305/mongodb305/bin
The database admin, username admin, and password admin exist in mongdb.
2. First, write a mongodb backup script test. sh and create a directory admin under the root directory:
[root@213 ~]# vi test.sh
[root@213 ~]# mkdir /root/admin
3. The content of the test. sh file is as follows)
rm -rf /root/admin; /mongodb305/mongodb305/bin/mongodump --port 27017 -u admin -p admin -d admin -o /root/admin ~ ~ ~ ~ ~ -- INSERT --
4. Set the scheduled task:
[root@213 ~]# cd /etc/cron.d [root@213 cron.d]# ls 0hourly raid-check sysstat [root@213 cron.d]# vi test
5. The content of the test file is as follows: (the file is automatically backed up every five minutes)
*/5 * * * * root /root/test.sh
6. Run the crontab command:
[root@213 cron.d]# crontab test
7. In this way, you can view/root/admin in ll to see that the admin directory and content will change once every 5 minutes.
Ii. Non-root users:
1. Environment: mongodb Directory:/home/tuzongxun/mongodb306/mongodb305/bin
2. Create the admin directory, create the Script test. sh, and edit it:
[tuzongxun@213 ~]# mkdir /root/admin
[tuzongxun@213 ~]# vi test.sh
3. The content of the test. sh file is 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. Set the scheduled task:
[root@213 ~]# cd /etc/cron.d [root@213 cron.d]# ls 0hourly raid-check sysstat test [root@213 cron.d]# vi test111
5. The content of the test111 file is as follows: (the file is automatically backed up every five minutes)
*/5 * * * * tuzongxun /home/tuzongxun/test.sh
6. Note that if the database used here is operated by the root user just now, some changes need to be made. Otherwise, the task cannot be executed.
Example: 1. Files starting with mongo under/tmp, the journal directory under the mongodb data storage directory, and j. _ 0 file; User Permissions must be changed; otherwise, you cannot start mongodb with a non-root user.
2. You need to change the cron. allow file in/etc and add the users you want to use, such as tuzongxun;
7. Run the crontab command:
[tuzongxun@213 cron.d]# crontab test111
3. When the preceding two operations are performed, the test. sh file is not an executable file by default, and the executable permission must be granted.
Before using crontab for scheduled operations, you can manually run the task once to verify that the test. sh file can be correctly and effectively executed.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.