Hvis du føler, at du vil tage en sikkerhedskopi af dine filer og mapper i MongoDB, så følg mig gennem denne artikel.
vi vil bruge mongodumo og mongorestore til vores backup- og gendannelsesmetode. mongodump læser data fra en MongoDB-database og opretter high fidelity BSON-filer, som mongorestore-værktøjet kan bruge til at udfylde en MongoDB-database. mongodump og mongorestore er enkle og effektive værktøjer til at sikkerhedskopiere og gendanne små MongoDB-implementeringer, men de er ikke ideelle til at tage backup af større systemer. læs mere
Sikkerhedskopiering ved hjælp af mongodump:
[[email protected] mongo]# mongodump --out=/home/mongoBackup --db=mughees 2019-10-21T13:32:48.421+0300 writing mughees.myNewCollection1 to 2019-10-21T13:32:48.422+0300 writing mughees.myNewCollection2 to 2019-10-21T13:32:48.425+0300 done dumping mughees.myNewCollection1 (3 documents) 2019-10-21T13:32:48.427+0300 writing mughees.myNewCollection3 to 2019-10-21T13:32:48.429+0300 done dumping mughees.myNewCollection3 (0 documents) 2019-10-21T13:32:48.431+0300 done dumping mughees.myNewCollection2 (1 document)
–out ==> for at angive stien, hvor det vil tage backup-output til.
–db ==> navnet på den database, som du vil tage backup af.
DROP mughees DB:
>show databases admin 0.000GB config 0.000GB local 0.000GB mughees 0.000GB db.dropDatabase() db.dropDatabase() { "dropped" : "mughees", "ok" : 1 } > Now we will create a mughees DB again and check if there is any collection available > use mughees use mughees switched to db mughees > show collectionsshow collections #no collection will be shown >
Ingen samlinger er blevet vist, da databasen er blevet slettet
> show databases; show databases; admin 0.000GB config 0.000GB local 0.000GB
Vi har lavet mughees DB, men DB oprettes ikke før og medmindre du opretter en samling inde i DB.
Gendan nu Mughees DB:
Lad os nu genoprette vores sikkerhedskopi af mughees db og sikre dig, at du har oprettet databasen med samme.
[[email protected] mongo]# mongorestore --db=mughees /home/mongoBackup/mughees 2019-10-21T13:41:34.773+0300 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead 2019-10-21T13:41:34.774+0300 building a list of collections to restore from /home/mongoBackup/mughees dir 2019-10-21T13:41:34.776+0300 reading metadata for mughees.myNewCollection1 from /home/mongoBackup/mughees/myNewCollection1.metadata.json 2019-10-21T13:41:34.783+0300 reading metadata for mughees.myNewCollection2 from /home/mongoBackup/mughees/myNewCollection2.metadata.json 2019-10-21T13:41:34.784+0300 reading metadata for mughees.myNewCollection3 from /home/mongoBackup/mughees/myNewCollection3.metadata.json 2019-10-21T13:41:34.828+0300 restoring mughees.myNewCollection1 from /home/mongoBackup/mughees/myNewCollection1.bson 2019-10-21T13:41:34.832+0300 no indexes to restore 2019-10-21T13:41:34.832+0300 finished restoring mughees.myNewCollection1 (3 documents, 0 failures) 2019-10-21T13:41:34.866+0300 restoring mughees.myNewCollection2 from /home/mongoBackup/mughees/myNewCollection2.bson 2019-10-21T13:41:34.869+0300 no indexes to restore 2019-10-21T13:41:34.871+0300 finished restoring mughees.myNewCollection2 (1 document, 0 failures) 2019-10-21T13:41:34.881+0300 restoring mughees.myNewCollection3 from /home/mongoBackup/mughees/myNewCollection3.bson 2019-10-21T13:41:34.895+0300 restoring indexes for collection mughees.myNewCollection3 from metadata 2019-10-21T13:41:34.921+0300 finished restoring mughees.myNewCollection3 (0 documents, 0 failures) 2019-10-21T13:41:34.921+0300 4 document(s) restored successfully. 0 document(s) failed to restore. [[email protected] mongo]#
Tjek gendannelsesdatabasen:
Lad os nu tjekke, om db og samlingerne indeni er tilgængelige eller ej:
>show databases; admin 0.000GB config 0.000GB local 0.000GB mughees 0.000GB > > use mugheesuse mughees switched to db mughees > show collectionsshow collections myNewCollection1 myNewCollection2 myNewCollection3