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. Để sao lưu data thành file .gz ta dùng câu lệnh sau: (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 đó:
Lưu ý: khi sử dụng mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase} -u={username} -p={password} --db={database} --collection={collection} --archive={filename.gz} --gzip5 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} --gzip6. Với mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase} -u={username} -p={password} --db={database} --collection={collection} --archive={filename.gz} --gzip7 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} --gzip0 và mongorestore --host={host:port} --authenticationDatabase={authenticationDatabase} -u={username} -p={password} --db={database} --collection={collection} --archive={filename.gz} --gzip9 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} DemoVí 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 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 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 Kết quả: Okay, Done! References: https://docs.mongodb.com/database-tools/mongodump https://docs.mongodb.com/database-tools/mongorestore |