fix(backup): 修复本地备份保留修剪静默失效,本地默认保留降到 2 份
bug:prune_local 里 ls 的 cd 只发生在命令替换的子 shell 中,
rm 却拿裸文件名在 PROJECT_ROOT 下执行——目标文件不存在,
而 rm -f 对不存在的文件静默成功。结果是"清理成功"日志照打、
本地备份数量却一直超出 KEEP(生产 configs 目录累积到 43 份,
dump 10 份,均超出 KEEP=7)。
修复:rm 改用 ${dir}/${file} 绝对路径,并补源码级守卫测试锁定。
顺带把 .env.server.example 的默认保留从 7 降到 2:
本地备份只是断网应急,OSS 已有完整备份;而部署脚本的磁盘预检
要求 可用 ≥ 备份目录×2,本地留太多会直接把部署卡死
(这次就是 3.8G 备份触发预检失败 → 被迫清构建缓存 → 全量构建变慢)。
This commit is contained in:
@@ -200,3 +200,21 @@ test('runLatest 跳过没有 manifest 的新对象并选择最近的有效备份
|
||||
'backups/postgres/valid.dump',
|
||||
)
|
||||
})
|
||||
|
||||
test('backup-db.sh 的本地保留修剪必须用绝对路径删除(曾静默失效导致备份无限累积)', async () => {
|
||||
const { readFile } = await import('node:fs/promises')
|
||||
const script = await readFile(new URL('../../../deploy/backup-db.sh', import.meta.url), 'utf8')
|
||||
|
||||
const pruneBody = script.slice(script.indexOf('prune_local() {'), script.indexOf('main() {'))
|
||||
|
||||
// 曾经的 bug:cd 只发生在 ls 的命令替换子 shell 里,rm 却拿裸文件名在
|
||||
// PROJECT_ROOT 下删(目标不存在),rm -f 静默吞错 → 本地备份远超 KEEP 份数。
|
||||
assert.ok(
|
||||
pruneBody.includes('rm -f -- "${dir}/${file}"'),
|
||||
'修剪必须删除 ${dir}/${file};只删文件名会在项目根目录下落空',
|
||||
)
|
||||
assert.ok(
|
||||
!/"\$\{file\}"\s*$/m.test(pruneBody.replace('rm -f -- "${dir}/${file}"', '')),
|
||||
'不得再出现只按文件名删除的写法',
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user