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