Mongodb恢复被删除的文件
前提条件
Mongodb需要是集群环境
获取时间戳
获取当前时间戳
date +%s
获取指定时间的时间戳
date -d "2022-03-28 23:59:59" +%s
获取oplog记录
获取操作日志中插入文件记录(指定时间以前的)
./mongodump -h localhost:27020 -u admin -p 123321 \
--authenticationDatabase admin -d local -c oplog.rs -q \
'{"op":"i","ns":"backup_db.fs.files","ts":{"$lt":{"$timestamp":{"t": 需要替换为时间戳, "i":1}}}}' -o \
/opt/bakdata.json
获取操作日志中插入文件记录(指定时间到当前时间的)
./mongodump -h localhost:27020 -u admin -p 123321 \
--authenticationDatabase admin -d local -c oplog.rs -q \
'{"op":"i","ns":"backup_db.fs.files","ts":{"$gt": {"$timestamp":{"t": 需要替换为时间戳, "i":1}}}}' -o \
/opt/bakdata.json
获取操作日志中插入文件记录(时间范围内的)
./mongodump -h localhost:27020 -u admin -p 123321 \
--authenticationDatabase admin -d local -c oplog.rs -q \
'{"op":"i","ns":"backup_db.fs.files","ts":{"$lt":{"$timestamp":{"t": 需要替换为时间戳, "i":1}}, "$gt": {"$timestamp":{"t": 需要替换为时间戳, "i":1}}}}' -o \
/opt/bakdata.json
重放oplog进行数据恢复
./mongorestore --port 27020 -u admin -p 123321 --authenticationDatabase admin --oplogReplay --oplogFile /opt/bakdata.json/local/oplog.rs.bson