In CentOS, Mongodb automatically backs up and deletes the backup that was created several days ago.

Source: Internet
Author: User
Tags mkdir mongodb centos mongodump mongorestore


Note:

Mongodb database server

Operating system: CentOS

Mongodb database version: mongodb-linux-x86_64-2.6.x

Specific operations:

1. Create a Mongodb database backup directory

Mkdir-p/home/backup/mongod_bak/mongod_bak_now

Mkdir-p/home/backup/mongod_bak/mongod_bak_list

2. Create a Mongodb database backup script

Vi/home/crontab/cmdd_bak.sh # Create a file and enter the following code:

#! /Bin/sh

DUMP =/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 _ % m _ % d' # obtain the current system time

DB_USER = username # Database account

DB_PASS = 123456 # database password

DAYS = 7 # DAYS = 7 indicates that the backup created seven DAYS ago is deleted, that is, only the backup of the last seven DAYS is retained.

TAR_BAK = "mongod_bak_mongodate.tar.gz" # Name of the final database backup file

Cd $ OUT_DIR

Rm-rf $ OUT_DIR /*

Mkdir-p $ OUT_DIR/$ DATE

$ DUMP-u $ DB_USER-p $ DB_PASS-o $ OUT_DIR/$ DATE # back up all databases

Tar-zcvf $ TAR_DIR/$ TAR_BAK $ OUT_DIR/$ DATE compressed into .tar.gz format

Find $ TAR_DIR/-mtime + $ DAYS-delete # delete the backup file seven DAYS ago

: Wq! # Save and exit

3. Modify file attributes to make them executable.

Chmod + x/home/crontab/mongod_bak.sh

4. Modify/etc/crontab # Add a scheduled task

Vi/etc/crontab # Add

30 1 * root/home/crontab/mongod_bak.sh # indicates that backup is performed at every day.

5. Restart crond to make the settings take effect.

/Etc/rc. d/init. d/crond restart

Chkconfig crond on # set to boot

Service crond start # start

In the/home/backup/mongod_bak/mongod_bak_listdirectory every day, you can see the contents such as mongod_bak_2015_02_28.tar.gz.

Compressed file.

Now, the Mongodb database is automatically backed up in Linux and the backup is completed several days ago.

Appendix: Mongodb database restoration

Restore all databases:

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

Recover a single database:

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

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

-- Directoryperdb parameter: database backup directory

-D parameter: name of the database to be restored

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.