1. 程式人生 > >mongodb 通過mongodump來備份Sharded Cluste分片集群

mongodb 通過mongodump來備份Sharded Cluste分片集群

原創 適用於 run default word ren con lin 重建

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

備份控制臺信息:

[[email protected]_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

[[email protected]_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分鐘

[[email protected]_mongodb_1 backup]$ du -sh 20160626

41G 20160626

[[email protected]_mongodb_1 backup]$

# 然後解壓縮,大概需要20分鐘

[[email protected]_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

[[email protected]_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集群的:

# 編寫簡潔的備份腳本

[[email protected]_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

"

[[email protected]_mongodb_1 ~]#

# 設置備份任務

[[email protected]_mongodb_1 ~]# crontab -l

1 30 * * * sh /data/mongodb/backup/full_backup.sh

[[email protected]_mongodb_1 ~]#
---------------------
作者:憶平齊
來源:CSDN
原文:https://blog.csdn.net/mchdba/article/details/51765021
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

mongodb 通過mongodump來備份Sharded Cluste分片集群