Bạn đang quản trị máy chủ MongoDB và mong muốn sao lưu dữ liệu database MongoDB để khi có sự cố phát sinh sẽ có thể khôi phục lại dữ liệu database MongoDB. Vậy làm thế nào và làm ra sao ? Chúng ta cùng tìm hiểu nhé.
MongoDB backup/restore như thế nào ?
Đầu tiên bạn cần lưu ý nhớ rằng MongoDB sử dụng format json và bson (binary json) để lưu trữ thông tin và dữ liệu. Json là chuẩn hỗ trợ cho con người đọc được rất dễ để export và import dữ liệu của bạn. Bạn chỉ cần sử dụng bất kì công cụ nào hỗ trợ json thậm chí là trình chỉnh sửa văn bản (text editor).
Ví dụ format Json:
{“address”:[ {“building”:“1007”, “street”:“Park Ave”}, {“building”:“1008”, “street”:“New Ave”}, ]} |
Json khá là tiện lợi như vậy, nhưng lại thuận tiện cho việc backup (sao lưu) và khôi phục (restore). Binary JSON (bson), MongoDB sẽ lưu trữ hiển thị dữ liệu JSON document dưới chuẩn định dạng binary-encoded. Bson sẽ mở rộng kiểu mẫu dữ liệu JSON bằng cách thêm các thông tin cần thiết như loại dữ liệu, thứ tự field, hỗ trợ encoding và decoding với các ngôn ngữ khác nhau. Vì vậy mà khi backup và restore thường sẽ sử dụng file ‘bson‘.
Khi tiến hành khôi phục MongoDB database, nếu database collection chưa tồn tại thì MongoDB sẽ tự khởi tạo database mới rỗng và khôi phục dữ liệu cho.
Để backup database MongoDB ta sử dụng chương trình lệnh ‘mongodump‘ và để restore database MongoDB ta sử dụng chương trình lệnh ‘mongorestore‘.
1. Backup (sao lưu) Cơ Sở Dữ Liệu MongoDB
Cú pháp:
mongodump –d <database_name> –o <folder_will_contain_backup_file> mongodump —db <database_name> —out <folder_will_contain_backup_file> mongodump —host $HOST —port $PORT –u $USERNAME –p $PASSWORD –d $DB –o <folder_will_contain_backup_file> |
Chú thích:
- -d/–db : tên MongoDB Database
- -o/–out : đường dẫn thư mục sẽ chứa dữ liệu backup của MongoDB Databse.
Giả sử mình có database mẫu tên ‘SampleCollections‘.
> show dbs; SampleCollections 0.059GB admin 0.000GB config 0.000GB local 0.000GB |
Giờ mình tạo thư mục để chứa dữ liệu backup database MongoDB.
mkdir /opt/backup/mongodb/ |
Tiếp đến ta chạy chương trình lệnh ‘mongodump‘ để tiến hành backup database ‘SampleColletions‘ ra thư mục : /opt/backup/mongodb/
mongodump –d SampleCollections –o /opt/backup/mongodb/ .. 2019–03–21T23:44:37.736+0700 done dumping SampleCollections.mongomart_cart (1 document) 2019–03–21T23:44:37.900+0700 done dumping SampleCollections.DBEnvyLoad_customers (100000 documents) 2019–03–21T23:44:37.964+0700 done dumping SampleCollections.DBEnvyLoad_products (100000 documents) 2019–03–21T23:44:39.322+0700 done dumping SampleCollections.and_sorted_test (900000 documents) |
Giờ kiểm tra thư mục database backup nào, bạn sẽ thấy có file .json và cả .bson .
ll /opt/backup/mongodb/SampleCollections/ total 100764 –rw–r—r— 1 root root 39600000 Mar 21 23:44 and_sorted_test.bson –rw–r—r— 1 root root 147 Mar 21 23:44 and_sorted_test.metadata.json –rw–r—r— 1 root root 3778762 Mar 21 23:44 crunchbase_database.bson –rw–r—r— 1 root root 151 Mar 21 23:44 crunchbase_database.metadata.json ... |
Lúc này bạn đã biết cách backup Cơ Sở Dữ Liệu MongoDB, bạn có thể kết hợp một số thứ như :
- Cronjob đặt lịch
- Kết hợp output thư mục theo ngày tháng
- Viết script để tự động backup
- Nén thư mục chứa Cơ Sở Dữ Liệu MongoDB vừa được backup ra.
2. Restore (khôi phục) Cơ Sở Dữ Liệu MongoDB
Cú pháp:
mongorestore —drop –d <database_will_be_restored> <location_to_folder_containing_mongodb_database> mongorestore —drop —db <database_will_be_restored> <location_to_folder_containing_mongodb_database> mongorestore —host $HOST —port $PORT –u $USERNAME –p $PASSWORD –d $DB <location_to_folder_containing_mongodb_database> |
Chú thích:
- –drop : option này sẽ yêu cầu MongDB xoá database mà bạn chuẩn bị restore trước rồi mới khôi phục lại.
- -d/–db : tên MongoDB database
- : thư mục chứa dữ liệu MongoDB Database đã backup trước đó.
Giờ bạn muốn khôi phục lại database MongoDB mà bạn đã backup trước đó thì cú phá cũng đơn giản với chương trình lệnh ‘mongorestore‘.
mongorestore —drop –d SampleCollections /opt/backup/mongodb/SampleCollections/ ... 2019–03–22T00:26:52.342+0700 finished restoring SampleCollections.DBEnvyLoad_customers (100000 documents) 2019–03–22T00:26:53.934+0700 [########…………….] SampleCollections.and_sorted_test 12.6MB/37.8MB (33.3%) 2019–03–22T00:26:56.934+0700 [#################…….] SampleCollections.and_sorted_test 26.9MB/37.8MB (71.3%) 2019–03–22T00:26:59.189+0700 [########################] SampleCollections.and_sorted_test 37.8MB/37.8MB (100.0%) 2019–03–22T00:26:59.189+0700 no indexes to restore 2019–03–22T00:26:59.189+0700 finished restoring SampleCollections.and_sorted_test (900000 documents) 2019–03–22T00:26:59.190+0700 done |
Đơn giản phải không nào ! Chúc các bạn thực hiện thành công nhé.