oplog の再生によるポイントインタイムリストア (誤った drop/delete の前に戻す)
MongoDBバックアップコマンド
# 1) Full backup + oplog written during the backup (whole instance, from a replica set member):
mongodump --uri="mongodb://admin@mongo2:27017/?authSource=admin&readPreference=secondary" --oplog --gzip --out=/backup/full
# 2) Find the time of the bad operation (mongosh): db.getSiblingDB("local").oplog.rs.find({ "o.drop": "musteri" }).sort({ $natural: -1 }).limit(1)
# 3) Dump the oplog after the backup (t: start time of the full backup, epoch seconds):
mongodump --uri="mongodb://admin@mongo2:27017/?authSource=admin" --db=local --collection=oplog.rs --query='{"ts":{"$gte":{"$timestamp":{"t":1791400000,"i":1}}}}' --out=/backup/oplog
# 4) Restore the full backup together with its own oplog:
mongorestore --uri="mongodb://admin@target:27017/?authSource=admin" --gzip --oplogReplay /backup/full
# 5) Replay the oplog up to the bad operation (oplogLimit is exclusive):
mkdir /backup/pitr && mv /backup/oplog/local/oplog.rs.bson /backup/pitr/oplog.bson
mongorestore --uri="mongodb://admin@target:27017/?authSource=admin" --oplogReplay --oplogLimit=1791403600:1 /backup/pitr概要
--oplog でフルバックアップの一貫性を確保し、バックアップ後に蓄積された oplog を誤った操作 (drop、一括削除) のタイムスタンプまで再生して、データをその時点に戻す。
使用する場面
コレクションを誤って削除した場合や、誤った一括更新 / 削除が実行され、損傷前の最後の状態が必要な場合。
注意
稼働中のシステムではなく別のターゲットにリストアし、検証後に必要なデータを移動する。バックアップ開始から誤った操作までの oplog が、まだ oplog ウィンドウ内にある必要がある。ウィンドウから外れた操作は復旧できない。--oplog はインスタンス全体のダンプでのみ機能する (--db とは併用できない)。シャードクラスタでは mongodump による一貫した PITR は不可能であり、ファイルシステムスナップショットまたはバックアップ製品が必要。