Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to back up MongoDB automatically through Shell script under Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian introduces in detail "how to back up MongoDB automatically through Shell script under Linux", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to automatically back up MongoDB through Shell script under Linux" can help you solve your doubts.

Log in to the MongoDB database using the administrator account:

[root@renwolecomdbs /] $mongoMongoDB shell version v3.4.9connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.4.9 > use adminswitched to db admin Authentication > db.auth ("admin", "KM5d9KhJcW7O0lIAT")

> db.createUser ({user: "renwolebackup", pwd: "RenwoleComACC", roles: [{role: "backup", db: "admin"}]}) > exit

Add the following content to $mkdir-p / etc/init.d/mongo$ mkdir-p / apps/data/mongobackup_db$ vim / etc/init.d/mongo/mongo-bak: #! / bin/bash sourcepath=''targetpath='/apps/data/mongobackup_db'nowtime=$ (date +% Y%m%d) start () {${sourcepath} mongodump-- host 127.0.1 renwolebackup 27017-u renwolebackup-p renwolebackup-- out ${targetpath} / ${nowtime}} execute () {start If [$?-eq 0] then echo "back successfully!" Else echo "back failure!" Fi} if [!-d "${targetpath} / ${nowtime} /"] thenmkdir ${targetpath} / ${nowtime} fiexecuteecho "= back end ${nowtime} ="

In the above script, please note that the path and folder are modified according to your own needs.

Note: the above parameters are my own actual use, be sure to read the settings in the code carefully before using it.

If the above code is in incorrect format, please use Notepad++ to convert it to Unix format under Windwos, as follows:

Edit-document format conversion-convert to unix format.

Delete a backup that is 7 days overdue

$vim / etc/init.d/mongo/mongo-delete adds the following deletion content: #! / bin/bash targetpath='/apps/data/mongobackup_db'nowtime=$ (date-d'- 7 days' "+% Y%m%d") if [- d "${targetpath} / ${nowtime} /"] then rm-rf "${targetpath} / ${nowtime} /" echo "= ${targetpath} / ${nowtime} / = = deleted successfully =" fiecho "= = $nowtime= =" set permissions

You cannot run the script if you do not give execute permission:

$cd / etc/init.d/mongo$ chmod + x mongo-bak$ chmod + x mongo-delete add scheduled tasks

To achieve daily backups, set up a task schedule:

$mkdir-p / var/log/mongobaklog$ crontab-e adds the following automatic backup and delete backup scripts: 30 03 * / etc/init.d/mongo/mongo-bak 1 > / var/log/mongobaklog/mongo-bak.log & 30 01 * / etc/init.d/mongo/mongo-delete 1 > / var/log/mongobaklog/mongo-delete &

The time shown above is the daily execution time of 3.30 am and 1.30 am, respectively.

Where mongo-bak and mongo-delete are the names of the script, respectively. The following is the backup log. If there is any exception, you can query it through the log.

After reading this, the article "how to back up MongoDB automatically through Shell script under Linux" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report