#!/usr/bin/env python #coding =utf8 #author: Itnihao #mail: itnihao@qq.com #source: Http://code.taoba o.org/p/python2/src/trunk/#version: 1.0 ' function: MySQL backup. Use Mysqldump to backup the libraries in mysql 1.user,pass,host,path,del parameter is variable 2. The default backup deletion cycle is 5 days, and the backup is in days 3. The default backup path is/mysql_backup, and if it does not exist it will be created Record 4. Use for the execution of permissions, timed task run ' ' Import os,subprocess,datetime ' ' Set variable ' mysql_user = ' root ' mysql_pass = ' pa SS ' Mysql_host = ' localhost ' del_days = 5 Back_path = '/mysql_backup ' ' ' Time set ' ' Cur_time = Datetime.dat
E.today () Ago_time = Datetime.timedelta (days=del_days) del_time = cur_time-ago_time DATABASE = '
' Backup function ' Def mysqldump (): If Os.path.isdir (Back_path): Os.chdir (Back_path) Else: Os.mkdir (Back_path) os.chdir (Back_path) database_cmd=subprocess. Popen ("Mysql-u%s-p%s-h%s-e ' show databases ' |grep-v database|grep-v information"% (Mysql_user,Mysql_pass,mysql_host), stdout=subprocess.
pipe,shell=true) Database_name=database_cmd.stdout.read (). Split () for the DATABASE in database_name: Mysqldump_filename= "/mysql_backup/%s%s.sql"% (cur_time,database) subprocess.call ("mysqldump-u%s-p%s-h%s %s>%s "% (mysql_user,mysql_pass,mysql_host,database,mysqldump_filename), shell=true) if Os.path.isfile (' ${DEL _time}${database}.sql '): Subprocess.call ("rm ${del_time}${database}.sql", shell=true) mysqldum P ()
Script Download Address http://code.taobao.org/p/python2/src/trunk/mysql_backup.py