Hướng dẫn save mongodb - lưu mongodb

Hướng dẫn sao lưu, khôi phục data mongo (mongodump, mongorestore)

Trong bài này mình sẽ hướng dẫn sao lưu, khôi phục data của mongodb một cách đơn giản nhất.

Mongo hỗ trợ 2 cách để sao lưu, khôi phục data là mongodump/mongorestore với mongoimport/mongoexport tuy nhiên mongoimport/mongoexport chỉ dùng cho trường hợp data nhỏ nên ở đây mình chỉ đề cập tới mongodump/mongorestore.

1. Ví dụ mongodump

Để sao lưu data thành file .gz ta dùng câu lệnh sau:

mongodump --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection} --archive={filename.gz} --gzip --query=

(lệnh mongodump còn nhiều tùy chọn khác, tuy nhiên ở đây mình chỉ đưa ra các lựa chon đơn giản nhất để có thể sao lưu data).

Trong đó:  --host={host:port} --authenticationDatabase={authenticationDatabase} -u={username} -p={password} là thông tin của database mà bạn cần backup lại

  • default host:port sẽ là localhost:27017 nên nếu bạn chạy ở local thì có thể bỏ qua option --host
  • nếu bạn ko cài password cho database thì cũng có thể bỏ qua các option --authenticationDatabase-u, -p
  • mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
    0: chỉ rõ database bạn muốn backup, nếu ko chọn thì mặc định nó sẽ sao lưu lại tất cả database trên host
  • mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
    1: chỉ rõ collection bạn muốn backup, nếu bỏ qua thì nó sẽ sao lưu tất cả collection trong database
  • mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
    2: để chọn backup ra file
    mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
    3 (nếu ko có sẽ lưu lại thành folder với nhiều mục nhỏ)
  • mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
    4: dùng để tùy chọn các bản ghi được backup, ví dụ chỉ chọn backup các bản ghi có tên là ‘xyz’…

Lưu ý: khi sử dụng

mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
5 trên window thường hay bị lỗi parse json nên người ta hay dùng 
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
6. Với
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
7 là đường dẫn tới file json chứa thông tin query, cách này vừa hạn chế lỗi, vừa cho phép các câu query nâng cao).

2. Ví dụ mongorestore

Để khôi phục (restore) data từ file .gz được backup bằng lệnh mongodump, ta sử dụng lệnh mongorestore với cú pháp như sau:

mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip

Nếu bạn ko chỉ rõ

mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
0 và
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --db={database} --collection={collection}  --archive={filename.gz} --gzip
9 thì nó sẽ restore lại tất cả data trong file .gz, theo đúng database name và collection name đã backup

Trường hợp tên database hoặc tên collection ở nơi bạn muốn restore khác với tên lúc backup thì ta sử dụng option

mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
0 và
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
1 như sau:

mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}

Demo

Ví dụ mình muốn chuyển data từ collection

mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
2 từ database
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
3 sang collection
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
2 của database
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
5

Hướng dẫn save mongodb - lưu mongodb

Thực hiện backup lại data của collection

mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
2 của của database
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
3 thành file
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
8

mongodump --db=store_1 -c=fruit --archive=fruit.gz --gzip

Hướng dẫn save mongodb - lưu mongodb

Thực hiện restore data từ file fruit.gz vào collection

mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
2 của database
mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase}  -u={username} -p={password} --archive={filename.gz} --gzip --nsFrom={sourceDatabase.sourceCollection} --nsTo={destinationDatabase.destinationCollection}
5

mongorestore --archive=fruit.gz --gzip --nsFrom=store_1.fruit --nsTo=store_2.fruit

Hướng dẫn save mongodb - lưu mongodb

Kết quả:

Hướng dẫn save mongodb - lưu mongodb

Okay, Done!

References:

https://docs.mongodb.com/database-tools/mongodump

https://docs.mongodb.com/database-tools/mongorestore