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 realize automatic backup of MongoDB Database under Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "how to achieve automatic backup of MongoDB database under Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn how to achieve automatic backup of MongoDB database under Linux "this article.

First, create a MongoDB backup directory

Mkdir-p / data/mongodb_bak/mongodb_bak_nowmkdir-p / data/mongodb_bak/mongodb_bak_list

Create a new MongoDB database backup script (/ data/mongodb_bak/MongoDB_bak.sh)

#! / bin/bash # backup MongoDB # mongodump command path DUMP=/usr/local/mongodb/bin/mongodump # temporary backup directory OUT_DIR=/data/mongodb_bak/mongodb_bak_now # backup storage path TAR_DIR=/data/mongodb_bak/mongodb_bak_list # get the current system time DATE= `dat e +% y% masks% d` # database account DB_USER=user # database password DB_PASS=123 # DAYS=15 represents the backup 15 days before deletion That is, only keep the backup DAYS=15 # for nearly 15 days. The final saved database backup file TAR_BAK= "mongodb_bak_$DATE.tar.gz" cd $OUT_DIR rm-rf $OUT_DIR/* mkdir-p $OUT_DIR/$DATE # backs up all databases $DUMP-h 15.62.32.112mongodb_bak_$DATE.tar.gz 27017-u $DB_USER-p $DB_PASS-- authenticationDatabase "admin"-o $OUT_DIR/$DATE # compressed to .tar.gz format tar-zcvf $TAR_DIR/ $TAR_BAK $OUT_DIR/$DATE # delete the backup file find $TAR_DIR/-mtime + $DAYS-delete exit that was backed up 15 days ago

Modify the attributes of the file to make it executable

Chmod + x MongoDB_bak.sh

4. Modify / etc/crontab and add scheduled tasks

Vi / etc/crontab# starts to execute the MongoDB database backup script 3020 * * 6 root / data/mongodb_bak/MongoDB_bak.sh every Saturday night at 20:30

Appendix: MongoDB database recovery

# restore all databases mongorestore-u $DB_USER-p $DB_PASS-- authenticationDatabase "admin"-- noIndexRestore-- dir / data/mongodb_bak/mongodb_bak_now/2016_12_17/# restore a single database mongorestore-u $DB_USER-p $DB_PASS-- authenticationDatabase "admin"-- noIndexRestore-d dbname-- dir / data/mongodb_bak/mongodb_bak_now/2016_12_17/dbname

Description of some parameters

-- drop parameter: delete the original data before restoring the data to avoid data duplication

-- noIndexRestore parameter: no index is created when the data is recovered

-- dir parameter: database backup directory

-d parameter: followed by the name of the database to be restored

The above is all the contents of the article "how to achieve automatic backup of MongoDB database under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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

Database

Wechat

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

12
Report