此脚本是以文件夹的形式备份,非压缩包形式
如需备份成加密压缩包,可用此脚本:MongoDB定时异地备份所有数据库为加密压缩包-CSDN博客
1.可以直接下载本文件使用,将其放到mongo安装目录的bin目录下(可手动执行,也可在定时任务中执行)
2.并赋予执行权限:chmod 777 文件名
文件内容:
#!/bin/bash#本脚本必须放在MongoDB安装目录的bin目录下,并赋予执行权限!!!!!!!!!!!!!!!!!!!!!!!!echo "[$(date "+%Y-%m-%d %H:%M:%S")] Start backup mongo database..."
# 以下配置信息请自己修改
mongo_user="root" #MongoDB备份用户
mongo_password="123456" #MongoDB备份用户的密码
mongo_host="127.0.0.1"
mongo_port="27017"
backup_db_arr=("das test_db") #要备份的数据库名称,多个用空格分开隔开 如("db1" "db2" "db3")
backup_location=/home/mongoDataBackup #备份数据存放位置,末尾请不要带"/",此项可以保持默认,程序会自动创建文件夹
expire_backup_delete="ON" #是否开启过期备份删除 ON为开启 OFF为关闭
expire_days=3 #过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效remote_host=172.20.202.130 #需要备份到远程服务器的IP
remote_port=22 #需要备份到远程服务器的ssh端口
remote_path=/home/mongoRemoteBackupData #需要备份到远程服务器的文件路径
remote_username=root #需要备份到远程服务器的用户名
remote_password=123456. #需要备份到远程服务器的密码# 本行开始以下不需要修改
backup_time=`date +%Y%m%d%H%M` #定义备份详细时间
backup_Ymd=`date +%Y-%m-%d` #定义备份目录中的年月日时间
backup_3ago=`date -d '3 days ago' +%Y-%m-%d` #3天之前的日期
backup_dir=$backup_location/$backup_Ymd #备份文件夹全路径
welcome_msg="Try to connect MongoDB..." #欢迎语# 判断MongoDB是否启动,没有启动则备份退出
mongo_ps=`ps -ef |grep mongo |wc -l`
mongo_listen=`netstat -an |grep LISTEN |grep $mongo_port|wc -l`
if [ [$mongo_ps == 0] -o [$mongo_listen == 0] ]; thenecho "[$(date "+%Y-%m-%d %H:%M:%S")] ERROR: MongoDB is not running! backup stop!!!"
exit
elseecho [$(date "+%Y-%m-%d %H:%M:%S")] $welcome_msg
fi# 连接到mongo数据库,无法连接则备份退出
mongo das --host $mongo_host --port $mongo_port -u $mongo_user -p $mongo_password <<end
exit
endflag=`echo $?`
if [ $flag != "0" ]; thenecho "[$(date "+%Y-%m-%d %H:%M:%S")] ERROR:Can't connect mongo server! backup stop!"
exitelse
echo "[$(date "+%Y-%m-%d %H:%M:%S")] MongoDB connect ok! Backing up in progress...... Please wait!"# 判断有没有定义备份的数据库,如果定义则开始备份,否则退出备份
if [ "$backup_db_arr" != "" ];then
#dbnames=$(cut -d ',' -f1-5 $backup_database)
#echo "arr is (${backup_db_arr[@]})"
for dbname in ${backup_db_arr[@]}
do
echo "[$(date "+%Y-%m-%d %H:%M:%S")] database $dbname backup start..."
`mkdir -p $backup_dir`
`mongodump --host $mongo_host --port $mongo_port -u $mongo_user -p $mongo_password --db $dbname -o $backup_dir`
flag=`echo $?`
if [ $flag == "0" ];thenecho "[$(date "+%Y-%m-%d %H:%M:%S")] database $dbname success backup to $backup_dir/$dbname"
elseecho "[$(date "+%Y-%m-%d %H:%M:%S")] database $dbname backup fail!"
fidone
elseecho "[$(date "+%Y-%m-%d %H:%M:%S")] ERROR:No database to backup! backup stop"
exit
fi#打包压缩
#tar -zcvf $backup_dir'.tar.gz' $backup_dir
#echo "Backup file compressed!" #拷贝数据库备份文件至另一台服务器
sshpass -p $remote_password scp -r -P $remote_port $backup_dir $remote_username@$remote_host:$remote_path
# 检查scp命令的退出状态码
if [ $? -eq 0 ]; thenecho "[$(date "+%Y-%m-%d %H:%M:%S")] Copy local backup file: [ $backup_dir ] to remote backup path: [ $remote_path ] successfully! "
elseecho "[$(date "+%Y-%m-%d %H:%M:%S")] ERROR:[remote_username:$remote_username][remote_password:$remote_password][remote_host:$remote_host][remote_port:$remote_port] Copy local backup file: [ $backup_dir ] to remote backup path: [ $remote_path ] faild!!!"
exit
fi# 如果开启了删除过期备份,则进行删除操作
if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then
#`find $backup_location/ -type d -o -type f -ctime +$expire_days -exec rm -rf {} \;`
`find $backup_location/ -type d -mtime +$expire_days | xargs rm -rf`
echo "[$(date "+%Y-%m-%d %H:%M:%S")] Expired backup file from $expire_days days ago delete complete!"
fi
echo "[$(date "+%Y-%m-%d %H:%M:%S")] All database backup success! Thank you!"
exit
fi
3.备份服务器上对过期的备份文件自动清理脚本如下
(该脚本和文章开头引用的另一篇博客中的步骤3脚本不同,不要混用!!!)
#!/bin/bash
# 以下配置信息请自己修改,该脚本放于任何位置皆可,记得赋予可执行权限
backup_location=/home/mongoDataBackup #备份数据存放位置,末尾请不要带"/",此项可以保持默认,程序会自动创建文件夹
expire_backup_delete="ON" #是否开启过期备份删除 ON为开启 OFF为关闭
expire_days=3 #过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效
log_path=./clear_expire_mongo_backup_file.log #脚本日志输出路径echo "[$(date "+%Y-%m-%d %H:%M:%S")] Start clear expire mongo backup file..." >> $log_path# 如果开启了删除过期备份,则进行删除操作
if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then`find $backup_location/ -type d -mtime +$expire_days | xargs rm -rf`echo "[$(date "+%Y-%m-%d %H:%M:%S")] Expired backup file from $expire_days days ago delete complete!" >> $log_path
fi
echo "[$(date "+%Y-%m-%d %H:%M:%S")] Clear expire mongo backup file completed!!!" >> $log_path
exit
fi