MongoDB定时备份脚本

1、MongoDB备份脚本

#!/bin/bash

MongodbPath=/sas/db-backup/mongodb
LogPath=/var/log/db-backup-mongo.log
SaveDays=7

echo "[$(date +"%Y-%m-%d %H:%M:%S")] ============ Start database backup task ============" &>> $LogPath

mkdir -p $MongodbPath/$(date +"%Y%m%d%H%M%S")/
mongodump -h devops/10.255.200.1,10.255.200.2,10.255.200.3 --authenticationDatabase admin -u hbtv -p 'xxxxxxxxxx' -d storage_insight -o $MongodbPath/$(date +"%Y%m%d%H%M%S")/ &>> $LogPath

if [ $? -eq 0 ]; then
        # only delete old backup files when new backup success !

        ExistBackupFiles=`ls $MongodbPath | wc -l`
        if [ $ExistBackupFiles -gt $SaveDays ]; then

                # only delete old backup files when exist backup files more then 7 days !
                # mariadb backup files clean

                echo "[$(date +"%Y-%m-%d %H:%M:%S")] Start mongodb backup files clean task ..." &>> $LogPath

                find $MariadbPath -type d -mtime +$SaveDays | xargs rm -rvf &>> $LogPath

                echo "[$(date +"%Y-%m-%d %H:%M:%S")] Mongodb backup files clean task finished ! " &>> $LogPath
        fi
fi

echo "[$(date +"%Y-%m-%d %H:%M:%S")] Mongodb backup task finished ! " &>> $LogPath

猜你喜欢

转载自blog.51cto.com/dusthunter/2623663