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 use the crontab command to back up mongodb regularly in centos

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article introduces how to use the crontab command in centos to back up mongodb regularly. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. Create a backup directory for Mongodb database

Mkdir-p / home/backup/mongod_bak/mongod_bak_nowmkdir-p / home/backup/mongod_bak/mongod_bak_list

2. Create a new Mongodb database backup script

Vi / home/crontab/mongod_bak.sh # create a new file and enter the following code

#! / bin/shDUMP=/usr/local/mongodb/bin/mongodump # mongodump backup file execution path OUT_DIR=/home/backup/mongod_bak/mongod_bak_now # temporary backup directory TAR_DIR=/home/backup/mongod_bak/mongod_bak_list # backup storage path DATE= `date +% Y% masks% d` # get the current system time DB_USER=username # database account DB_PASS=123456 # database password DAYS=7 # DAYS=7 represents the backup 7 days before deletion That is, only the last 7-day backup TAR_BAK= "mongod_bak_$DATE.tar.gz" # final saved database backup file name cd $OUT_DIRrm-rf $OUT_DIR/*mkdir-p $OUT_DIR/$DATE$DUMP-u $DB_USER-p $DB_PASS-o $OUT_DIR/$DATE # backup all databases tar-zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE # compressed into .tar.gz format find $TAR_DIR/-mtime + $DAYS-delete # delete backup files 7 days ago

3. Modify the file properties to make it executable

Chmod + x / home/crontab/mongod_bak.sh

4. Modify / etc/crontab # to add scheduled tasks

Crontab-e

Add below

301 * root / home/crontab/mongod_bak.sh # means that backups are performed at 1: 30 a.m. Every day.

5. Restart crond to make the settings take effect

/ sbin/service crond reload # reload configuration chkconfig-- level 35 crond on # join Boot Auto start: / sbin/service crond start # Startup Service crontab-l # list crontab file

You can see compressed files like mongod_bak_2017_02_28.tar.gz in the / home/backup/mongod_bak/mongod_bak_list directory every day.

At this point, Linux automatically backs up the Mongodb database and deletes the backup before the specified number of days.

Mongodb restores backups regularly

Restore all databases:

Mongorestore-drop-directoryperdb / home/backup/mongod_bak/mongod_bak_now/2017_02_28/

Restore a single database:

Mongorestore-drop-d dataname-directoryperdb / home/backup/mongod_bak/mongod_bak_now/2017_02_28/dataname

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

-directoryperdb parameter: database backup directory

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

Crontab command:

Crontab commands are common in Unix and Unix-like operating systems and are used to set instructions that are executed periodically. This command reads instructions from a standard input device and stores them in a "crontab" file for later reading and execution. The word comes from the Greek chronos, which originally means time.

Typically, instructions stored in crontab are activated by daemons, and crond is often run in the background, checking every minute to see if there are scheduled jobs to be executed. This type of assignment is commonly referred to as cron jobs.

Install crontab:

[root@CentOS ~] # yum install vixie-cron [root@CentOS ~] # yum install crontabs

Description:

Vixie-cron package is the main program of cron

The crontabs package is a program that installs, uninstalls, or enumerates tables used to drive cron daemons.

Cron is a built-in service for linux, but it does not start automatically. You can start and shut down this service in the following ways:

/ sbin/service crond start # start service / sbin/service crond stop # shut down service / sbin/service crond restart # restart service / sbin/service crond reload # reload configuration

View the status of the crontab service:

Service crond status

Start the crontab service manually:

Service crond start

Other commands:

# check whether the crontab service is set to boot, execute the command: ntsysv# join boot auto start: chkconfig-- level 35 crond on# list crontab file crontab-l# edit crontab file crontab-e# delete crontab file $crontab-r # recover the missing crontab file # assuming you have a backup in your $HOME directory, you can copy it to / var/spool/cron/, where is the user name # or use the following command Is your copy in the $HOME directory file name crontab on how to use the crontab command in centos for regular backup of mongodb to share here, I hope the above content can be of some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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