温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql备份脚本

发布时间:2020-05-16 21:21:56 来源:网络 阅读:612 作者:zhangguangyi 栏目:MySQL数据库

在网上看了几个脚本根据网上的稍微修改了下,稍微更准确了点,就是缺少记录日志的功能,还没想好怎么写,后续添加

#!/bin/bash
#做大备份数量
Backup_Max_Files=20
#数据库备份目录
WORKING_DIR=$(dirname "$0")
export WORKING_DIR=$(cd -P "$WORKING_DIR"/ > /dev/null; pwd)
#数据库IP地址、帐号、密码
DB_Host="XXXXXXX"
DB_User="XXXXXX"
DB_Password="XXXXXXX"
#需要备份的数据库
Databases=(
"mysql"
"zabbix"
)

#==========
echo "###########Bash Shell MySql Databases Backup Tools!"

#定义变量=当前日期
Date_Time=$(date +%F)

#定义备份文件前缀
BackDataFile=zhang"${Date_Time}"
cd $WORKING_DIR
mkdir $BackData_File
#统计需要备份数据库的个数
count=0
while [ "x${Databases[count]}" != "x" ]
do
count=$((count + 1))
echo "Databases=$count"
done
echo "[+] ${count} The database is about to be backed up"

for Databases in ${Databases[@]}
do
echo "[+] MySql-backing up: ${Databases}"
echo -n " MySql Backup Start: "
echo $(date +"%Y-%m-%d %H:%M:%S")
#使用Mysqdump工具进行逻辑备份(全备)并使用邮件脚本发送成功失败提醒
if $(mysqldump -h${DB_Host} -u${DB_User} -p${DB_Password} --single-transaction --flush-logs mysql > ${BackData_File}/${Databases}.sql )
then
echo " Success OK!: Mysql database backup success!"
python /root/jiaoben/mail.py "y@163.com" "${Databases}数据库备份成功" "${Databases}>数据库备份成功"
else
echo " Error NO!: Failure of MySQL database backup!"
#python /root/jiaoben/mail.py "y@163.com" "${Databases}数据库备份失败" "${Databases}>数据库备份失败请查看"
fi
echo -n "Backup success: "
echo $(date +"%Y-%m-%d %H:%M:%S")
done
echo
echo "[+] Packaging and compressing folders with Tar ...."
#使用tar命令打包并压缩生成的数据库文件
cd $WORKING_DIR
tar -cv ${BackData_File} | bzip2 > ${BackData_File}.tar.bz2 && rm -rf ${BackData_File}
#删除n天以前备份的数据文件

echo "[+] Delete over time database backup files ...."
find $WORKINGDIR -name "zhang*.tar.bz2" -mtime +${Backup_Max_Files} -exec rm -rf {} \;

#find $WORKINGDIR -name "zhang*.tar.bz2" -mtime +${Backup_Max_Files} -exec ls {} \;

后补:邮件报警脚本(网络版,已测试可以使用没有问题)
#!/usr/bin/python
coding=utf-8

#测试方法:python 5.py XXXXX@163.com test test
#能正常发送邮件说明成功
import smtplib
import sys
from email.mime.text import MIMEText

class send_mail():mail_host="smtp.163.com"
mail_user="XXXXXX@163.com"
br/>mail_host="smtp.163.com"
mail_user="XXXXXX@163.com"
mail_pass="XXXXXx"
mail_postfix="163.com"
def init(self,sub,content):self.me="Zabbix-Alter"+"<"+self.mail_user+"@"+self.mail_postfix+">"
br/>self.me="Zabbix-Alter"+"<"+self.mail_user+"@"+self.mail_postfix+">"
self.msg['From'] = self.me
self.msg['Subject'] = sub

def sendMessage(self,user):
    self.msg['To'] = ";".join(user)
    try:
        s = smtplib.SMTP_SSL(host=self.mail_host,port=465)
        s.login(self.mail_user,self.mail_pass)
        s.sendmail(self.me, user, self.msg.as_string())  

#发送邮件
s.close()
except Exception, e:
print str(e)

if name == 'main':
mailto_list=[]
mailto_list.append(sys.argv[1])
s = send_mail(sys.argv[2],sys.argv[3])
s.sendMessage(mailto_list)

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI