资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python脚本按表备份MySQL数据库-创新互联

脚本功能:

公司主营业务:网站设计制作、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出武山免费做网站回馈大家。

 1.自动创建备份目录

 2.自动备份数据库的每个表,并压缩

 3.备份结果,邮件通知联系人

python脚本按表备份MySQL数据库

python脚本按表备份MySQL数据库

 4.脚本内容

#!/usr/bin/env  python # --*-- coding:UTF-8 --*-- # Create by JIANGLEI.YU on 2016/04/21 # 多表自动备份ok。发送邮件失败。 import  MySQLdb import  sys import  os import  datetime import  smtplib from    email.mime.text   import MIMEText import  sys # Define Mysql Environments Hostname='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 Environments Host='smtp.exmail.qq.com' Port=25 sender='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 e print "邮件发送失败!" def backup_failed(): global msg msg = MIMEText('数据库单表备份失败') msg['subject'] = '数据库单表备份失败' msg['From'] = sender msg['To'] = recivers email() sys.exit(2) def backup_sucess(): global msg  msg = MIMEText(('数据库单表备份成功,共备份%d张表,共用时%.2f分钟.') % (tables_count,backup_period) ,  'plain','utf-8' )  msg['subject'] = '数据库单表备份成功'   msg['From'] = sender msg['To'] = recivers email() 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 e print "连接数据库失败" backup_failed() cursor.execute('show tables') f = cursor.fetchall() list_status = [] for table in f: # print table for 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_count tables_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_period backup_period = ((end_time - start_time).seconds)/60.0 backup_sucess() else: backup_failed() cursor.close() db.close() if __name__ ==  '__main__': main()

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:python脚本按表备份MySQL数据库-创新互联
新闻来源:http://cdkjz.cn/article/dieghs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220