def clear_old_backup(): if 'ftp' in REMOTE_SAVE_TYPE: #清除远程FTP上旧备份文件 for option in FTP_OPTIONS: ftp = FtpHelper(option['host'], option['username'], option['password'], option['port'], option['pasv']) for filename in ftp.get_files(option['site_save_path']): if is_oldfile(filename): ftp.delete_file(option['site_save_path'], filename) for filename in ftp.get_files(option['db_save_path']): if is_oldfile(filename): ftp.delete_file(option['db_save_path'], filename) ftp.quit() #清除本地网站旧文件 for root, dirs, files in os.walk(LOCAL_SAVE_PATH['sites']): for filename in files: if is_oldfile(filename): FileHelper.delete(os.path.join(root, filename)) #清除本地数据库旧文件 for root, dirs, files in os.walk(LOCAL_SAVE_PATH['databases']): for filename in files: if is_oldfile(filename): FileHelper.delete(os.path.join(root, filename)) if 'oss' in REMOTE_SAVE_TYPE: #清除oss旧文件 for option in OSS_OPTIONS: oss = OssHelper(option['accesskeyid'], option['accesskeysecret'], option['url'], option['bucket']) for file in oss.get_file_list( option['sitedir'].rstrip('/') + '/') + oss.get_file_list( option['databasedir'].rstrip('/') + '/'): if is_oldfile(os.path.basename(file)): oss.delete(file) if 'css' in REMOTE_SAVE_TYPE: #清除cos旧文件 for option in COS_OPTIONS: cos = CosHelper(option['accesskeyid'], option['accesskeysecret'], option['region'], option['bucket']) for file in cos.get_file_list( option['sitedir'].rstrip('/') + '/') + cos.get_file_list( option['databasedir'].rstrip('/') + '/'): if is_oldfile(os.path.basename(file)): cos.delete(file) if 'onedrive' in REMOTE_SAVE_TYPE: #清除onedrive旧文件 for option in ONE_DRIVE_OPTION: od = OneDriveHelper(option['name']) for file in od.get_file_list(option['sitedir'].rstrip('/') + '/'): if is_oldfile(os.path.basename(file['name'])): od.delete(os.path.join(option['sitedir'], file['name'])) for file in od.get_file_list(option['databasedir'].rstrip('/') + '/'): if is_oldfile(os.path.basename(file['name'])): od.delete(os.path.join(option['databasedir'], file['name'])) log('清除旧备份文件 完成')
def clear_old_backup(config, db_file, task_name): if 'oss' in config: # 清除oss旧文件 ossConf = config.oss oss = OssHelper(ossConf.accessKey, ossConf.secretKey, ossConf.url, ossConf.bucket) file_list = oss.get_file_list(os.path.join(ossConf.prefix, task_name)) file_dict = dict( zip( map(lambda x: os.path.basename(x['name']).split(".")[0], file_list), file_list)) ret_file_dict = clean_rule(file_dict, config.oss) for key, value in ret_file_dict.items(): if value: continue delete_file = ossConf.prefix + task_name + "/" + file_dict[key][ 'name'] logger.info(f'delete remote file {delete_file}') oss.delete(delete_file) if 'local' in config: local_path = os.path.dirname(db_file) zip_files = os.listdir(local_path) file_dict = dict( zip(map(lambda x: x.split(".")[0], zip_files), zip_files)) ret_file_dict = clean_rule(file_dict, config.local) for key, value in ret_file_dict.items(): if value: continue delete_file = os.path.join(local_path, file_dict[key]) logger.info(f'delete local file {delete_file}') os.remove(delete_file) # 删除value为空的文件 # else: # #清除本地文件, 默认保留时间是365天 # expireDays = config.expireDays if config.expireDays else 365 # for zip_file in zip_files: # # 文件创建时间 # create_time = datetime.datetime.fromtimestamp(os.path.getctime(zip_file)) # # 计算文件现在的差值 # diff_days = (datetime.datetime.now() - create_time).days # if diff_days > expireDays: # os.remove(zip_file) else: # 没有配置local,表示不进行本地存档 os.remove(db_file)