数据库备份,修改父类的方法
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')
修改父类的方法
import redis
class Coon(object):
#基类
def __init__(self,host,passwd,port):
self.host= host
self.passwd=passwd
self.port= port
class CoonMySql(Coon):
def __init__(self,host,passwd,port,username,db,charset='utf8'):
#Coon.__init__(self,host,passwd,port) #z在调用父类的构造方法,咱们自己手动调用父类的方法
super(CoonMySql,self).__init__(host,passwd,port) #super会自动找到父类,然后调用下面的方法
self.username=username
self.db=db
self.charset=charset
def coon_mysql(self):
print(self.host,self.host)
class CoonRedis(Coon):
def conn(self,host,passwd,port):
Coon.conn(self, host, passwd, port)
redis.Redis(host=self.host,password=self.passwd,port=self.port)