It is not very difficult to write a Python backup script for MySQL today. A lot more concise than the shell!
Open Whole:
Comments are written in English, some English-based friends should be able to read!
#!/usr/bin/env python#backup the gtshop#author:ley#encoding=utf8#date:2015-06import os,sys , datetime,timefrom stat import * #mysqlbackup userUser = ' root ' #mysqlbackup passwordpassword = ' root ' #mysqlbackup commandMysqlcommand = '/usr/local/mysql/bin/ Mysqldump ' #gzip command Gzipcommand = '/bin/gzip ' #backup mysql databasemysqldata = [' gtshop ' #backup dirTobackup = '/home/gtshop_backup/' for db in Mysqldata: #backup file nameBackupfile = Tobackup + DB + '-' + time.strftime ('%y-%m-%d ') + '. sql ' #gzip file namegzipfile = backupfile + '. Gz ' If os.path.isfile (gzipfile):p rint gzipfile + "is already Backup "Else: #backup commandBack_command = Mysqlcommand + ' -u ' + user + ' -p ' + Password + ' --events ' + ' --master-data=2 ' + ' --single-transaction ' + DB + ' > ' + Backupfileif Os.system (Back_command) == 0: print ' Sucessful backup gtshop ' else: print ' backup failed ' #gzip commandGzip_command = Gzipcommand + ' ' + backupfile if os.system (Gzip_command) == 0: print ' Sucessful gzip gtshop ' else:print ' gzip failed '
Execution Result:
[[email protected] script]# python mysql_backup.py sucessful backup gtshopsucessful gzip gtshop
Finally, it can be added to the task plan according to the requirements!
This article is from the "Liang Enyu-9527" blog, be sure to keep this source http://liangey.blog.51cto.com/9097868/1662026
Python backup MySQL Script