@xiaoyuyu6420/dsh-backup 0.5.2 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -7
- package/README.zh.md +37 -7
- package/lib/client.js +7 -7
- package/lib/index.js +193 -44
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -9,8 +9,11 @@
|
|
|
9
9
|
* - `/backup list` 列出已有备份(含大小)+ 自动备份状态
|
|
10
10
|
* - `/backup verify [前缀|all]` 校验备份完整性(缺省校验最新一份)
|
|
11
11
|
* - `/backup restore <前缀|latest> [--dry-run]` 恢复(先校验 + 自动快照当前数据)
|
|
12
|
-
* - `/backup auto <N小时>|off|status` 定时自动备份(1~720
|
|
13
|
-
*
|
|
12
|
+
* - `/backup auto <N小时>|off|status` 定时自动备份(1~720;保留份数由 config.keep
|
|
13
|
+
* 支配,未配置时 <24h 3 份、否则 7 份;重启按
|
|
14
|
+
* 上次执行时间推算下次,不重置节奏)
|
|
15
|
+
* - `/backup github status|sync|repo <地址|off>` GitHub 同步状态 / 立即同步 / 设置仓库
|
|
16
|
+
* - `/backup delete|rm <前缀|latest>` 删除备份(归档 + 校验边车)
|
|
14
17
|
* - `/backup --keep N` 覆盖本次保留份数
|
|
15
18
|
* - `backup_dsh` 模型工具:mode=backup|list|verify|restore|auto
|
|
16
19
|
* - cordis.yml `config`:destination / keep / exclude / githubRepo(见 README)
|
|
@@ -30,7 +33,7 @@
|
|
|
30
33
|
*/
|
|
31
34
|
import { createHash } from 'node:crypto';
|
|
32
35
|
import { createReadStream } from 'node:fs';
|
|
33
|
-
import { mkdir, copyFile, readdir, readFile, stat as fsStat, writeFile } from 'node:fs/promises';
|
|
36
|
+
import { mkdir, open, copyFile, readdir, readFile, rename, stat as fsStat, unlink, writeFile } from 'node:fs/promises';
|
|
34
37
|
import { dirname, join } from 'node:path';
|
|
35
38
|
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
36
39
|
import { TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol';
|
|
@@ -38,7 +41,7 @@ import { TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol';
|
|
|
38
41
|
export const name = 'dsh-backup';
|
|
39
42
|
export const inject = ['subprocess', 'commands', 'timer', 'tools'];
|
|
40
43
|
|
|
41
|
-
/** Windows
|
|
44
|
+
/** Windows 无 POSIX chmod 语义(用户目录 ACL 默认私有)。 */
|
|
42
45
|
const IS_WIN = process.platform === 'win32';
|
|
43
46
|
/** sha256 回退路径(node:fs 读取 + node:crypto)的内存上限。 */
|
|
44
47
|
const HASH_MAX_BYTES = 256 * 1024 * 1024;
|
|
@@ -144,6 +147,25 @@ class BackupPanelService extends TypertRemoteService {
|
|
|
144
147
|
|
|
145
148
|
export function apply(ctx, pluginConfig) {
|
|
146
149
|
// ---------- 工具函数 ----------
|
|
150
|
+
/**
|
|
151
|
+
* 读取 subprocess 收集的输出流。dsh-subprocess 的收集契约:输出超过
|
|
152
|
+
* maxBytes(8192)时内存窗口只保留尾部(readFrom 返回 lossy: true),
|
|
153
|
+
* 完整输出 spill 到 spillPath——restore 的 `tar -tvzf` 校验清单必须看
|
|
154
|
+
* 全量,否则大归档的校验可被清单前段的恶意条目绕过(8KB 尾窗看不到)。
|
|
155
|
+
*/
|
|
156
|
+
async function collectOutput(col) {
|
|
157
|
+
const read = col?.readFrom(0);
|
|
158
|
+
if (!read) return '';
|
|
159
|
+
if (read.lossy && read.spillPath) {
|
|
160
|
+
try {
|
|
161
|
+
return await readFile(read.spillPath, 'utf8');
|
|
162
|
+
} catch {
|
|
163
|
+
// spill 文件不可读时降级为内存尾窗文本(截断,但比失败好)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return read.text ?? '';
|
|
167
|
+
}
|
|
168
|
+
|
|
147
169
|
async function spawnRun(argv, cwd, signal) {
|
|
148
170
|
const proc = ctx.subprocess.spawn({
|
|
149
171
|
argv,
|
|
@@ -157,8 +179,18 @@ export function apply(ctx, pluginConfig) {
|
|
|
157
179
|
},
|
|
158
180
|
});
|
|
159
181
|
const outcome = await proc.done;
|
|
160
|
-
const out = proc.collected
|
|
161
|
-
const err = proc.collected
|
|
182
|
+
const out = await collectOutput(proc.collected?.stdout);
|
|
183
|
+
const err = await collectOutput(proc.collected?.stderr);
|
|
184
|
+
// 取消优先于一切结果分类:用户取消不是"命令失败"。进程已正常退出后、
|
|
185
|
+
// 结果返回前的毫秒窗口内 abort 同样报"操作已取消"——刻意取舍:真取消
|
|
186
|
+
// 绝不得误报成功,窗口期误报取消(R7 的反向错误)比误报成功危害小。
|
|
187
|
+
if (signal?.aborted) {
|
|
188
|
+
throw new Error('操作已取消');
|
|
189
|
+
}
|
|
190
|
+
// loose 检查:smoke 桩与部分平台下 signal 字段可能为 undefined。
|
|
191
|
+
if (outcome.signal != null) {
|
|
192
|
+
throw new Error(`命令被终止(signal=${outcome.signal})`);
|
|
193
|
+
}
|
|
162
194
|
if (outcome.exitCode !== 0) {
|
|
163
195
|
throw new Error(`命令失败 exit=${outcome.exitCode}: ${err || out}`);
|
|
164
196
|
}
|
|
@@ -199,29 +231,24 @@ export function apply(ctx, pluginConfig) {
|
|
|
199
231
|
}
|
|
200
232
|
|
|
201
233
|
/**
|
|
202
|
-
* 删除 dir
|
|
203
|
-
*
|
|
234
|
+
* 删除 dir 下的文件。纯 Node fs.unlink(零 shell):文件名来自备份目录
|
|
235
|
+
* 内容,拼接进 cmd/rm 命令行会构成命令注入面(Windows 下 cmd 的
|
|
236
|
+
* `del /f /q ${names}` 可被 `dsh-x & calc &x.tar.gz` 之类文件名利用)。
|
|
204
237
|
*/
|
|
205
238
|
async function removeFiles(names, dir, signal) {
|
|
206
239
|
if (!names.length) return;
|
|
207
|
-
if (
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
240
|
+
if (signal?.aborted) throw new Error('操作已取消');
|
|
241
|
+
await Promise.all(names.map((n) => unlink(`${dir}/${n}`).catch((err) => {
|
|
242
|
+
// 目标已不存在等价 rm -f 的静默成功;其余错误照常抛出
|
|
243
|
+
if (err && err.code === 'ENOENT') return;
|
|
244
|
+
throw err;
|
|
245
|
+
})));
|
|
214
246
|
}
|
|
215
247
|
|
|
216
248
|
/** 把 dir 下的文件/目录改名为同目录下的另一个名字(恢复时挪开现有数据)。 */
|
|
217
249
|
async function renameBeside(dir, srcName, dstName, signal) {
|
|
218
|
-
if (
|
|
219
|
-
|
|
220
|
-
await spawnRun([cmd, '/d', '/c', `move /y ${srcName} ${dstName}`], dir, signal);
|
|
221
|
-
} else {
|
|
222
|
-
const mv = await ctx.subprocess.resolveExecutable('mv');
|
|
223
|
-
await spawnRun([mv, `${dir}/${srcName}`, `${dir}/${dstName}`], dir, signal);
|
|
224
|
-
}
|
|
250
|
+
if (signal?.aborted) throw new Error('操作已取消');
|
|
251
|
+
await rename(`${dir}/${srcName}`, `${dir}/${dstName}`);
|
|
225
252
|
}
|
|
226
253
|
|
|
227
254
|
async function listBackups() {
|
|
@@ -230,7 +257,7 @@ export function apply(ctx, pluginConfig) {
|
|
|
230
257
|
try {
|
|
231
258
|
dirents = await readdir(root, { withFileTypes: true });
|
|
232
259
|
} catch {
|
|
233
|
-
//
|
|
260
|
+
// 备份目录缺失或不可读时视为空列表(首次使用前 / EACCES 容错)
|
|
234
261
|
return [];
|
|
235
262
|
}
|
|
236
263
|
const backups = [];
|
|
@@ -240,6 +267,7 @@ export function apply(ctx, pluginConfig) {
|
|
|
240
267
|
try {
|
|
241
268
|
size = (await fsStat(`${root}/${d.name}`)).size;
|
|
242
269
|
} catch {
|
|
270
|
+
// 归档在列表与 stat 之间被删除(轮换/删除竞态)——大小显示为未知而非报错
|
|
243
271
|
size = undefined;
|
|
244
272
|
}
|
|
245
273
|
backups.push({ name: d.name, size });
|
|
@@ -373,6 +401,7 @@ export function apply(ctx, pluginConfig) {
|
|
|
373
401
|
try {
|
|
374
402
|
await spawnRun([git, 'init', '-b', 'main'], syncDir, signal);
|
|
375
403
|
} catch {
|
|
404
|
+
// git 版本不支持 -b 时回退 init + branch -M
|
|
376
405
|
await spawnRun([git, 'init'], syncDir, signal);
|
|
377
406
|
await spawnRun([git, 'branch', '-M', 'main'], syncDir, signal);
|
|
378
407
|
}
|
|
@@ -384,20 +413,33 @@ export function apply(ctx, pluginConfig) {
|
|
|
384
413
|
if (cfg.token) {
|
|
385
414
|
// 正斜杠路径:msys git 会把反斜杠绝对路径当相对路径解析(怪名化)
|
|
386
415
|
const creds = `${syncDir}/.git-credentials`;
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
416
|
+
// 独占创建(0600):凭据文件不留 umask 默认权限窗口;已存在时普通
|
|
417
|
+
// 打开后显式 chmod 收紧——open 的 mode 参数只作用于新建文件,对已
|
|
418
|
+
// 存在文件(如 0644 残留)不生效。
|
|
419
|
+
let fh;
|
|
420
|
+
try {
|
|
421
|
+
fh = await open(creds, 'wx', 0o600);
|
|
422
|
+
} catch (e) {
|
|
423
|
+
if (!(e && e.code === 'EEXIST')) throw e;
|
|
424
|
+
fh = await open(creds, 'w', 0o600);
|
|
425
|
+
if (!IS_WIN) await fh.chmod(0o600);
|
|
426
|
+
}
|
|
427
|
+
try {
|
|
428
|
+
await fh.writeFile(`https://x-access-token:${cfg.token}@github.com\n`, 'utf8');
|
|
429
|
+
} finally {
|
|
430
|
+
await fh.close();
|
|
391
431
|
}
|
|
392
432
|
await spawnRun([git, 'config', 'credential.helper', `store --file=${creds}`], syncDir, signal);
|
|
393
433
|
}
|
|
394
434
|
// token 文件绝不能进仓库:git add -A 会把它当普通文件提交
|
|
395
435
|
await writeOwned(`${syncDir}/.gitignore`, '.git-credentials\n');
|
|
396
436
|
|
|
397
|
-
// 镜像工作树:只保留 .gitignore
|
|
398
|
-
//
|
|
399
|
-
//
|
|
437
|
+
// 镜像工作树:只保留 .gitignore、凭据文件(.git-credentials,token
|
|
438
|
+
// 配置时在 keep 集内保留)与当前备份集(归档+边车),其余文件
|
|
439
|
+
// (旧副本、误入杂物)清理——git add -A 因此只会收录归档;轮换
|
|
440
|
+
// 删除与误入文件一并同步移除。
|
|
400
441
|
const keep = new Set(['.gitignore']);
|
|
442
|
+
if (cfg.token) keep.add('.git-credentials');
|
|
401
443
|
for (const b of await listBackups()) {
|
|
402
444
|
keep.add(b.name);
|
|
403
445
|
keep.add(`${b.name}.sha256`);
|
|
@@ -406,6 +448,7 @@ export function apply(ctx, pluginConfig) {
|
|
|
406
448
|
try {
|
|
407
449
|
entries = await readdir(syncDir, { withFileTypes: true });
|
|
408
450
|
} catch {
|
|
451
|
+
// 同步目录刚创建或不可读时视为空
|
|
409
452
|
entries = [];
|
|
410
453
|
}
|
|
411
454
|
const stale = entries
|
|
@@ -464,6 +507,7 @@ export function apply(ctx, pluginConfig) {
|
|
|
464
507
|
});
|
|
465
508
|
createReadStream(abs).on('error', () => { res.destroy(); }).pipe(res);
|
|
466
509
|
} catch {
|
|
510
|
+
// 任何读取/解析失败按 404 处理(下载是尽力而为的附件通道)
|
|
467
511
|
if (!res.headersSent) res.writeHead(404);
|
|
468
512
|
res.end();
|
|
469
513
|
}
|
|
@@ -506,6 +550,49 @@ export function apply(ctx, pluginConfig) {
|
|
|
506
550
|
return { ok: true, name: picked.name, summary: `已删除备份: ${picked.name}` };
|
|
507
551
|
}
|
|
508
552
|
|
|
553
|
+
/**
|
|
554
|
+
* 解析 `tar -tvzf` 单行输出,返回 { type, name };布局不匹配返回 null。
|
|
555
|
+
* 兼容三种 verbose 布局(locale 无关锚定,不依赖英文月份缩写):
|
|
556
|
+
* 1. GNU tar:mode owner size YYYY-MM-DD 时间|年份 name——以 YYYY-MM-DD
|
|
557
|
+
* 字段为锚,name 起点 = 该字段 + 2(跳过 date 与 time/年份)。
|
|
558
|
+
* 2. bsdtar:mode uid gid size 月 日 时间 name(中文 locale 月份形如
|
|
559
|
+
* `8月`,英文 `Aug`)——以最后一个 HH:MM 字段为锚,name 起点 = 其后 1。
|
|
560
|
+
* 3. bsdtar 跨年条目无时间字段(月 日 年份)——以最后一个 4 位数字为锚
|
|
561
|
+
* (年份;size 恰为 4 位数字时年份更靠右仍正确),name 起点 = 其后 1。
|
|
562
|
+
* name 保留含空格路径并去掉目录尾部的斜杠。
|
|
563
|
+
* 已知边界:POSIX 文件名恰为 HH:MM 形态(如 `12:30`)会被误取为时间锚
|
|
564
|
+
* (Windows 文件名禁 `:`,故 Windows 不受影响)——极端误取,保守接受。
|
|
565
|
+
*/
|
|
566
|
+
function parseTarEntry(line) {
|
|
567
|
+
const f = line.trim().split(/\s+/);
|
|
568
|
+
if (f.length < 6) return null;
|
|
569
|
+
let nameIdx = -1;
|
|
570
|
+
for (let i = 1; i < f.length; i++) {
|
|
571
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(f[i])) {
|
|
572
|
+
nameIdx = i + 2;
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (nameIdx < 0) {
|
|
577
|
+
for (let i = f.length - 1; i >= 1; i--) {
|
|
578
|
+
if (/^\d{1,2}:\d{2}$/.test(f[i])) {
|
|
579
|
+
nameIdx = i + 1;
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
if (nameIdx < 0) {
|
|
585
|
+
for (let i = f.length - 1; i >= 1; i--) {
|
|
586
|
+
if (/^\d{4}$/.test(f[i])) {
|
|
587
|
+
nameIdx = i + 1;
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (nameIdx < 0 || nameIdx >= f.length) return null;
|
|
593
|
+
return { type: f[0][0], name: f.slice(nameIdx).join(' ').replace(/\/$/, '') };
|
|
594
|
+
}
|
|
595
|
+
|
|
509
596
|
async function restoreArchive(selector, dryRun, signal) {
|
|
510
597
|
const { home, dshHome, root } = paths();
|
|
511
598
|
const picked = await pickArchive(selector);
|
|
@@ -519,12 +606,33 @@ export function apply(ctx, pluginConfig) {
|
|
|
519
606
|
const parent = dshHome.slice(0, -(base.length + 1)) || '/';
|
|
520
607
|
const tar = await ctx.subprocess.resolveExecutable('tar');
|
|
521
608
|
// 同 doBackup:cwd 为备份目录,-f 用纯文件名,规避 Windows GNU tar 的盘符冒号问题。
|
|
522
|
-
const listed = await spawnRun([tar, '-
|
|
523
|
-
|
|
524
|
-
//
|
|
525
|
-
|
|
609
|
+
const listed = await spawnRun([tar, '-tvzf', picked.name], root, signal);
|
|
610
|
+
// tar 路径穿越防护:逐行解析 -tvzf 输出(双布局,见 parseTarEntry),
|
|
611
|
+
// 条目必须是普通文件/目录、相对路径且位于备份根目录之下;任一违规
|
|
612
|
+
// 即整体拒绝,恢复绝不触碰备份根之外的文件。
|
|
613
|
+
const entries = [];
|
|
614
|
+
const bad = [];
|
|
615
|
+
for (const line of listed.out.split('\n')) {
|
|
616
|
+
const s = line.trim();
|
|
617
|
+
if (!s) continue;
|
|
618
|
+
const parsed = parseTarEntry(s);
|
|
619
|
+
// 反斜杠归一化后再做全部路径检查:`.dsh/..\..\escape.txt` 与
|
|
620
|
+
// `/` 分隔的 `..` 段同样拒绝(Windows 风格路径变体)
|
|
621
|
+
const name = parsed ? parsed.name.replace(/\\/g, '/').replace(/\/$/, '') : null;
|
|
622
|
+
if (
|
|
623
|
+
!parsed || !name
|
|
624
|
+
|| (parsed.type !== '-' && parsed.type !== 'd')
|
|
625
|
+
|| name.startsWith('/') || /^[A-Za-z]:/.test(name)
|
|
626
|
+
|| name.split('/').includes('..')
|
|
627
|
+
|| (name !== base && !name.startsWith(`${base}/`))
|
|
628
|
+
) {
|
|
629
|
+
bad.push(parsed ? name : s);
|
|
630
|
+
} else {
|
|
631
|
+
entries.push(name);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
526
634
|
if (bad.length) {
|
|
527
|
-
throw new Error(
|
|
635
|
+
throw new Error(`归档包含不安全条目(拒绝恢复):${bad.slice(0, 3).join(', ')},恢复已中止`);
|
|
528
636
|
}
|
|
529
637
|
|
|
530
638
|
if (dryRun) return { archive, files: entries.length, sample: entries.slice(0, 12), aside: null, snapshotPath: null, dryRun: true };
|
|
@@ -536,7 +644,10 @@ export function apply(ctx, pluginConfig) {
|
|
|
536
644
|
try {
|
|
537
645
|
await fsStat(dshHome);
|
|
538
646
|
current = true;
|
|
539
|
-
} catch {
|
|
647
|
+
} catch (err) {
|
|
648
|
+
// dshHome 不存在(ENOENT)视为首次恢复、无现有数据可移开;其他 stat
|
|
649
|
+
// 失败(如 EACCES)中止恢复,不跳过 aside 直接覆盖现有数据
|
|
650
|
+
if (!err || err.code !== 'ENOENT') throw err;
|
|
540
651
|
current = false;
|
|
541
652
|
}
|
|
542
653
|
if (current) {
|
|
@@ -552,11 +663,18 @@ export function apply(ctx, pluginConfig) {
|
|
|
552
663
|
let autoDispose = null;
|
|
553
664
|
let autoHours = 0;
|
|
554
665
|
let lastAuto = null;
|
|
666
|
+
let lastAutoAt = null;
|
|
555
667
|
let githubState = { repo: null, lastPush: null, lastError: null };
|
|
556
668
|
|
|
669
|
+
/** 自动备份的保留份数:config.keep 支配(未配置时 <24h 3 份、否则 7 份)。 */
|
|
670
|
+
function autoKeep() {
|
|
671
|
+
const k = Number(pluginConfig?.keep);
|
|
672
|
+
return Number.isFinite(k) && k > 0 ? Math.floor(k) : (autoHours >= 24 ? 7 : 3);
|
|
673
|
+
}
|
|
674
|
+
|
|
557
675
|
async function saveAutoState() {
|
|
558
676
|
const { root } = paths();
|
|
559
|
-
await writeOwned(`${root}/auto.json`, `${JSON.stringify({ hours: autoHours, github: githubState })}\n`);
|
|
677
|
+
await writeOwned(`${root}/auto.json`, `${JSON.stringify({ hours: autoHours, lastAutoAt, github: githubState })}\n`);
|
|
560
678
|
}
|
|
561
679
|
|
|
562
680
|
async function loadAutoState() {
|
|
@@ -571,24 +689,54 @@ export function apply(ctx, pluginConfig) {
|
|
|
571
689
|
lastError: typeof parsed.github.lastError === 'string' ? parsed.github.lastError : null,
|
|
572
690
|
};
|
|
573
691
|
}
|
|
692
|
+
lastAutoAt = typeof parsed?.lastAutoAt === 'string' && !Number.isNaN(Date.parse(parsed.lastAutoAt))
|
|
693
|
+
? parsed.lastAutoAt
|
|
694
|
+
: null;
|
|
574
695
|
return Number.isFinite(h) && h >= 1 && h <= 720 ? Math.floor(h) : 0;
|
|
575
|
-
} catch {
|
|
696
|
+
} catch (err) {
|
|
697
|
+
console.warn(`[dsh-backup] auto.json 无法读取,自动备份计划已重置: ${String(err && err.message ? err.message : err)}`);
|
|
576
698
|
return 0;
|
|
577
699
|
}
|
|
578
700
|
}
|
|
579
701
|
|
|
702
|
+
/** 下次自动备份触发时间(毫秒):上次执行 + 周期;无锚点从当前时间起算。 */
|
|
703
|
+
function nextAutoAt() {
|
|
704
|
+
const cycle = autoHours * 3600 * 1000;
|
|
705
|
+
// 非法/缺失 lastAutoAt 按 0 处理:从当前时间起算,避免 NaN 传染延迟计算
|
|
706
|
+
const anchor = (Number(new Date(lastAutoAt)) || 0) + cycle;
|
|
707
|
+
return Math.max(anchor, Date.now());
|
|
708
|
+
}
|
|
709
|
+
|
|
580
710
|
function autoSummary() {
|
|
581
711
|
if (!autoDispose) return '自动备份未开启(/backup auto <N小时> 开启)';
|
|
582
|
-
|
|
712
|
+
// 绝对节奏:下次 = 上次执行 + 周期;错过(重启间隔超周期)则按现在显示,调度会立即补跑。
|
|
713
|
+
const next = new Date(nextAutoAt()).toLocaleString();
|
|
583
714
|
return `自动备份已开启:每 ${autoHours} 小时一次(已持久化,重启续跑),下次约 ${next}${lastAuto ? `;上次自动备份: ${lastAuto}` : ''}`;
|
|
584
715
|
}
|
|
585
716
|
|
|
717
|
+
/** 链式 timeout 调度:每次触发后按上次执行时间推算下一次,重启后节奏不重置。 */
|
|
718
|
+
function scheduleAuto() {
|
|
719
|
+
// 关闭竞态保护:auto off 时 in-flight 备份完成后的续链会以 autoHours=0
|
|
720
|
+
// 算出 delay=0,形成无限备份循环——关闭后绝不续链。
|
|
721
|
+
if (autoHours <= 0) return;
|
|
722
|
+
const delay = nextAutoAt() - Date.now();
|
|
723
|
+
autoDispose = ctx.timeout(async () => {
|
|
724
|
+
await runAutoBackup();
|
|
725
|
+
scheduleAuto();
|
|
726
|
+
}, delay);
|
|
727
|
+
}
|
|
728
|
+
|
|
586
729
|
async function runAutoBackup() {
|
|
587
730
|
try {
|
|
588
|
-
const r = await doBackup(
|
|
731
|
+
const r = await doBackup(autoKeep());
|
|
589
732
|
lastAuto = r.path.split('/').pop();
|
|
733
|
+
lastAutoAt = new Date().toISOString();
|
|
734
|
+
await saveAutoState();
|
|
590
735
|
console.log(`[dsh-backup] 自动备份完成: ${r.path} (sha ${r.sha.slice(0, 12)}…)`);
|
|
591
736
|
} catch (err) {
|
|
737
|
+
// 失败也推进内存锚点:否则下次 delay=0 立即重试形成热循环。不落盘
|
|
738
|
+
// (失败路径 auto.json 大概率也写失败),下次成功备份会持久化。
|
|
739
|
+
lastAutoAt = new Date().toISOString();
|
|
592
740
|
console.error(`[dsh-backup] 自动备份失败: ${String(err && err.message ? err.message : err)}`);
|
|
593
741
|
}
|
|
594
742
|
}
|
|
@@ -596,7 +744,8 @@ export function apply(ctx, pluginConfig) {
|
|
|
596
744
|
async function setAuto(h) {
|
|
597
745
|
if (autoDispose) { autoDispose(); autoDispose = null; }
|
|
598
746
|
autoHours = h;
|
|
599
|
-
|
|
747
|
+
lastAutoAt = null;
|
|
748
|
+
if (h > 0) scheduleAuto();
|
|
600
749
|
await saveAutoState();
|
|
601
750
|
}
|
|
602
751
|
|
|
@@ -703,7 +852,7 @@ export function apply(ctx, pluginConfig) {
|
|
|
703
852
|
return { kind: 'error', text: '小时数需为 1~720 之间的数字(如 /backup auto 12)' };
|
|
704
853
|
}
|
|
705
854
|
await setAuto(h);
|
|
706
|
-
return { kind: 'success', text: `✅ 自动备份已开启:每 ${h} 小时执行一次(保留 ${
|
|
855
|
+
return { kind: 'success', text: `✅ 自动备份已开启:每 ${h} 小时执行一次(保留 ${autoKeep()} 份,已持久化)。\n${autoSummary()}` };
|
|
707
856
|
}
|
|
708
857
|
|
|
709
858
|
let keep;
|
|
@@ -930,12 +1079,12 @@ export function apply(ctx, pluginConfig) {
|
|
|
930
1079
|
}), 'dsh-backup: download route');
|
|
931
1080
|
});
|
|
932
1081
|
|
|
933
|
-
//
|
|
1082
|
+
// 启动时恢复持久化的定时备份计划(不阻塞插件装配);错过则 delay=0 立即补跑。
|
|
934
1083
|
void (async () => {
|
|
935
1084
|
const h = await loadAutoState();
|
|
936
1085
|
if (h > 0 && !autoDispose) {
|
|
937
1086
|
autoHours = h;
|
|
938
|
-
|
|
1087
|
+
scheduleAuto();
|
|
939
1088
|
}
|
|
940
1089
|
})();
|
|
941
1090
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xiaoyuyu6420/dsh-backup",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Backup, restore, download and GitHub-sync DeepSeek Harness user data (~/.dsh): /backup, scheduled auto-backup that survives restarts, sha256 checksums, integrity verify, rotation and a visual Settings panel. Cross-platform (macOS/Linux/Windows). 一键备份与恢复 DSH 数据:定时自动备份、完整性校验、下载与 GitHub 同步,附 Settings 可视面板。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|