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 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 remote_save_ftp(site_files,db_files): log('开始上传到FTP') for option in FTP_OPTIONS: ftp = FtpHelper(option['host'],option['username'], option['password'],option['port']) for file in site_files: if not file: continue filename = os.path.basename(file) ftp.upload_file(file,option['site_save_path'].rstrip('/') + '/' + filename) for file in db_files: if not file: continue filename = os.path.basename(file) ftp.upload_file(file,option['db_save_path'].rstrip('/') + '/' + filename) ftp.quit() log('远程保存到FTP 完成')