day9-Python學習筆記(二十)數據庫備份,
阿新 • • 發佈:2018-03-07
python學習 swd div pan self. 數據 筆記 tmp filename
數據庫備份,
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‘)
day9-Python學習筆記(二十)數據庫備份,