Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize automatic backup of MongoDB Database under Linux

Shulou Source: shulou.com Published: 2022-05-31 21:12:27 11月17日 Update

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!

Tags: Data backup database parameters content file directory article script path learning help representative task single name command password attribute time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Microsoft OPPO Reno Shulou Information