1. 程式人生 > 其它 >mongodb備份和恢復

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