oplog备份
环境:
OS:Centos 7
DB:mongodb 4.0/5.0
每天进行备份
[root@localhost script]# more backup_mongo_oplog.sh
#!/bin/bash
##start_time=`date -d '-62 Minutes' +%s`
##backup_dir=`date +"%Y%m%d-%H%M%S"`
backup_date_dir=`date +%Y%m%d -d "1 days ago"` ##备份目录
start_time=`tail -1 /script/log/log.txt|cut -d '|' -f 2`
end_time=`date -d '0 days ago' +%s` ##当前的时间
mkdir -p /script/oplog_bak/$backup_date_dir
echo $start_time'|'$end_time>>/script/log/log.txt
/opt/mongodb-database-tools/bin/mongodump -h 192.168.1.10028001 -u hxl -p hxl123 --authenticationDatabase admin --db=local -c oplog.rs --gzip --query '{"ts":{"$gte": {"$timestamp":{"t":'$start_time', "i":1}},"$lte": {"$timestamp":{"t":'$end_time', "i":1}}}}' -o /script/oplog_bak/$backup_date_dir
[root@localhost script]# crontab -l
##mongodb oplog备份
0 9 * * * /script/backup_mongo_oplog.sh>/dev/null 2>&1