Home > Software design >  restore mongodb database few minutes before mistaken command in case of missing backup
restore mongodb database few minutes before mistaken command in case of missing backup

Time:01-24

We did some wrong update on a few fields in some documents , can we do mongodump now and restore fresh until before the change with oplogReply or the backup need to be taken before the action?

let say we have: 17:00h update on field

18:00h mongodump

19:00h mongorestore with oplogReply until 16:59h

Is this possible ?

CodePudding user response:

Okey , fixed , based on following steps:

  1. Mongodump the oplog from any of the members of affected rset:

     mongodump --db local -c oplog.rs --port 10001  --out /dump
      2022-01-20T21:33:52.233 0100 writing local.oplog.rs to /dump/local/oplog.rs.bson
      2022-01-20T21:33:52.235 0100 done dumping local.oplog.rs (627 documents)
    
  2. Find the entry before the faulty change:

    bsondump /dump/local/oplog.rs
      ( check the timestamp until where the oplog reply need to happen )
      {"op":"n","ns":"","o":{"msg":"periodic noop"},"ts": 
      {"$timestamp":{"t":1642710669,"i":1}},"t": 
      {"$numberLong":"1"},"v":{"$numberLong":"2"},"wall":{"$date":{"$numberLong":"1642710669341"}}}
    
  3. Copy the oplog to different file:

    cp  /dump/local/oplog.rs.bson  /dump/oplog
    
  4. Reply the oplog to timestamp before faulty entry to the PRIMARY replicaSet member:

    mongorestore  --port 10001 --oplogReplay --oplogLimit 1642710669:1  --oplogFile /dump/oplog
    
  5. Now you will see in the database the last faulty changes are removed.

  •  Tags:  
  • Related