mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-23 17:31:50 +08:00
fix(backup): 加固一键备份与恢复安全性及大包流式传输 (#160)
- 导出前拒绝存在未暂停的运行中/排队中任务(409 backup_active_tasks + X-Artex-Error-Code),恢复事务内将未完成非暂停任务强制置为手动暂停,防止迁移后自动继续打目标,并同步更新确认文案与 README - 启动恢复时若文件替换或暂存清理失败,ApplyPendingRestore 返回错误且 main 非零退出,交由守护脚本重试,避免带着新库与半套文件启动业务引擎 - 数据库表导出改为 8 MiB 分片流式写入 tar+zstd 并刷新 HTTP 响应,消除大表落盘临时文件与反代长静默超时;README 补齐 /api/system/backup/ 的 Nginx client_max_body_size 0 与 proxy_request_buffering off 配置 - 暂存新恢复包时先将旧 pending 移走、rename 失败则回滚,避免丢弃已有待恢复包 - 恢复接口返回唯一 restore_id 并写入暂存标记,前端改为精确匹配 restore_id 判断重启恢复完成,消除时钟偏差与旧结果误判 - 补充 Windows->Linux 归档路径改写、活跃任务拦截与恢复自动暂停、多片流式 COPY、文件替换失败快速退出续跑及暂存原子替换测试
This commit is contained in:
1 parent
c6f7c29b71
commit
4d2c327df7
9 files changed
+578
-81
No files matched your search
@@ -49,9 +49,9 @@ export function BackupCard() {
|
||||
}
|
||||
};
|
||||
|
||||
// 重启期间接口连不上是预期的;以"出现了晚于本次上传的恢复结果"为准判断新进程已就绪。
|
||||
// 重启期间接口连不上是预期的;以"返回本次上传对应的 restore_id 结果"为准判断新进程已就绪。
|
||||
const waitForRestore = React.useCallback(
|
||||
async (since: number) => {
|
||||
async (restoreId: string) => {
|
||||
setRestarting(true);
|
||||
const deadline = Date.now() + RESTORE_TIMEOUT_MS;
|
||||
while (Date.now() < deadline) {
|
||||
@@ -60,8 +60,7 @@ export function BackupCard() {
|
||||
await tick.promise;
|
||||
try {
|
||||
const next = await api.backupStatus();
|
||||
const at = next.last_restore ? Date.parse(next.last_restore.at) : 0;
|
||||
if (at >= since) {
|
||||
if (next.last_restore?.id === restoreId) {
|
||||
window.location.reload();
|
||||
return;
|
||||
}
|
||||
@@ -80,11 +79,10 @@ export function BackupCard() {
|
||||
const selected = file;
|
||||
setFile(null);
|
||||
setUploading(true);
|
||||
const since = Date.now() - 60_000; // 容忍浏览器与服务器之间的时钟偏差
|
||||
try {
|
||||
await api.restoreBackup(selected);
|
||||
const res = await api.restoreBackup(selected);
|
||||
setUploading(false);
|
||||
await waitForRestore(since);
|
||||
await waitForRestore(res.restore_id);
|
||||
} catch (e) {
|
||||
setUploading(false);
|
||||
toast.error(t("settings.backup.restoreFailedPrefix") + localizedError(e, t));
|
||||
|
||||
@@ -4,6 +4,10 @@ export const errorMessages = {
|
||||
"The intelligence response is incomplete. Refresh to try again.",
|
||||
],
|
||||
"error.backup_busy": ["已有备份或恢复正在进行", "A backup or restore is already in progress"],
|
||||
"error.backup_active_tasks": [
|
||||
"存在未暂停的运行中或排队中任务,请先暂停或等待任务完成后再下载备份",
|
||||
"Active or queued tasks are running. Pause them or wait for them to finish before downloading a backup.",
|
||||
],
|
||||
"error.backup_update_running": [
|
||||
"更新正在进行中,无法恢复备份",
|
||||
"An update is in progress. The backup cannot be restored now.",
|
||||
|
||||
@@ -130,8 +130,8 @@ export const settingsMessages = {
|
||||
"settings.backup.restoreFailedPrefix": ["恢复失败:", "Restore failed: "],
|
||||
"settings.backup.confirmTitle": ["用备份替换当前全部数据?", "Replace all current data with this backup?"],
|
||||
"settings.backup.confirmBody": [
|
||||
"当前实例的任务、资产、漏洞、配置、文件和流量都会被「{{name}}」中的内容替换,且无法撤销。上传校验通过后程序会重启,正在运行的任务会被中断。建议先下载一份当前数据的备份。",
|
||||
"Tasks, assets, findings, settings, files and traffic on this instance will be replaced by the contents of “{{name}}”. This cannot be undone. After the upload is verified, ARTEX restarts and running tasks are interrupted. Download a backup of the current data first.",
|
||||
"当前实例的任务、资产、漏洞、配置、文件和流量都会被「{{name}}」中的内容替换,且无法撤销。上传校验通过后程序会重启并停止当前运行中的任务;备份中未结束的任务在恢复后会保持暂停,不会自动继续执行。建议先下载一份当前数据的备份。",
|
||||
"Tasks, assets, findings, settings, files and traffic on this instance will be replaced by the contents of “{{name}}”. This cannot be undone. After the upload is verified, ARTEX restarts and stops tasks currently running here; any unfinished tasks from the backup start paused rather than resuming automatically. Download a backup of the current data first.",
|
||||
],
|
||||
"settings.backup.cancel": ["取消", "Cancel"],
|
||||
"settings.backup.confirm": ["替换并重启", "Replace and restart"],
|
||||
@@ -140,8 +140,8 @@ export const settingsMessages = {
|
||||
"Backup verified. Restarting to restore data. Please wait; the page will reload automatically…",
|
||||
],
|
||||
"settings.backup.restoreTimeout": [
|
||||
"等待服务重启超时。请检查后端日志,或确认 artex 是通过 start.sh / start.bat 启动的。",
|
||||
"Timed out waiting for the service to restart. Check the backend logs and confirm ARTEX was started with start.sh / start.bat.",
|
||||
"等待服务重启超时(大备份的数据库恢复可能仍在进行)。请查看后端日志、稍后刷新页面,或确认 artex 是通过 start.sh / start.bat 启动的。",
|
||||
"Timed out waiting for the service to restart (a large backup may still be restoring on the server). Check the backend logs, refresh later, or confirm ARTEX was started with start.sh / start.bat.",
|
||||
],
|
||||
"settings.backup.lastRestoreOk": [
|
||||
"已于 {{time}} 从备份恢复(备份版本 {{version}})。",
|
||||
@@ -153,8 +153,8 @@ export const settingsMessages = {
|
||||
"An uploaded backup will be restored at the next restart.",
|
||||
],
|
||||
"settings.backup.note": [
|
||||
"恢复需要相同或更新版本的 ARTEX。登录密码、Basic Auth 与 LLM 配置随数据库一起迁移,恢复后请使用原实例的密码登录;新机器上已有的同名 skill 保留本机版本。",
|
||||
"Restoring requires the same or a newer ARTEX version. The sign-in password, Basic Auth and LLM settings move with the database, so sign in with the original instance's password afterwards. Skills that already exist on the new machine keep the local copy.",
|
||||
"下载备份前需先暂停或等待所有运行中、排队中的任务结束。恢复需要相同或更新版本的 ARTEX;登录密码、Basic Auth 与 LLM 配置随数据库一起迁移,恢复后请使用原实例的密码登录;新机器上已有的同名 skill 保留本机版本。",
|
||||
"Pause or finish active and queued tasks before downloading a backup. Restoring requires the same or a newer ARTEX version. The sign-in password, Basic Auth and LLM settings move with the database, so sign in with the original instance's password afterwards. Skills that already exist on the new machine keep the local copy.",
|
||||
],
|
||||
"settings.keywords.basicAuth": [
|
||||
"基础认证 登录 用户名 密码",
|
||||
|
||||
+6
-6
@@ -168,6 +168,7 @@ export interface BackupStatus {
|
||||
pending: boolean;
|
||||
// 本次启动时执行的恢复结果;未执行恢复时为 null。
|
||||
last_restore: {
|
||||
id?: string;
|
||||
applied: boolean;
|
||||
error?: string;
|
||||
at: string;
|
||||
@@ -439,11 +440,10 @@ export const api = {
|
||||
method: "HEAD",
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : {},
|
||||
});
|
||||
if (!response.ok)
|
||||
throw new ApiError(
|
||||
`HEAD /system/backup/export: ${response.status}`,
|
||||
response.status === 409 ? "backup_busy" : undefined,
|
||||
);
|
||||
if (!response.ok) {
|
||||
const code = response.headers.get("X-Artex-Error-Code") ?? (response.status === 409 ? "backup_busy" : undefined);
|
||||
throw new ApiError(`HEAD /system/backup/export: ${response.status}`, code);
|
||||
}
|
||||
const a = document.createElement("a");
|
||||
a.href = "/api/system/backup/export";
|
||||
document.body.appendChild(a);
|
||||
@@ -451,7 +451,7 @@ export const api = {
|
||||
a.remove();
|
||||
},
|
||||
restoreBackup: (file: File) =>
|
||||
http<{ ok: boolean; version: string; created_at: string }>("/system/backup/restore", {
|
||||
http<{ ok: boolean; restore_id: string; version: string; created_at: string }>("/system/backup/restore", {
|
||||
method: "POST",
|
||||
body: file,
|
||||
headers: { "Content-Type": "application/octet-stream" },
|
||||
|
||||
Reference in new issue
Block a user