官方文檔地址:https://docs.mongodb.com/manual/reference/command/,所有的基礎組件都在里面,包括備份恢復的mongodump、mongorestore,如01.png所示:
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數據庫內容以2進制日志的方式導出的組件函數。Mongodump和mongostore1起可以作為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將不在內存里面進行操作(字面理解)。
mongodump --db test --collection collection
/usr/local/mongodb-linux-x86_64⑶.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⑶.0.3/bin/mongodump -hlocalhost --port 30000 -d parking -c enter -o /data/mongodb/0626/enter.csv 2016-06⑵6T21:57:31.408+0800 writing parking.enter to /data/mongodb/0626/enter.csv/parking/enter.bson 2016-06⑵6T21:57:33.858+0800 [........................] parking.enter 141864/4843440 (2.9%) 2016-06⑵6T21:57:36.855+0800 [#.......................] parking.enter 374518/4843440 (7.7%) …………………… 2016-06⑵6T21:58:23.696+0800 writing parking.enter metadata to /data/mongodb/0626/enter.csv/parking/enter.metadata.json 2016-06⑵6T21:58:23.698+0800 done dumping parking.enter [mongodb@db_mongodb_1 backup]$ |
Mongodump可以在mongos上面進行備份,在后面添加mongos的端口就能夠了,備份所有庫命令以下:
# 創建備份目錄 mkdir /data/mongodb/backup/20160626 # 開始備份 time /usr/local/mongodb-linux-x86_64⑶.0.3/bin/mongodump --host localhost:30000 -o /data/mongodb/backup/20160626 |
備份是多進程的,4個進程1起備份(N核就是N個進程),控制臺顯示信息:
# 備份控制臺信息: ….. 2016-06⑵6T16:42:58.398+0800 [#.......................] pv.mobile 3184618/62143101 (5.1%) 2016-06⑵6T16:42:58.398+0800 [#####...................] traffice.passenger 4982012/23503868 (21.2%) 2016-06⑵6T16:42:58.398+0800 [........................] pv.stat 108114/3739398 (2.9%) 2016-06⑵6T16:42:58.398+0800 [###.....................] parking.leave 402109/2920264 (13.8%) 2016-06⑵6T16:42:58.398+0800 2016-06⑵6T16:43:01.397+0800 [#.......................] pv.mobile 3209372/62143101 (5.2%) 2016-06⑵6T16:43:01.397+0800 [#####...................] traffice.passenger 5222668/23503868 (22.2%) 2016-06⑵6T16:43:01.397+0800 [........................] pv.stat 124667/3739398 (3.3%) 2016-06⑵6T16:43:01.397+0800 [####....................] parking.leave 551075/2920264 (18.9%) 2016-06⑵6T16:43:01.397+0800 2016-06⑵6T16:43:04.396+0800 [#.......................] pv.mobile 3218224/62143101 (5.2%) 2016-06⑵6T16:43:04.396+0800 [#####...................] traffice.passenger 5461117/23503868 (23.2%) 2016-06⑵6T16:43:04.396+0800 [........................] pv.stat 141247/3739398 (3.8%) 2016-06⑵6T16: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]$ |
In the next example, mongodump creates a database dump located at /data/backup/mongodump⑵016-06⑵6, 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:
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.
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. |
全面理解了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⑶.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 ~]# |
上一篇 Transition的使用分析