MongoDB Backup (mongodump)
In MongoDB we use the Mongodump command to back up MongoDB data. The command can export all data to the specified directory.
The Mongodump command allows you to specify the server to which the exported data can be dumped by parameter.
Grammar
The mongodump command script syntax is as follows:
---o dbdirectory
- -H:
MONGDB the server address, for example: 127.0.0.1, you can also specify the port number: 127.0.0.1:27017
- -D:
The database instance that needs to be backed up, for example: test
- -O:
Backup data storage location, for example: C:\data\dump, of course, the directory needs to be established in advance, after the backup is completed, the system automatically establishes a test directory under the Dump directory, which holds the backup data of the database instance.
Grammar |
Description |
Example |
Mongodump--host host_name--port port_number |
This command will back up all MONGODB data |
Mongodump--host w3cschool.cc--port 27017 |
Mongodump--dbpath Db_path--out backup_directory |
|
Mongodump--dbpath/data/db/--out/data/backup/ |
Mongodump--collection Collection--db db_name |
The command backs up a collection of the specified databases. |
Mongodump--collection mycol--db test |
Note:
1. Before the backup, the Mongod service process is started
2. If it is initiated with the authentication--auth, the account and password that need to have the backup permission can be backed up
2015-12-18t11:32:54.422+0800 Failed:error Getting database Names:not authorized on Admin to execute command {Listdat Abases:1}
MongoDB Data Recovery
MongoDB uses the Mongorerstore command to restore the backed up data.
Grammar
The mongorestore command script syntax is as follows:
>----directoryperdb dbdirectory
- -H:
MongoDB server Address
- -D:
A database instance that needs to be recovered, for example: test, and of course the name can be different from the backup time, such as Test2
- --directoryperdb:
Where to back up your data, for example: C:\data\dump\test, why add a test here instead of a dump at backup time, and read the tips yourself!
- --drop:
When recovering, delete the current data and then restore the backed up data. That is, after the recovery, add the modified data after the backup will be deleted, use with caution Oh!
Mongoexport Export a table, or some field in a table
1, Common command format
1 |
mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --csv -o 文件名 |
The above parameters are good to understand, focus on:
-F export refers to field, split by font size,-f name,email,age export name,email,age three fields
-Q can export the root query condition,-Q ' {"UID": "100"} ' exports the data with UID 100
--csv means that the exported file format is CSV, which is useful because most of the relational databases are supported by CSV, and there's something in common here.
2, export the whole sheet
?
123 |
[[email protected] mongodb] # mongoexport -d tank -c users -o /home/zhangy/mongodb/tank/users.dat connected to: 127.0.0.1 exported 4 records |
3. Export some fields from the table
?
123 |
[[email protected] mongodb] # mongoexport -d tank -c users --csv -f uid,name,sex -o tank/users.csv connected to: 127.0.0.1 exported 4 records |
4, according to the conditions to dare to out data
?
123 |
[[email protected] mongodb] # mongoexport -d tank -c users -q ‘{uid:{$gt:1}}‘ -o tank/users.json connected to: 127.0.0.1 exported 3 records |
Mongoimport Import a table, or some field in a table
1, Common command format
1.1. Restore a non-CSV file exported from the whole table
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsert--drop file name
Focus on--upsert, the other arguments above have been mentioned,--upsert insert or update existing data
1.2. Restore the export file for some fields
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsertfields field--drop file name
--upsertfields Root--upsert like
1.3. Restore the exported CSV file
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--type type--headerline--upsert--drop file name
In the above three cases, there can be other permutations and combinations.
2. Restore the exported table data
?
123 |
[[email protected] mongodb] # mongoimport -d tank -c users --upsert tank/users.dat connected to: 127.0.0.1 Tue Dec 3 08:26:52.852 imported 4 objects |
3. Table data import for some fields
[Email protected] mongodb]# mongoimport-d tank-c users--upsertfields uid,name,sex tank/users.dat
Connected to:127.0.0.1
Tue DEC 3 08:31:15.179 imported 4 objects
4. Restore the CSV file
123 |
[[email protected] mongodb]# mongoimport -d tank -c users --type csv --headerline --file tank/users.csv connected to: 127.0.0.1 Tue Dec 3 08:37:21.961 imported 4 objects |
automatically backs up the MongoDB database under Linux and deletes backups that refer to the number of days before the work is scheduled
Specific operation:
1. Creating a MongoDB Database backup directory
Mkdir-p/home/backup/mongod_bak/mongod_bak_now
Mkdir-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/sh
Dump=/usr/local/mongodb/bin/mongodump #mongodump备份文件执行路径
Out_dir=/home/backup/mongod_bak/mongod_bak_now #临时备份目录
Tar_dir=/home/backup/mongod_bak/mongod_bak_list #备份存放路径
Date= ' Date +%y_%m_%d ' #获取当前系统时间
Db_user=username #数据库账号
db_pass=123456 #数据库密码
Days=7 #DAYS = 7 means to delete a backup 7 days ago, that is, only the last 7 days of backup are retained
tar_bak= "Mongod_bak_$date.tar.gz" #最终保存的数据库备份文件名
CD $OUT _dir
RM-RF $OUT _dir/*
Mkdir-p $OUT _dir/$DATE
$DUMP-u $DB _user-p $DB _pass-o $OUT _dir/$DATE #备份全部数据库
TAR-ZCVF $TAR _dir/$TAR _bak $OUT _dir/$DATE #压缩为. tar.gz format
Find $TAR _dir/-mtime + $DAYS-delete #删除7天前的备份文件
: wq! #保存退出
System operation and maintenance www.osyunwei.com warm reminder: qihang01 original Content © Copyright, reproduced please indicate the source and the original link
3. Modify the file properties to make it executable
chmod +x/home/crontab/mongod_bak.sh
4, modify the/etc/crontab #添加计划任务
Vi/etc/crontab #在下面添加
1 * * * root/home/crontab/mongod_bak.sh #表示每天凌晨1点30执行备份
5. Restart the Crond to make the settings effective
/etc/rc.d/init.d/crond restart
Chkconfig Crond on #设为开机启动
Service Crond Start #启动
You can see mongod_bak_2015_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/2015_02_28/
To restore 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 recovering data, avoid duplication of data.
--DIRECTORYPERDB parameter: Database backup directory
-D parameter: followed by the name of the database to be recovered
Ext.: http://www.osyunwei.com/archives/8985.html
MongoDB Backup (mongodump) recovery (mongorerstore) export (mongoexport) import (Mongoimport)