Phần 8 Backup và Restore trên MongoDB Server

Phần 8 Backup và Restore trên MongoDB Server

1./ Lý thuyết

Nếu sử dụng MongoDB Atlas thì sẽ được backup, restore trên cloud service

#

Because mongodump and mongorestore operate by interacting with a running mongod instance, they can impact the performance of your running database. Not only do the tools create traffic for a running database instance, they also force the database to read all data through memory. When MongoDB reads infrequently used data, it can evict more frequently accessed data, causing a deterioration in performance for the database's regular workload.

=> việc sử dụng mongodump và mongorestore có thể ảnh hưởng trực tiếp đến hiệu suất của mongodb. Do dữ liệu cũ sẽ được đẩy lên Ram và thay thế các document mới đang được cache và sử dụng nhiều. Ram cũng sẽ bị tốn.

2./ Backup MongoDB

2.1/ Import 1 DB lớn khoảng 30 ngàn bản ghi để test

cd /opt/backup/mongodb

wget https://media.mongodb.org/zips.json

mongoimport --db test3 --collection zips --file zips.json

#Kết quả

2022-02-28T16:20:53.049+0700  ​​ ​​​​ connected to: mongodb://localhost/

2022-02-28T16:20:55.310+0700  ​​ ​​​​ 29353 document(s) imported successfully. 0 document(s) failed to import.

#Kiểm tra lại DB test3​​ đã có đủ dữ liệu

2.2/ Restore​​ MongoDB​​ 

Tiến hành xoá DB test3

rs0:PRIMARY> show dbs

admin  ​​​​ 0.000GB

config ​​ 0.000GB

local  ​​​​ 0.007GB

test  ​​ ​​​​ 0.000GB

test3  ​​​​ 0.002GB

rs0:PRIMARY> db.dropDatabase()

{

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "dropped" : "test3",

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "ok" : 1,

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "$clusterTime" : {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "clusterTime" : Timestamp(1646042298, 2),

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "signature" : {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "keyId" : NumberLong(0)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ },

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "operationTime" : Timestamp(1646042298, 2)

}

rs0:PRIMARY> show dbs

admin  ​​​​ 0.000GB

config ​​ 0.000GB

local  ​​​​ 0.007GB

test  ​​ ​​​​ 0.000GB

Script Restore DB test3

#!/bin/bash

#/opt/script/restore_mongodb_bson.sh

#create by cuongnh

#2022.02.28

 

MONGO_DIR=/opt/backup/mongodb/

mkdir -p $MONGO_DIR

now1="$(date +'%Y.%m.%d-%H.%M.%S.%3N')"

DB_NAME=$1

DB_FOLDER=$2

#mongorestore --host=mongodb1.example.net --port=3017 --username=user ​​ --authenticationDatabase=admin /opt/backup/mongodump-2013-10-24

#1./Restore MongoDB bson

mongorestore -d $DB_NAME /opt/backup/mongodb/$DB_FOLDER/$DB_NAME

#mongorestore -d test3 -o /opt/backup/mongodb/test3-\[2022.02.28-16.26.53.236\]

echo ""

 

#Kết quả:

root@svr11:/opt/backup/mongodb# /opt/script/restore_mongodb_bson.sh test3 test3-[2022.02.28-16.26.53.236]

2022-02-28T17:02:25.952+0700  ​​ ​​​​ The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}

2022-02-28T17:02:25.952+0700  ​​ ​​​​ building a list of collections to restore from /opt/backup/mongodb/test3-[2022.02.28-16.26.53.236]/test3 dir

2022-02-28T17:02:25.953+0700  ​​ ​​​​ reading metadata for test3.zips from /opt/backup/mongodb/test3-[2022.02.28-16.26.53.236]/test3/zips.metadata.json

2022-02-28T17:02:25.969+0700  ​​ ​​​​ restoring test3.zips from /opt/backup/mongodb/test3-[2022.02.28-16.26.53.236]/test3/zips.bson

2022-02-28T17:02:27.779+0700  ​​ ​​​​ finished restoring test3.zips (29353 documents, 0 failures)

2022-02-28T17:02:27.780+0700  ​​ ​​​​ no indexes to restore for collection test3.zips

2022-02-28T17:02:27.780+0700  ​​ ​​​​ 29353 document(s) restored successfully. 0 document(s) failed to restore.

 

 

Có thể backup và restore với gzip để nén file

https://docs.ovh.com/gb/en/publiccloud/databases/mongodb/backups-and-restores-cli/?xtor=SEC-13-GOO-[ID_INT_OVH_Domain_Undefinite_Traffic_Reach_Srch_Offensive_DomainNameGenericVIETNAM_(Web)()]-[563968396548]-S-[]&xts=563736&sitelink=&gclid=CjwKCAiAgvKQBhBbEiwAaPQw3PtsV2wGq8BuMl-X12pYU-2Mp0s_5tXqMXwH0ZjwwNvwU1lWFO7isBoC8NoQAvD_BwE

 

 

 

 

 

 

https://stackoverflow.com/questions/5723896/is-there-a-sample-mongodb-database-along-the-lines-of-world-for-mysql

 

BÀI VIẾT CÙNG CHUYÊN MỤC

Không copy được giữa máy Windows và máy ảo qua mRemoteNG/RDP: Nguyên nhân và cách sửa
Không copy được giữa máy Windows và máy ảo qua ...

mRemoteNG Remote Desktop RDP Clipboard Redirection rdpclip.exe VPS Windows ...

Hướng dẫn bật Nested Virtualization trên ESXi để chạy Android Studio Emulator trong máy ảo Windows
Hướng dẫn bật Nested Virtualization trên ESXi để chạy ...

Nested Virtualization ESXi VMware Android Studio Android Emulator WHPX Hyper-V ...

PITR (Point In Time Recovery) là gì? Khôi phục Database về đúng thời điểm cần thiết
PITR (Point In Time Recovery) là gì? Khôi phục ...

PITR, Point In Time Recovery, PITR là gì, Database Recovery, PostgreSQL PITR, ...

Backup Database Full, Incremental, Differential khác nhau thế nào?
Backup Database Full, Incremental, Differential khác ...

Backup Database, Full Backup, Incremental Backup, Differential Backup, Backup ...

Database Cluster là gì? High Availability, Failover và Cluster Database hoạt động như thế nào?
Database Cluster là gì? High Availability, Failover và ...

Database Cluster, Database Cluster là gì, High Availability Database, Database ...