python实现mysql备份的脚本

Posted on 2016-01-19 22:51  心在北方  阅读(431)  评论(0编辑  收藏  举报

#!/usr/bin/env python                                                                                                                           

# encoding: utf-8
     

import os,time,sys
     
bakup_dir = '/home/bak/mysql'
     

current_time = time.strftime('%Y%m%d%H%M%S')
     
user = 'root'
    

password = 'root'
    
database = ['mysql','aaa']
    

if os.path.exists(bakup_dir):
    
        print "The path %s exists" % bakup_dir
    
else :
    
        os.makedirs(bakup_dir)
    
        print "The path %s create sucessful" % bakup_dir
    
 os.chdir(bakup_dir)


 for i in range(len(database)):
    
        a = database[i]
    
        mysqlbak_cmd = "mysqldump -u%s -p%s --default-character-set=utf8  %s > %s%s.sql" % (user,password,a,current_time,a)
    
        os.system(mysqlbak_cmd)
 

print "backup sucessful"

Copyright © 2024 心在北方
Powered by .NET 9.0 on Kubernetes