规划
副本集,其中加了个隐藏节点,用来做备份,所以备份脚本直接在隐藏节点做,目前数据不大,直接本机磁盘存储,后续如果数据集大,那么在本地存最近一天的备份,远程根据需求存储几天的备份
创建备份用户
db.createUser(
{
user: "backup",
pwd: "password",
roles: [ "backup" ]
}
)
备份脚本
#!/bin/bash
PATH=$PATH:/usr/local/mongodb/bin
export PATH
port=27017
user="backup"
password="password"
backup_home="/data/backup"
backup_date="`date +"%Y%m%d"`"
backup_dir="/data/backup/$backup_date"
mongodump --port=$port -u $user -p $password --oplog --out=$backup_dir --gzip
find $backup_home -mtime +2 -type d -exec rm -rf {} \;
定时任务
echo "22 21 * * * /usr/local/mongodb/backup_mongod.sh 2>&1 >/dev/null" >> /var/spool/cron/root