备份数据库

备份数据库命令:

mysqldump -uroot -p123456 -A >wxc123.sql 备份数据库命令

 1 import os,datetime
 2 class BakDb(object):
 3     def __init__(self,ip,username,passwd,port=3306,path='E:\db_bak'):
 4         self.ip=ip
 5         self.username=username
 6         self.passwd=passwd
 7         self.port=port
 8         self.path=path
 9         self.path_exist()
10         # self.bak_db()
11     def path_exist(self):
12         if not os.path.isdir(self.path):#不存在的话就创建
13             os.mkdir(self.path)
14     def bak_db(self):
15         #mysqldump - u%s - p%s -P%s -h%s -A > wxc123.sql
16         filename=str(datetime.date.today())+'.sql'
17         abs_file=os.path.join(self.path,filename)#变成绝对路径
18         command=r'''
19         mysqldump -u{username} -p{passwd} -P{port} -h{ip} -A > {filename}
20         '''.format(username=self.username,
21                    passwd=self.passwd,
22                    port=self.port,
23                    filename=abs_file,
24                    ip=self.ip)
25         print(command)
26         os.system(command)
27 
28         print('done!数据库备份完成!')
29 db1=BakDb('211.149.218.16','jxz','123456')
30 db1.bak_db()

 

posted @ 2018-03-05 23:53  飞鸟与新月  阅读(475)  评论(0编辑  收藏  举报