MongoDB scheduled backup

Source: Internet
Author: User
Tags mongodump mongorestore

Perform backup operations with CentOS Footstep, use crontab to implement timing functions, and delete backups up to a specified number of days

Specific operation:

1. Creating a MongoDB Database backup directory

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

2. New MongoDB Database backup script

Vi/home/crontab/mongod_bak.sh #新建文件, enter the following code

#!/bin/shDUMP=/usr/local/mongodb/bin/mongodump#mongodump backup File Execution pathOut_dir=/home/backup/mongod_bak/mongod_bak_now#Temporary backup directoryTar_dir=/home/backup/mongod_bak/mongod_bak_list#Backup Storage PathDATE= ' Date +%y_%m_%d '#get current system timeDb_user=username#Database AccountDb_pass=123456#Database Password Days=7#Days=7 represents the deletion of a backup 7 days ago, that is, only the last 7 days of backupTar_bak="mongod_bak_$date.tar.gz" #The final saved database backup file nameCD$OUT _dirRM-rf$OUT _dir/*mkdir-P$OUT _dir/$DATE$DUMP-U$DB _user-P$DB _pass-O$OUT _dir/$DATE #back up all databasesTar-zcvf$TAR _dir/$TAR _bak $OUT _dir/$DATE #compress to. tar.gz formatFind$TAR _dir/-mtime +$DAYS-delete#Delete backup files from 7 days ago

3. Modify the file properties to make it executable

chmod +x/home/crontab/mongod_bak.sh

4, modify the/etc/crontab #添加计划任务

Crontab-e

#在下面添加

# indicates a backup is performed 1 o'clock in the morning 30 per day

5. Restart the Crond to make the settings effective

# Reload configuration chkconfig--level crond on  # join Power on auto start:/sbin/service crond start   #  Start service # list crontab files

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

Compress the file.

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

Appendix: MongoDB Database Recovery

Recover all databases:

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

To 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 recovering data, avoid duplication of data.

–DIRECTORYPERDB parameter: Database backup directory

-D parameter: followed by the name of the database to be recovered

about crontab:

The crontab command is commonly used in Unix-and Unix-like operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek language Chronos (χρ?νο), which was originally meant to be time.
Typically, crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be executed. This type of work is generally called cron jobs.

Install Crontab:

[[email protected] ~]# yum install vixie-cron[[email protected] ~]# Yum install Crontabs

Description
Vixie-cron package is the main program of Cron;
The Crontabs package is a program used to install, uninstall, or list tables used to drive the cron daemon.

Cron is a built-in service for Linux, but it does not automatically get up, and you can start and shut down this service in the following ways:

# Start the service # Close Service # Restart Service # Reload Configuration

View Crontab Service Status:

Service Crond Status

To start the Crontab service manually:

Service Crond Start

Other commands:

#To see if the crontab service is set to boot, execute the command:Ntsysv#Add to boot auto start:Chkconfig--level 35Crond on#list crontab FilesCrontab-L#Edit crontab fileCrontab-e#Delete crontab file$ crontab-R#recovering a lost crontab file#If you have a backup in your $home directory, you can copy it to/var/spool/cron/<username>, where <username > is the username#or use the following command where,<filename> is the file name of your copy in the $home directoryCrontab <filename>

MongoDB scheduled backup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.