python脚本按表备份MySQL数据库
发表于:2024-10-27 作者:千家信息网编辑
千家信息网最后更新 2024年10月27日,脚本功能:1.自动创建备份目录2.自动备份数据库的每个表,并压缩3.备份结果,邮件通知联系人4.脚本内容#!/usr/bin/env python# --*-- coding:UTF-8 --*--
千家信息网最后更新 2024年10月27日python脚本按表备份MySQL数据库
脚本功能:
1.自动创建备份目录
2.自动备份数据库的每个表,并压缩
3.备份结果,邮件通知联系人
4.脚本内容
#!/usr/bin/env python# --*-- coding:UTF-8 --*--# Create by JIANGLEI.YU on 2016/04/21# 多表自动备份ok。发送邮件失败。import MySQLdbimport sysimport osimport datetimeimport smtplibfrom email.mime.text import MIMETextimport sys# Define Mysql EnvironmentsHostname='192.168.0.141'Username='root'Password='123456'Database='virtual'MYSQLDUMP='/usr/bin/mysqldump'GZIP='/usr/bin/gzip'timestamp=datetime.datetime.now().strftime("%Y%m%d%H%M%S")Destination_dir='/home/bak/tables/' + timestamp + '/'# Define Smtp EnvironmentsHost='smtp.exmail.qq.com'Port=25sender='yujianglei@singulax.com'Pass='123456'recivers='jianglei.yu@foxmail.com'def main():if os.path.exists(Destination_dir) == False:os.makedirs(Destination_dir)db_table_backup()else:db_table_backup()def email():try:server= smtplib.SMTP()server.connect(Host,Port)server.login(sender,Pass)server.sendmail(sender,recivers,msg.as_string())except Exception,e:print eprint "邮件发送失败!"def backup_failed():global msgmsg = MIMEText('数据库单表备份失败')msg['subject'] = '数据库单表备份失败'msg['From'] = sendermsg['To'] = reciversemail()sys.exit(2)def backup_sucess():global msg msg = MIMEText(('数据库单表备份成功,共备份%d张表,共用时%.2f分钟.') % (tables_count,backup_period) , 'plain','utf-8' ) msg['subject'] = '数据库单表备份成功' msg['From'] = sendermsg['To'] = reciversemail()def db_table_backup():start_time=datetime.datetime.now()try:db = MySQLdb.connect(Hostname,Username,Password,Database,connect_timeout=2)cursor = db.cursor()except Exception, e:# print eprint "连接数据库失败"backup_failed()cursor.execute('show tables')f = cursor.fetchall()list_status = []for table in f:# print tablefor i in table:MYSQLDUMP_CMD = MYSQLDUMP + ' -h' + Hostname + ' -u' + Username + ' -p' + Password + ' ' + Database + ' ' + i + ' ' + '| ' + GZIP + ' >' + Destination_dir + Database + '-' + timestamp+ '.' + i + '.' + 'sql.gz'result = os.system(MYSQLDUMP_CMD)list_status.append(result)global tables_counttables_count = len(list_status)list_test=[0]j = set(list_status).issubset(set(list_test))if j == True:end_time=datetime.datetime.now()global backup_periodbackup_period = ((end_time - start_time).seconds)/60.0backup_sucess()else:backup_failed()cursor.close()db.close()if __name__ == '__main__':main()
备份
数据
数据库
邮件
脚本
成功
内容
功能
目录
结果
联系人
UTF-8
联系
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
关系模型就是关系数据库对还是错
服务器安全设置实例
我的世界韩国服务器
长宁区品质网络技术型号
PB软件开发工具可否转让
软件开发做什么挣钱
网络安全体系概念
网络安全专业电大
数据库开发工程师很累吗
数据库自动备份命令
基础安全服务器设备的介绍
腾讯服务器不升级怎么回事
畅捷通数据库维护
目前戴尔塔式服务器最新是几代
普通内存安装到服务器上
视频直播系统软件开发
车车科技互联网金融协会
数字化网络安全底座
网络安全人人有责演讲
3d游戏服务器
服务器数据库地址
江苏潮流软件开发服务应用
研究网络安全与信息化工作
治理数据库建设
网络安全小知识一百字
武汉科技大学网络技术试卷
日志导入数据库怎么做
湖南工商学院数据库招标
数字化网络安全底座
互联网科技营销策划