def backup_db_mysql(db): db_filename = db['database_name'] + '_' + get_datestr() + '.sql' db_filepath = TEMP_SAVE_PATH + os.path.sep + db_filename archive_type = db['archive_type'] if archive_type not in 'zip,tar,gztar': log('archive_type存档类型"' + archive_type + '"错误,应该为zip,tar,gztar') return None host = '' if not db['host'] else '-h ' + db['host'] mysqldump = 'mysqldump' if not db['mysqldump_path'] else db[ 'mysqldump_path'] cmd = '{0} {1} -P{2} -u{3} -p{4} --databases {5} > {6}'.format( mysqldump, host, db['port'], db['username'], db['password'], db['database_name'], db_filepath) status, result = subprocess.getstatusoutput(cmd) if status != 0: log('备份数据库{0}出错,返回值为{1},执行的命令为{2}'.format(db['database_name'], result, cmd)) return None else: flag, msg = FileHelper.compress(archive_type, db_filepath, TEMP_SAVE_PATH, db_filename, db['archive_password']) archive_path = msg if flag: FileHelper.delete(db_filepath) return archive_path else: log('打包数据库文件出错,' + msg) return None
def backup_db_mssql(db): db_filename = db['database_name'] + '_' + get_datestr() + '.bak' db_filepath = TEMP_SAVE_PATH + os.path.sep + db_filename archive_type = db['archive_type'] if archive_type not in 'zip,tar,gztar': log('archive_type存档类型"' + archive_type + '"错误,应该为zip,tar,gztar') return None sqlcmd = 'sqlcmd' if not db['sqlcmd_path'] else db['sqlcmd_path'] cmd = '{0} -S {1},{2} -U {3} -P {4} -Q "BACKUP DATABASE {5} to disk="{6}"'.format( sqlcmd, db['host'], db['port'], db['username'], db['password'], db['database_name'], db_filepath) status, result = subprocess.getstatusoutput(cmd) if status != 0: log('备份数据库{0}出错,返回值为{1},执行的命令为{2}'.format(db['database_name'], result, cmd)) return None else: flag, msg = FileHelper.compress(archive_type, db_filepath, TEMP_SAVE_PATH, db_filename, db['archive_password']) archive_path = msg if flag: FileHelper.delete(db_filepath) return archive_path else: log('打包数据库文件出错,' + msg) return None
def backup_site(): site_files = [] log('开始备份站点') for site in SITES: if not site: continue site_path = site['path'] if site['type'] == 'ftp': ftp = FtpHelper(site['host'],site['username'], site['password'],site['port']) log('开始下载FTP远程目录:' + site['path']) ftp.download_dir(os.path.join(TEMP_SAVE_PATH ,os.path.basename(site['path'])),site['path']) log('下载FTP远程目录结束') site_path = os.path.join(TEMP_SAVE_PATH,os.path.basename(site['path'])) archive_type = site['archive_type'] if not os.path.exists(site_path): log('站点路径%s不存在' % site_path) continue if archive_type not in 'zip,tar,gztar': log('archive_type存档类型"' + archive_type + '"错误,应该为zip,tar,gztar') continue dirname = os.path.basename(site_path) site_filename = dirname + '_' + get_datestr() flag,msg = FileHelper.compress(archive_type,site_path,TEMP_SAVE_PATH,site_filename,site['archive_password']) if site['type'] == 'ftp': FileHelper.delete(site_path) if not flag: log('创建' + site_path + '存档出错:' + msg) continue site_archive_path = msg site_files.append(site_archive_path) log('备份站点结束') return site_files
def remote_save_email(site_files, db_files): log('开始发送到Email') for option in EMAIL_OPTIONS_SENDERS: email = EmailHelper(option['host'], option['username'], option['password'], option['port'], option['is_ssl']) for file in site_files: if not file: continue if option['partSize']: part_file_path = os.path.join(TEMP_SAVE_PATH, 'EmailPart') if not os.path.exists(part_file_path): os.makedirs(part_file_path) flag, msg = FileHelper.compress(option['archive_type'], file, part_file_path, os.path.basename(file), None, None, None, None, option['partSize']) part_files = FileHelper.get_file_list(part_file_path) for part_file in part_files: flag, msg = email.send( '新的站点备份', '站点备份:' + os.path.basename(part_file), '站点备份:' + os.path.basename(part_file), EMAIL_OPTIONS_RECEIVERS, [part_file]) FileHelper.delete(part_file_path) else: flag, msg = email.send('新的站点备份', '站点备份:' + os.path.basename(file), '站点备份:' + os.path.basename(file), EMAIL_OPTIONS_RECEIVERS, [file]) if flag: log('使用 {0} 发送邮件 {1} 成功'.format(option['username'], file)) else: log('使用 {0} 发送邮件 {1} 失败,原因:'.format(option['username'], file, msg)) for file in db_files: if not file: continue if option['partSize']: if not os.path.exists(part_file_path): os.makedirs(part_file_path) part_file_path = os.path.join(TEMP_SAVE_PATH, 'EmailPart') flag, msg = FileHelper.compress(option['archive_type'], file, part_file_path, os.path.basename(file), None, None, None, None, option['partSize']) part_files = FileHelper.get_file_list(part_file_path) for part_file in part_files: flag, msg = email.send( '新的数据库备份', '数据库备份:' + os.path.basename(part_file), '数据库备份:' + os.path.basename(part_file), EMAIL_OPTIONS_RECEIVERS, [part_file]) FileHelper.delete(part_file_path) else: flag, msg = email.send('新的数据库备份', '数据库备份:' + os.path.basename(file), '数据库备份:' + os.path.basename(file), EMAIL_OPTIONS_RECEIVERS, [file]) if flag: log('使用 {0} 发送邮件 {1} 成功'.format(option['username'], file)) else: log('使用 {0} 发送邮件 {1} 失败,原因:'.format(option['username'], file, msg)) email.quit() log('发送到Email 完成')