There are two recovery methods of after the database is deleted by mistake

  • 2020-09-16 07:50:45
  • OfStack

Method 1: Restore via oplog

If an MongoDB replication set is deployed, there is a hope that oplog can be used to recover as much data as possible. Every change to the MongoDB replication set records one oplog, so when the database is deleted by mistake, you can "recover as much data as possible" by replaying the existing oplog. Recently, I met a user who was very lucky. The database was created recently, and all the operations were still kept in oplog, so the user recovered all the mistakenly deleted data through oplog.

The process of recovering data through oplog is very simple. You just need to export the oplog collection through mongodump and then replay 1 through mongorestore's oplogReplay mode.

Step1: Export the oplog collection

mongodump -d local -c oplog.rs -d -o backupdir

Step2: Copies the data from the oplog collection


mkdir new_backupdir
cp backupdir/local/oplog.rs.bson new_backupdir/oplog.bson

oplog Step3: replay

mongorestore --oplogReplay new_backupdir

Method 2: Restore by backup set

If you do a full + incremental backup of MongoDB, you can restore the data through the backup set and. Backups can take many forms, such as:

Logical backup of the database through tools such as mongodump Copy the physical backup generated by the dbpath directory Snapshots of file systems, volume management, and so on

There is also a question, "Why do you need to do data backup when you have a multi-node replication set deployed?" ; In the case of database deletion by mistake, the dropDatabase command is also synchronized to all standby nodes, resulting in the deletion of data from all nodes.

conclusion


Related articles: