Files
notes/tiddlywiki/MongoDB - point in time recovery.txt
2026-03-12 22:01:38 +01:00

64 lines
2.9 KiB
Plaintext
Executable File

~~ getting min/max timestamp in oplog can be done on PRIMARY or on any SECONDARY member of a replica set
rs.slaveOk();
~~ display usefull oplog informations
rs.printReplicationInfo()
use local
db.oplog.rs.find({}, {ts: 1,}).sort({ts: -1}).limit(1)
db.oplog.rs.find({}, {ts: 1,}).sort({ts: 1}).limit(1)
~~ exemple
x=Timestamp(1590072867, 1)
>> Timestamp(1590072867, 1)
new Date(x.t * 1000)
>> ISODate("2020-05-21T14:54:27Z")
x=Timestamp(1581603867, 1)
>> Timestamp(1581603867, 1)
new Date(x.t * 1000)
>> ISODate("2020-02-13T14:24:27Z")
~~ note that a ISODate finishing by Z is a UTC date
~~ pay attention to the diffrence between your local time and UTC; for example CEST=UTC+2
~~ exemple: find min/max timestamp for oplog records for the last hour
var SECS_PER_HOUR = 3600
var now = Math.floor((new Date().getTime()) / 1000) // seconds since epoch right now
db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(now, 1), "$gt" : Timestamp(now - SECS_PER_HOUR, 1) } }).sort({ts:-1}).limit(1);
db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(now, 1), "$gt" : Timestamp(now - SECS_PER_HOUR, 1) } }).sort({ts:1}).limit(1);
~~ exemple: list oplog records between 2 dates
var since = Math.floor(ISODate("2020-05-21T15:43:16Z").getTime() / 1000)
var until = Math.floor(ISODate("2020-05-21T15:43:18Z").getTime() / 1000)
db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(until, 1), "$gt" : Timestamp(since, 1) } })
~~ exemple: get lst oplog record before a date (usefull for Point In Time Recovery)
var until = Math.floor(ISODate("2020-05-22T15:08:25Z").getTime() / 1000)
db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(until, 1) } }).sort({ts:-1}).limit(1);
~~ oplog is a collection, it can be dump umping mongodump tool
mongodump -u superhero -p secret --authenticationDatabase admin -d local -c oplog.rs -o oplogdump
~~ the format is BSON, if you want to query-it, you should to convert the file in JSON format:
cd oplogdump/local
bsondump oplog.rs.bson > archive.json
~~ Point In Time Recovery exemple for PITR=2020-05-22 17:08:25 CEST
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~ create a new empty stand-alone MondgoDB without authentificztion and replica set in configuration file
-- restore the last FULL BACKUP of data before your PITR
~~ convert CEST in UTC: PITR=2020-05-22T15:08:25Z and note it down
~~ find the correspondind Timestamp in oplog
var until = Math.floor(ISODate("2020-05-22T15:08:25Z").getTime() / 1000)
db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(until, 1) } }).sort({ts:-1}).limit(1);
~~ in my exemple I obtained Timestamp(1590160104, 1)
~~ copy oplog.rs.bson file locally in a EMPTY folder and rename-it oplog.bson
~~ in my exemple, the folder is: /mnt/yavin4/tmp/_mongodb_/tmp
~~ optionally perform a dryrun in order to check your mongorestore command
mongorestore --dryRun --oplogReplay --oplogLimit 1590160104:1 /mnt/yavin4/tmp/_mongodb_/tmp
~~ recover until the corresponding Timespamp
mongorestore --oplogReplay --oplogLimit 1590160104:1 /mnt/yavin4/tmp/_mongodb_/tmp