DBAtlas · 데이터베이스 운영 아틀라스

oplog replay로 시점 복원 (잘못된 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, 대량 삭제)의 timestamp까지 재생하여 데이터를 그 시점으로 되돌린다.

사용 시점

컬렉션이 실수로 삭제되었거나 잘못된 대량 update/delete가 실행되어 손상 직전의 마지막 상태가 필요할 때.

주의

운영 시스템이 아닌 별도 대상에 복원하고, 검증 후 필요한 데이터를 옮긴다. 백업 시작부터 잘못된 작업까지의 oplog가 아직 oplog window 안에 있어야 한다. window 밖으로 밀려난 작업은 복구할 수 없다. --oplog는 전체 인스턴스 덤프에서만 동작한다(--db와 함께 사용할 수 없음). sharded cluster에서는 mongodump로 일관된 PITR이 불가능하므로 파일 시스템 스냅샷이나 백업 제품이 필요하다.

DBAtlas에서 열기 →

관련 항목