1、mongodb全量备份脚本,内容如下:
cat /data/soft/mongodb_backup.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
authuser=admin1
authpass=admin123
servername=testrepl/127.0.0.1:30000
instancename=test
authdbname=admin
stamp=`date +"%Y-%m-%d"`
programname=bulingbuling
backuppath=/data/backup
backname=${backuppath}/${instancename}
oldstamp=`date +"%Y-%m-%d" -d "-5 day"` oldbackname=${backname}-${oldstamp}.dump
dbpath=/data/mongodb/bin
backuplogname=/tmp/mongodb_bakcup_${stamp}.log
localname=`hostname`
localip=`ip a |grep global| head -1| awk '{print $2}'|awk -F'/' '{print $1}'`
dest_mail='test@qq.com'
if [ -e ${backuplogname} ];then sudo rm -rf ${backuplogname} else : fi
send_fail_mail(){ echo "${programname}备份失败,登录服务器${localname}-${localip}的日志${backuplogname}下查看报错!!!" |mail -s ${programname}数据库备份情况 ${dest_mail} }
send_success_mail(){ echo "${programname}备份成功,可登录服务器${localname}-${localip}的日志${backuplogname}下查看备份信息。" |mail -s ${programname}数据库备份情况 ${dest_mail} }
date_func(){ dateresult=$((${afterstamp}-${beforstamp})) hour=$((${dateresult}/3600)) min=$(((${dateresult}-${hour}*3600)/60)) sec=$((${dateresult}-${hour}*3600-${min}*60)) echo "6、本次备份运行时间为:"${hour}时${min}分${sec}秒 >> ${backuplogname} }
del_old_bakdata(){ if [ -e ${oldbackname} ];then sudo echo "7、五天前的备份数据${oldbackname}存在,进行删除" >> ${backuplogname} rm -rf ${oldbackname} if [ $? == 0 ];then sudo echo "7.1、备份数据删除成功" >> ${backuplogname} else sudo echo "7.1、备份数据删除失败,注意查看失败原因" >> ${backuplogname} fi else sudo echo "7、五天前的备份数据${oldbackname}不存在,不需要再删除。" >> ${backuplogname} fi }
rename_backname_func(){ if [ -e ${backname}-${stamp} ];then mv ${backname}-${stamp} ${backname}-${stamp}-old mv ${backname} ${backname}-${stamp} if [ $? == 0 ];then echo "5、备份目录重命名成功" >> ${backuplogname} else echo "5、备份目录重命名失败,查看命名失败原因!" >> ${backuplogname} fi else mv ${backname} ${backname}-${stamp} if [ $? == 0 ];then echo "5、备份目录重命名成功" >> ${backuplogname} else echo "5、备份目录重命名失败,查看命名失败原因!" >> ${backuplogname} fi fi }
if [ -d ${backuppath} ];then sudo echo "1、备份目录存在,可执行备份操作" >> ${backuplogname} else sudo echo "1、备份目录不存在,手动创建" >> ${backuplogname} sudo mkdir -p ${backuppath} if [ $? == 0 ];then sudo echo "1.1、备份目录创建成功,可进行下面操作" >> ${backuplogname} else sudo echo "1.1、备份目录不存在,且创建失败,无法进行下面操作,退出备份" >> ${backuplogname} exit fi fi
if [ -d ${backname} ];then sudo echo "2、指定的备份实例目录${backname}已存在,对其进行重命名" >> ${backuplogname} mv ${backname} ${backuppath}/bak-${fullbackupname}-${stamp}.dump if [ $? == 0 ];then sudo echo "2.1、文件已重新命名为${backuppath}/bak-${fullbackupname}-${stamp}.dump,可继续备份" >> ${backuplogname} else sudo echo "2.1、文件重命名失败,退出备份操作" >> ${backuplogname} exit fi else sudo echo "2、指定的备份文件不存在,开始进行备份" >> ${backuplogname} fi
beforstamp=`date +%s`
sudo echo "3、开始全量备份数据库" >> ${backuplogname} ${dbpath}/mongodump -h ${servername} -u ${authuser} -p ${authpass} -d ${instancename} -o ${backuppath} --authenticationDatabase ${authdbname} backupresult=$? afterstamp=`date +%s` if [ ${backupresult} == 0 ];then sudo echo "4、${programname}-${instancename}数据库备份成功,文件名称为${backname},重命名备份文件" >> ${backuplogname} rename_backname_func date_func del_old_bakdata send_success_mail else sudo echo "4、${programname}-${instancename}数据库备份失败,注意查看备份失败原因并重新备份" >> ${backuplogname} send_fail_mail exit fi
|
使用该脚本需要修改自己数据库对应的用户名authuser,密码authpass,邮箱dest_mail,项目名称programname,服务器名称servername,数据库实例名称instancename,mongodb数据库安装路径dbpath。其他参数可使用脚本内默认的,也可自行修改
2、配置邮件发送功能
2.1、安装mailx软件
2.2、修改配置文件/etc/mail.rc,在行尾添加一下信息:
1 2
| set from=test@qq.com smtp=smtp.qq.com set smtp-auth-user=test@qq.com smtp-auth-password=testpassword smtp-auth=login
|
参数详解:
1 2 3 4
| from:指定发送邮件的发件人 smtp:指定smtp服务器信息 smtp-auth-user:允许第三方登录的用户名 smtp-auth-password:允许第三方登录的密码
|
2.3、发送测试邮件:
1
| echo 'test email' |mail -s 'title' test@qq.com
|
title:指定发送文件的标题,可自行定义
3、修改备份脚本权限
1
| chmod +x /data/soft/mongodb_backup.sh
|
4、将脚本添加到定时任务
如:每天凌晨一点备份
1
| 0 1 * * * sh /data/soft/mongodb_backup.sh
|