1,mongodb所有组件
官方文档地址:https://docs.mongodb.com/manual/reference/command/,所有的基础组件都在里面,包括备份恢复的mongodump、mongorestore,如01.png所示:
2,备份组件mongodump 概要
mongodump is a utility for creating a binary export of the contents of a database. mongodump can export data from either mongod or mongos instances.
mongodump can be a part of a backup strategy with mongorestore for partial backups based on a query, syncing from production to staging or development environments, or changing the storage engine of a standalone. However, the use of mongodump and mongorestore as a backup strategy can be problematic for sharded clusters and replica sets.
# 翻译如下
Mongodump是将mongodb数据库内容以二进制日志的方式导出的组件函数。Mongodump和mongostore一起可以作为mongodb的备份策略的模块,可以同步生产环境到临时环境库以及开发环境库,或者也可以改变单机环境的存储引擎。然而作为备份策略方案mongodump和mongostore也能适用于cluster集群和replica-sets副本集。
Mongodump不备份索引数据,在做mongostore的时候会自动重建索引
mongodump can adversely affect performance of the mongod. Ifyour data is larger than system memory, themongodump will pushthe working set out of memory.
意味着如果数据所占据的容量远远大于系统内存,那么,mongodb将不在内存里面进行操作(字面理解)。
3,备份单个collection
mongodump --db test --collection collection
/usr/local/mongodb-linux-x86_64-3.0.3/bin/mongodump-hlocalhost --port 30000 -d parking -c enter -o /data/mongodb/0626/enter.csv
备份控制台信息:
[mongodb@db_mongodb_1 backup]$ /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongodump -hlocalhost --port 30000 -d parking -c enter -o /data/mongodb/0626/enter.csv
2016-06-26T21:57:31.408+0800 writing parking.enter to /data/mongodb/0626/enter.csv/parking/enter.bson
2016-06-26T21:57:33.858+0800 [........................] parking.enter 141864/4843440 (2.9%)
2016-06-26T21:57:36.855+0800 [#.......................] parking.enter 374518/4843440 (7.7%)
……………………
2016-06-26T21:58:21.855+0800 [#######################.] parking.enter 4654865/4843440 (96.1%)
2016-06-26T21:58:23.696+0800 writing parking.enter metadata to /data/mongodb/0626/enter.csv/parking/enter.metadata.json
2016-06-26T21:58:23.698+0800 done dumping parking.enter
[mongodb@db_mongodb_1 backup]$
4,备份sharding库
Mongodump可以在mongos上面进行备份,在后面添加mongos的端口就可以了,备份所有库命令如下:
# 创建备份目录
mkdir /data/mongodb/backup/20160626
# 开始备份
time /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongodump --host localhost:30000 -o /data/mongodb/backup/20160626
备份是多进程的,4个进程一起备份(N核就是N个进程),控制台显示信息:
# 备份控制台信息:
…..
2016-06-26T16:42:58.398+0800 [#.......................] pv.mobile 3184618/62143101 (5.1%)
2016-06-26T16:42:58.398+0800 [#####...................] traffice.passenger 4982012/23503868 (21.2%)
2016-06-26T16:42:58.398+0800 [........................] pv.stat 108114/3739398 (2.9%)
2016-06-26T16:42:58.398+0800 [###.....................] parking.leave 402109/2920264 (13.8%)
2016-06-26T16:42:58.398+0800
2016-06-26T16:43:01.397+0800 [#.......................] pv.mobile 3209372/62143101 (5.2%)
2016-06-26T16:43:01.397+0800 [#####...................] traffice.passenger 5222668/23503868 (22.2%)
2016-06-26T16:43:01.397+0800 [........................] pv.stat 124667/3739398 (3.3%)
2016-06-26T16:43:01.397+0800 [####....................] parking.leave 551075/2920264 (18.9%)
2016-06-26T16:43:01.397+0800
2016-06-26T16:43:04.396+0800 [#.......................] pv.mobile 3218224/62143101 (5.2%)
2016-06-26T16:43:04.396+0800 [#####...................] traffice.passenger 5461117/23503868 (23.2%)
2016-06-26T16:43:04.396+0800 [........................] pv.stat 141247/3739398 (3.8%)
2016-06-26T16:43:04.396+0800 [#####...................] parking.leave 706371/2920264 (24.2%)
……
备份结果统计:
# 备份完,大概40G,所花费时间大概20分钟
[mongodb@db_mongodb_1 backup]$ du -sh 20160626
41G 20160626
[mongodb@db_mongodb_1 backup]$
# 然后解压缩,大概需要20分钟
[mongodb@db_mongodb_1 backup]$ time tar -zcvf 20160626.tar.gz 20160626
……
20160626/ibeacon/usercoord.metadata.json
20160626/ibeacon/system.indexes.bson
20160626/ibeacon/usercoord.bson
real 19m48.689s
user 17m33.293s
sys 1m7.212s
[mongodb@db_mongodb_1 backup]$
5,备份用户登录验证
In the next example, mongodump creates a database dump located at /data/backup/mongodump-2016-06-26, from a database running on port 37017 on the host mongodb1.example.net and authenticating using the username user and the password pass,as follows:
mongodump --host mongodb1.example.net --port 37017 --username user --password pass --out /data/backup/mongodump-2016-06-26
6,压缩备份
To compress thefiles in the output dump directory, run mongodump with the new --gzip option. For example, the following operation outputs compressedfiles into the default dump directory.
mongodump --gzip --db test
PS:此mongodb版本必须不低于3.2:
--gzip¶
New in version 3.2.
Compresses the output. If mongodump outputs to the dump directory, the new feature compresses the individual files. The files have the suffix .gz.
If mongodump outputs to an archive file or the standard out stream, the new feature compresses the archive file or the data output to the stream.
7,设立自动备份任务
全面理解了mongodb的mongodump备份组件后,可以设置crontab任务做到每天自动备份mongodb数据,可以在shareding集群的:
# 编写简洁的备份脚本
[root@db_mongodb_1 ~]# more /data/mongodb/backup/full_backup.sh
#!/bin/sh
export DATE=`date +%F`
export BACK_DIR='/data/mongodb/backup'
su - mongodb -c "
mkdir -p $BACK_DIR/$DATE
/usr/local/mongodb-linux-x86_64-3.0.3/bin/mongodump --host localhost:30000 -o $BACK_DIR/$DATE
tar -zcvf $BACK_DIR/$DATE.tar.gz $BACK_DIR/$DATE
"
[root@db_mongodb_1 ~]#
# 设置备份任务
[root@db_mongodb_1 ~]# crontab -l
1 30 * * * sh /data/mongodb/backup/full_backup.sh
[root@db_mongodb_1 ~]#
---------------------
作者:忆平齐
来源:CSDN
原文:https://blog.csdn.net/mchdba/article/details/51765021
版权声明:本文为博主原创文章,转载请附上博文链接!