数据库备份,
import os,datetime
class BakDb(object):
def __init__(self,ip,username,passwd,port=3306,path=‘/tmp/db_bak‘):
self.ip = ip
self.username = username
self.passwd = passwd
self.port = port
self.path = path
self.path_exist()
self.bak_db()
def path_exist(self):
if not os.path.isdir(self.path): #不存在的话就创建
os.mkdir(self.path)
def bak_db(self):
filename = str(datetime.date.today())+‘.sql‘
abs_file = os.path.join(self.path,filename) #变成绝对路径
command = ‘‘‘
mysqldump -u{username} -p{passwd} -P{port} -h{ip} -A > {filename}
‘‘‘.format(username=self.username,
passwd=self.passwd,
port=self.port,
filename=abs_file,
ip=self.ip)
os.system(command)
print(‘done!数据库备份完成!‘)
#db1 = BakDb(‘192.168.12.1‘,‘root‘,‘123456‘)