def addServiceRun(self): ''' 检查备份脚本是否为自启动服务,如果不是需要添加到系统自启动服务。 ''' if utils.is_windows(): scriptdir = 'C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows \ \\Start Menu\\Programs\\Startup\\' copy = 'copy ' elif utils.is_linux(): scriptdir = '/etc/init.d/' copy = 'cp ' else: print 'Unkown System Type!' filelist = os.listdir(scriptdir) dbtype = self.serInfo['dbtype'][0] if dbtype == 'MySQL': bakscript = 'mysqlCron.py' elif dbtype == 'Oracle': bakscript = 'oracleCron.py' else: print "Unkown database type!" if bakscript not in filelist: print scriptdir os.system(copy + bakscript + ' "' + scriptdir + '"') os.system('python ' + '"' + scriptdir + bakscript + '"')
def __init__(self, conf): self.conf = conf self.dbs = {} if utils.is_windows(): self.bkdir = os.environ['TMP'] elif utils.is_linux(): self.bkdir = '/var/tmp/oraclebak' else: print 'Unkown System Type!' sys.exit(1) if not os.path.exists(self.bkdir): os.mkdir(self.bkdir)
def _get_logbin_path(self): ''' 获取MySQL二进制日志文件的路径,要求在mysql的配置文件中设定log-bin选项。这里有三种情况: 1. log-bin= :即为空,此时MySQL自动将日志文件存放到$datadir中,并以$hostname-bin.index/0000001的格式命令。 2. log-bin=filename.* : 指定文件名,此时日志文件同样存放到$datadir中,但是以filename.index/0000001的格式命令。 3. log-bin=filepath/filename.* : 此时日志文件存放到filepath路径下,以filename.index/0000001的格式命令。 ''' if utils.is_windows(): try: cursor = self.conn.cursor() cursor.execute("show variables like 'basedir'") basedir = cursor.fetchall()[0][1] inipath = basedir + 'my.ini' except MySQLdb.Error, e: print 'MySQL Error %d: %s' % (e.args[0], e.args[1])
def bk_now(self, tables=None): ''' 使用逻辑备份导出指定用户的数据表,参数tables为表名元组,默认为空时导出整个用户空间。 ''' timestamp = str( time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))) if utils.is_windows(): bak_dir = self.bkdir + '\\' + timestamp dmp_file = bak_dir + '\\' + timestamp + '_exp.dmp' log_file = bak_dir + '\\' + timestamp + '_exp.log' options = self.conf['seruser'] + '/' + self.conf['serpass'] + ' buffer=64000 file=' \ + dmp_file + ' log=' + log_file if tables is not None: options += ' tables=' + tables else: options += ' owner=' + self.conf['seruser'] os.mkdir(bak_dir) if os.system('exp ' + options) == 0: zip_file = bak_dir + '\\' + timestamp + '_exp.zip' myzipfile = zipfile.ZipFile(zip_file, 'w') myzipfile.write(dmp_file) myzipfile.close() return True, zip_file else: print 'backup oprations failed!' return False, None elif utils.is_linux(): bak_dir = self.bkdir + '/' + timestamp dmp_file = bak_dir + '/' + timestamp + '_exp.dmp' log_file = bak_dir + '/' + timestamp + '_exp.log' options = self.conf['seruser'] + '/' + self.conf['serpass'] + ' buffer=64000 file=' \ + dmp_file + ' log=' + log_file if tables is not None: options += ' tables=' + tables else: options += ' owner=' + self.conf['seruser'] os.mkdir(bak_dir) if os.system('exp ' + options) == 0: tar_file = bak_dir + '/' + timestamp + '_exp.tar.gz' tarfile = tarfile.open(tar_file, 'w:gz') tarfile.add(dmp_file) tarfile.close() return True, tar_file else: print 'Unkown System Type!' sys.exit(1)
def bk_now(self, tables=None): ''' 使用逻辑备份导出指定用户的数据表,参数tables为表名元组,默认为空时导出整个用户空间。 ''' timestamp = str(time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))) if utils.is_windows(): bak_dir = self.bkdir + '\\' + timestamp dmp_file = bak_dir + '\\' + timestamp + '_exp.dmp' log_file = bak_dir + '\\' + timestamp + '_exp.log' options = self.conf['seruser'] + '/' + self.conf['serpass'] + ' buffer=64000 file=' \ + dmp_file + ' log=' + log_file if tables is not None: options += ' tables=' + tables else: options += ' owner=' + self.conf['seruser'] os.mkdir(bak_dir) if os.system('exp ' + options) == 0: zip_file = bak_dir + '\\' + timestamp + '_exp.zip' myzipfile = zipfile.ZipFile(zip_file, 'w') myzipfile.write(dmp_file) myzipfile.close() return True, zip_file else: print 'backup oprations failed!' return False, None elif utils.is_linux(): bak_dir = self.bkdir + '/' + timestamp dmp_file = bak_dir + '/' + timestamp + '_exp.dmp' log_file = bak_dir + '/' + timestamp + '_exp.log' options = self.conf['seruser'] + '/' + self.conf['serpass'] + ' buffer=64000 file=' \ + dmp_file + ' log=' + log_file if tables is not None: options += ' tables=' + tables else: options += ' owner=' + self.conf['seruser'] os.mkdir(bak_dir) if os.system('exp ' + options) == 0: tar_file = bak_dir + '/' + timestamp + '_exp.tar.gz' tarfile = tarfile.open(tar_file, 'w:gz') tarfile.add(dmp_file) tarfile.close() return True, tar_file else: print 'Unkown System Type!' sys.exit(1)
def incr_bak(self): timestamp = str( time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))) if utils.is_windows(): dump_dir = self.bkdir + '\\' + timestamp zip_file_path = dump_dir + '\\' + timestamp + '_incr.zip' log_file = dump_dir + '\\' + timestamp + '_incr.log' options = '-h' + self.conf['serip'] + ' -u' + self.conf['seruser'] \ + ' -p' + self.conf['serpass'] + ' flush-logs' os.mkdir(dump_dir) if os.system('mysqladmin ' + options) == 0: logbin_file = self._get_logbin_last() print logbin_file myzipfile = zipfile.ZipFile(zip_file_path, 'w') myzipfile.write(logbin_file) myzipfile.close() os.system('echo "DataBase Backup Success!" >> ' + log_file) return True, zip_file_path else: os.system('echo "DataBase Backup Failed! >> ' + log_file) return False, None elif utils.is_linux(): dump_dir = self.bkdir + '/' + timestamp tar_file = dump_dir + '/' + timestamp + '_incr.tar.gz' log_file = dump_dir + '/' + timestamp + '_incr.log' options = '-h' + self.conf['serip'] + ' -u' + self.conf['seruser'] \ + ' -p' + self.conf['serpass'] + ' flush-logs' os.mkdir(dump_dir) if os.system('mysqladmin ' + options) == 0: logbin_file = self._get_logbin_last() tarfile = tarfile.open(tar_file, 'w:gz') tarfile.add(logbin_file) tarfile.close() os.system('echo "DataBase Backup Success!" >> ' + log_file) return True, tar_file else: os.system('echo "DataBase Backup Failed! >> ' + log_file) return False, None else: print 'Unkown System Type!' sys.exit(1) pass
def glob_bak(self): timestamp = str( time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))) if utils.is_windows(): dump_dir = self.bkdir + '\\' + timestamp dump_file = dump_dir + '\\' + timestamp + '_glob.sql' zip_file_path = dump_dir + '\\' + timestamp + '_glob.zip' log_file = dump_dir + '\\' + timestamp + '_glob.log' options = '-h' + self.conf['serip'] + ' -u' + self.conf['seruser'] \ + ' -p' + self.conf['serpass'] + ' --all-databases --flush-logs --master-data=2' os.mkdir(dump_dir) os.system('purge master logs') if os.system('mysqldump ' + options + ' > ' + dump_file) == 0: myzipfile = zipfile.ZipFile(zip_file_path, 'w') myzipfile.write(dump_file) myzipfile.close() os.system('echo "DataBase Backup Success!" >> ' + log_file) return True, zip_file_path else: os.system('echo "DataBase Backup Failed! >> ' + log_file) return False, None elif utils.is_linux(): dump_dir = self.bkdir + '/' + timestamp dump_file = dump_dir + '/' + timestamp + '_glob.sql' tar_file = dump_dir + '/' + timestamp + '_glob.tar.gz' log_file = dump_dir + '/' + timestamp + '_glob.log' options = '-h' + self.conf['serip'] + ' -u' + self.conf['seruser'] \ + ' -p' + self.conf['serpass'] + ' --all-databases --flush-logs --master-data=2' os.mkdir(dump_dir) os.system('purge master logs') if os.system('mysqldump ' + options + ' > ' + dump_file) == 0: tarfile = tarfile.open(tar_file, 'w:gz') tarfile.add(dump_file) tarfile.close() os.system('echo "DataBase Backup Success!" >> ' + log_file) return True, tar_file else: os.system('echo "DataBase Backup Failed! >> ' + log_file) return False, None else: print 'Unkown System Type!' sys.exit(1)
def incr_bak(self): timestamp = str(time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))) if utils.is_windows(): dump_dir = self.bkdir + '\\' + timestamp zip_file_path = dump_dir + '\\' + timestamp + '_incr.zip' log_file = dump_dir + '\\' + timestamp + '_incr.log' options = '-h' + self.conf['serip'] + ' -u' + self.conf['seruser'] \ + ' -p' + self.conf['serpass'] + ' flush-logs' os.mkdir(dump_dir) if os.system('mysqladmin ' + options) == 0: logbin_file = self._get_logbin_last() print logbin_file myzipfile = zipfile.ZipFile(zip_file_path, 'w') myzipfile.write(logbin_file) myzipfile.close() os.system('echo "DataBase Backup Success!" >> ' + log_file) return True, zip_file_path else: os.system('echo "DataBase Backup Failed! >> ' + log_file) return False, None elif utils.is_linux(): dump_dir = self.bkdir + '/' + timestamp tar_file = dump_dir + '/' + timestamp + '_incr.tar.gz' log_file = dump_dir + '/' + timestamp + '_incr.log' options = '-h' + self.conf['serip'] + ' -u' + self.conf['seruser'] \ + ' -p' + self.conf['serpass'] + ' flush-logs' os.mkdir(dump_dir) if os.system('mysqladmin ' + options) == 0: logbin_file = self._get_logbin_last() tarfile = tarfile.open(tar_file, 'w:gz') tarfile.add(logbin_file) tarfile.close() os.system('echo "DataBase Backup Success!" >> ' + log_file) return True, tar_file else: os.system('echo "DataBase Backup Failed! >> ' + log_file) return False, None else: print 'Unkown System Type!' sys.exit(1) pass
def glob_bak(self): timestamp = str(time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))) if utils.is_windows(): dump_dir = self.bkdir + '\\' + timestamp dump_file = dump_dir + '\\' + timestamp + '_glob.sql' zip_file_path = dump_dir + '\\' + timestamp + '_glob.zip' log_file = dump_dir + '\\' + timestamp + '_glob.log' options = '-h' + self.conf['serip'] + ' -u' + self.conf['seruser'] \ + ' -p' + self.conf['serpass'] + ' --all-databases --flush-logs --master-data=2' os.mkdir(dump_dir) os.system('purge master logs') if os.system('mysqldump ' + options + ' > ' + dump_file) == 0: myzipfile = zipfile.ZipFile(zip_file_path, 'w') myzipfile.write(dump_file) myzipfile.close() os.system('echo "DataBase Backup Success!" >> ' + log_file) return True, zip_file_path else: os.system('echo "DataBase Backup Failed! >> ' + log_file) return False, None elif utils.is_linux(): dump_dir = self.bkdir + '/' + timestamp dump_file = dump_dir + '/' + timestamp + '_glob.sql' tar_file = dump_dir + '/' + timestamp + '_glob.tar.gz' log_file = dump_dir + '/' + timestamp + '_glob.log' options = '-h' + self.conf['serip'] + ' -u' + self.conf['seruser'] \ + ' -p' + self.conf['serpass'] + ' --all-databases --flush-logs --master-data=2' os.mkdir(dump_dir) os.system('purge master logs') if os.system('mysqldump ' + options + ' > ' + dump_file) == 0: tarfile = tarfile.open(tar_file, 'w:gz') tarfile.add(dump_file) tarfile.close() os.system('echo "DataBase Backup Success!" >> ' + log_file) return True, tar_file else: os.system('echo "DataBase Backup Failed! >> ' + log_file) return False, None else: print 'Unkown System Type!' sys.exit(1)
def __init__(self, conf): ''' Constructor ''' if utils.is_windows(): self.bkdir = os.environ['TMP'] elif utils.is_linux(): self.bkdir = '/var/tmp/mysqlbak' else: print 'Unkown System Type!' sys.exit(1) if not os.path.exists(self.bkdir): os.mkdir(self.bkdir) self.conf = conf self.dbs = {} self.conn = MySQLdb.connect(host=self.conf['serip'], user=self.conf['seruser'], passwd=self.conf['serpass']) self.logbinpath = self._get_logbin_path()