mongodb备份和恢复
--------------------------备份--------------------------------------
1.备份语句
/usr/local/services/mongodb/bin/mongodump -h 172.17.70.190:28001 -u root -p 123456 --authenticationDatabase mgdb_livex --db=mgdb_livex -c ${table_name} -o /home/hxl/mongo_exportdata/${table_name}_${dump_date}
--------------------------恢复--------------------------------------
1.备份文件上传到服务器/tmp目录,然后进行解压
[root@localhost tmp]# tar -xvf test1.tar.gz
[root@localhost tmp]# tar -xvf test2.tar.gz
解压后会生成目录
[root@localhost tmp]# ls -1
test1
test2
目录下是库名目录,库名下面是具体的数据文件
[root@localhost mgdb_livex]# pwd
/tmp/test1/mgdb_livex
[root@localhost mgdb_livex]# ls -l
total 17360
-rw-r--r-- 1 test test 17768550 Sep 7 02:10 test1.bson
-rw-r--r-- 1 test test 536 Sep 7 02:10 test1.metadata.json
将数据恢复到另外的库:dbtest
/usr/local/services/mongodb/bin/mongorestore --host 127.0.0.1 --port 28001 -u root -p 123456 --authenticationDatabase admin -d dbtest /tmp/t_chartroom_msg_20210907/mgdb_livex
/usr/local/services/mongodb/bin/mongorestore --host 127.0.0.1 --port 28001 -u root -p 123456 --authenticationDatabase admin -d dbtest /tmp/t_chartroom_msg_client_20210907/mgdb_livex
说明:
恢复不需要提前创建数据库dbtest
###################################例子############################
环境:
OS:Centos 7
DB:4.4.13
1.恢复导入
说明:db_test库不需要提前创建,库大小:115GB
将数据恢复到另外的库:db_test
[root@hxl-biz-dgf6wfs-prd bin]#/usr/local/services/mongodb-database-tools/bin/mongorestore --host 127.0.0.1 --port 29109 -u hxl -p hxl123 --authenticationDatabase admin -d db_test /home/hxl/tb_test_record_20221212/mgdb_msg
2022-12-13T09:50:59.326+0800 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2022-12-13T09:50:59.326+0800 building a list of collections to restore from /home/hxl/tb_test_record_20221212/mgdb_msg dir
2022-12-13T09:50:59.326+0800 reading metadata for db_test.tb_test_record from /home/hxl/tb_test_record_20221212/mgdb_msg/tb_test_record.metadata.json
2022-12-13T09:50:59.350+0800 restoring db_test.tb_test_record from /home/hxl/tb_test_record_20221212/mgdb_msg/tb_test_record.bson
2022-12-13T09:51:02.326+0800 [........................] db_test.tb_test_record 58.9MB/115GB (0.0%)
2022-12-13T09:51:05.326+0800 [........................] db_test.tb_test_record 118MB/115GB (0.1%)
2022-12-13T09:51:08.326+0800 [........................] db_test.tb_test_record 176MB/115GB (0.1%)
2022-12-13T09:51:11.327+0800 [........................] db_test.tb_test_record 233MB/115GB (0.2%)
2022-12-13T09:51:14.326+0800 [........................] db_test.tb_test_record 291MB/115GB (0.2%)
2022-12-13T09:51:17.326+0800 [........................] db_test.tb_test_record 349MB/115GB (0.3%)
2022-12-13T09:51:20.326+0800 [........................] db_test.tb_test_record 407MB/115GB (0.3%)
2022-12-13T09:51:23.326+0800 [........................] db_test.tb_test_record 463MB/115GB (0.4%)
2022-12-13T09:51:26.326+0800 [........................] db_test.tb_test_record 523MB/115GB (0.4%)
2022-12-13T09:51:29.327+0800 [........................] db_test.tb_test_record 564MB/115GB (0.5%)
2022-12-13T09:51:32.326+0800 [........................] db_test.tb_test_record 623MB/115GB (0.5%)
2022-12-13T09:51:35.326+0800 [........................] db_test.tb_test_record 675MB/115GB (0.6%)
2022-12-13T09:51:38.326+0800 [........................] db_test.tb_test_record 733MB/115GB (0.6%)
2022-12-13T09:51:41.326+0800 [........................] db_test.tb_test_record 792MB/115GB (0.7%)
2022-12-13T11:35:23.326+0800 [#######################.] db_test.tb_test_record 114GB/115GB (99.4%)
2022-12-13T11:35:26.326+0800 [#######################.] db_test.tb_test_record 114GB/115GB (99.5%)
2022-12-13T11:35:29.326+0800 [#######################.] db_test.tb_test_record 114GB/115GB (99.5%)
2022-12-13T11:35:32.326+0800 [#######################.] db_test.tb_test_record 114GB/115GB (99.6%)
2022-12-13T11:35:35.326+0800 [#######################.] db_test.tb_test_record 115GB/115GB (99.6%)
2022-12-13T11:35:38.326+0800 [#######################.] db_test.tb_test_record 115GB/115GB (99.7%)
2022-12-13T11:35:41.327+0800 [#######################.] db_test.tb_test_record 115GB/115GB (99.7%)
2022-12-13T11:35:44.326+0800 [#######################.] db_test.tb_test_record 115GB/115GB (99.8%)
2022-12-13T11:35:47.326+0800 [#######################.] db_test.tb_test_record 115GB/115GB (99.8%)
2022-12-13T11:35:50.326+0800 [#######################.] db_test.tb_test_record 115GB/115GB (99.8%)
2022-12-13T11:35:53.326+0800 [#######################.] db_test.tb_test_record 115GB/115GB (99.9%)
2022-12-13T11:35:56.326+0800 [#######################.] db_test.tb_test_record 115GB/115GB (99.9%)
2022-12-13T11:35:59.326+0800 [#######################.] db_test.tb_test_record 115GB/115GB (100.0%)
2022-12-13T11:36:00.441+0800 [########################] db_test.tb_test_record 115GB/115GB (100.0%)
2022-12-13T11:36:00.441+0800 finished restoring db_test.tb_test_record (650385542 documents, 0 failures)
2022-12-13T11:36:00.441+0800 restoring indexes for collection db_test.tb_test_record from metadata
2022-12-13T11:36:00.441+0800 index: &idx.IndexDocument{Options:primitive.M{"name":"idx_biz_msgid", "v":2}, Key:primitive.D{primitive.E{Key:"biz", Value:1}, primitive.E{Key:"msg_id", Value:-1}}, PartialFilterExpression:primitive.D(nil)}
2022-12-13T11:36:00.441+0800 index: &idx.IndexDocument{Options:primitive.M{"expireAfterSeconds":7776000, "name":"idx_expire", "v":2}, Key:primitive.D{primitive.E{Key:"create_time", Value:1}}, PartialFilterExpression:primitive.D(nil)}
2022-12-13T11:36:00.441+0800 index: &idx.IndexDocument{Options:primitive.M{"name":"idx_update_time", "v":2}, Key:primitive.D{primitive.E{Key:"update_time", Value:-1}}, PartialFilterExpression:primitive.D(nil)}
数据导入完成后会自动自动导入索引
数据导入时间:1小时40分钟
创建索引时间:1小时20分钟
2.查看索引
[root@hxl-biz-dgf6wfs-prd data]# /usr/local/services/mongodb/bin/mongo localhost:29109
>use admin
>db.auth("hxl","hxl123");
> use db_test;
switched to db db_test
> db.tb_test_record.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"biz" : 1,
"msg_id" : -1
},
"name" : "idx_biz_msgid"
},
{
"v" : 2,
"key" : {
"create_time" : 1
},
"name" : "idx_expire",
"expireAfterSeconds" : NumberLong(7776000)
},
{
"v" : 2,
"key" : {
"update_time" : -1
},
"name" : "idx_update_time"
}
]
3.查看记录数
> db.tb_test_record.find().count();
638773994
-- The End --