2424other_inputs = [
2525 "FILE_PREFIX" ,
2626 "BACKUP_PATH" ,
27+ "BACKUP_CLEAN_ENABLE" ,
2728 "BACKUP_SAVE_NUMS" ,
2829 "MONGO_COLLECTION" ,
2930 "MONGO_EXCLUDE_COLLECTIONS" ,
@@ -67,6 +68,9 @@ def check_bool(key):
6768backup_path = (
6869 os .environ ["BACKUP_PATH" ] if check_var ("BACKUP_PATH" ) else DEFAULT_BACKUP_PATH
6970)
71+ backup_clean_enable = (
72+ check_bool ("BACKUP_CLEAN_ENABLE" ) if check_var ("BACKUP_CLEAN_ENABLE" ) else True
73+ )
7074backup_save_nums = (
7175 int (os .environ ["BACKUP_SAVE_NUMS" ])
7276 if check_var ("BACKUP_SAVE_NUMS" )
@@ -214,34 +218,41 @@ def upload_s3(prefix):
214218 upload_path = f"{ s3_prefix } /{ file_name } " if s3_prefix else file_name
215219 client .upload_file (f"{ backup_path } /{ file_name } " , s3_bucket , upload_path )
216220
217- # 清理 S3 上的多余备份文件
218- list_prefix = f"{ s3_prefix } /" if s3_prefix else ""
219- resp = client .list_objects_v2 (Bucket = s3_bucket , Prefix = list_prefix , Delimiter = "/" )
220- if "Contents" in resp :
221- objects = resp ["Contents" ]
222-
223- file_prefix = f"{ list_prefix } { prefix } "
224- # 构造正则表达式
225- regex_pattern = f"^{ re .escape (file_prefix )} (\\ d{{14}})\\ .tar\\ .gz(\\ .crypt)?$"
226- compiled_regex = re .compile (regex_pattern )
227- keys = [
228- object ["Key" ] for object in objects if compiled_regex .match (object ["Key" ])
229- ]
230-
231- # 根据文件名排序(这使得最新的备份文件位于列表的末尾)
232- keys .sort ()
233-
234- # 确定需要删除的文件(保留最后N个文件,这里是backup_save_nums)
235- keys_to_remove = keys [:- backup_save_nums ]
236-
237- # 删除旧的备份文件
238- if keys_to_remove :
239- for key in keys_to_remove :
240- client .delete_object (
241- Bucket = s3_bucket ,
242- Key = key ,
243- )
244- print (f"Deleted s3 old backup files: { keys_to_remove } " )
221+ if backup_clean_enable :
222+ # 清理 S3 上的多余备份文件
223+ list_prefix = f"{ s3_prefix } /" if s3_prefix else ""
224+ resp = client .list_objects_v2 (
225+ Bucket = s3_bucket , Prefix = list_prefix , Delimiter = "/"
226+ )
227+ if "Contents" in resp :
228+ objects = resp ["Contents" ]
229+
230+ file_prefix = f"{ list_prefix } { prefix } "
231+ # 构造正则表达式
232+ regex_pattern = (
233+ f"^{ re .escape (file_prefix )} (\\ d{{14}})\\ .tar\\ .gz(\\ .crypt)?$"
234+ )
235+ compiled_regex = re .compile (regex_pattern )
236+ keys = [
237+ object ["Key" ]
238+ for object in objects
239+ if compiled_regex .match (object ["Key" ])
240+ ]
241+
242+ # 根据文件名排序(这使得最新的备份文件位于列表的末尾)
243+ keys .sort ()
244+
245+ # 确定需要删除的文件(保留最后N个文件,这里是backup_save_nums)
246+ keys_to_remove = keys [:- backup_save_nums ]
247+
248+ # 删除旧的备份文件
249+ if keys_to_remove :
250+ for key in keys_to_remove :
251+ client .delete_object (
252+ Bucket = s3_bucket ,
253+ Key = key ,
254+ )
255+ print (f"Deleted s3 old backup files: { keys_to_remove } " )
245256
246257
247258try :
@@ -259,8 +270,9 @@ def upload_s3(prefix):
259270 print ("backup end" )
260271
261272 # 2. 清理备份,保留最近的若干份
262- cleanup_files (final_prefix )
263- print ("cleanup end" )
273+ if backup_clean_enable :
274+ cleanup_files (final_prefix )
275+ print ("cleanup end" )
264276
265277 if s3_enable :
266278 # 3. 将备份文件上传到 S3,并清理 S3 上的多余备份文件
0 commit comments