Skip to content

Commit fdf2397

Browse files
committed
feat: clean enable
1 parent 585d9db commit fdf2397

File tree

4 files changed

+47
-34
lines changed

4 files changed

+47
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ S3 支持使用虚拟域名作为 endpoint,即可以将 region 或者 bucket
137137

138138
- MONGO_URI: 必填,uri,例如 `mongodb://root:[email protected]/some-db?authSource=admin`
139139
- FILE_PREFIX: 选填,备份文件前缀,例如 fcp
140+
- BACKUP_CLEAN_ENABLE: 选填,开启备份清理,默认为 true 表示启用
140141
- BACKUP_SAVE_NUMS: 选填,备份保存数量,例如 3,默认保存 3 份
141142
- MONGO_COLLECTION: 选填,集合名称,不支持多个,例如 gantry,若不为空,则需保证 MONGO_DB 也存在
142143
- MONGO_EXCLUDE_COLLECTIONS: 选填,忽略的集合名称,支持多个,例如 test1,test2,若不为空,则需保证 MONGO_DB 也存在,且若 MONGO_COLLECTION 不为空,则忽略该参数

docker/backup.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
other_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):
6768
backup_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+
)
7074
backup_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

247258
try:
@@ -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 上的多余备份文件

helm-chart/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 1.6.4
18+
version: 1.6.5
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "1.6.4"
24+
appVersion: "1.6.5"

helm-chart/values.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ backup:
6969
image:
7070
registry: ""
7171
repository: 36node/mongodb-backup
72-
tag: 1.6.4
72+
tag: 1.6.5
7373
digest: ""
7474
## Specify a imagePullPolicy
7575
## Defaults to 'Always' if image tag is 'latest', else set to 'IfNotPresent'
@@ -134,7 +134,7 @@ restore:
134134
image:
135135
registry: ""
136136
repository: 36node/mongodb-backup
137-
tag: 1.6.4
137+
tag: 1.6.5
138138
digest: ""
139139
## Specify a imagePullPolicy
140140
## Defaults to 'Always' if image tag is 'latest', else set to 'IfNotPresent'

0 commit comments

Comments
 (0)