feat: add restore backup zip

This commit is contained in:
engigu
2026-02-27 23:51:32 +08:00
parent 416c47d8e7
commit 22e3e89ba0
4 changed files with 52 additions and 0 deletions
+1
View File
@@ -564,6 +564,7 @@ nginx -t && nginx -s reload
baihu server # 以前台方式启动面板的后台进程服务(面板启动指令) baihu server # 以前台方式启动面板的后台进程服务(面板启动指令)
baihu reposync # 供定时任务调用,将远程 Git 仓库的高级特性同步到本地目录中 baihu reposync # 供定时任务调用,将远程 Git 仓库的高级特性同步到本地目录中
baihu resetpwd # 交互式重置系统 admin 账号密码(密码丢失时可通过进入终端重置) baihu resetpwd # 交互式重置系统 admin 账号密码(密码丢失时可通过进入终端重置)
baihu restore <file> # 使用本地的 .zip 备份压缩包文件,一条命令直接全量恢复系统数据
``` ```
终端执行 `baihu` 会直接打印内置支持的高级命令帮助列表。 终端执行 `baihu` 会直接打印内置支持的高级命令帮助列表。
+2
View File
@@ -3,6 +3,7 @@ package cmd
import ( import (
"github.com/engigu/baihu-panel/cmd/reposync" "github.com/engigu/baihu-panel/cmd/reposync"
"github.com/engigu/baihu-panel/cmd/resetpwd" "github.com/engigu/baihu-panel/cmd/resetpwd"
"github.com/engigu/baihu-panel/cmd/restore"
) )
// CommandHandler 定义命令执行函数 // CommandHandler 定义命令执行函数
@@ -12,4 +13,5 @@ type CommandHandler func(args []string)
var Handlers = map[string]CommandHandler{ var Handlers = map[string]CommandHandler{
"reposync": reposync.Run, "reposync": reposync.Run,
"resetpwd": resetpwd.Run, "resetpwd": resetpwd.Run,
"restore": restore.Run,
} }
+45
View File
@@ -0,0 +1,45 @@
package restore
import (
"fmt"
"os"
"path/filepath"
"github.com/engigu/baihu-panel/internal/bootstrap"
"github.com/engigu/baihu-panel/internal/services"
)
func Run(args []string) {
if len(args) < 1 {
fmt.Println("用法: baihu restore <backup_file.zip>")
os.Exit(1)
}
backupFile := args[0]
absPath, err := filepath.Abs(backupFile)
if err != nil {
fmt.Printf("文件路径解析失败: %v\n", err)
os.Exit(1)
}
if _, err := os.Stat(absPath); os.IsNotExist(err) {
fmt.Printf("错误: 备份文件 '%s' 不存在\n", absPath)
os.Exit(1)
}
// 必须初始化环境与数据库才能恢复数据
bootstrap.InitBasic()
backupService := services.NewBackupService()
fmt.Printf("正在从 '%s' 恢复系统数据,请勿强制中断...\n", absPath)
err = backupService.Restore(absPath)
if err != nil {
fmt.Printf("恢复备份失败: %v\n", err)
os.Exit(1)
}
fmt.Println("--------------------------------------------------")
fmt.Println("系统备份恢复成功!")
fmt.Println("注意:部分设定可能需要重启后台服务后才能完全生效。")
fmt.Println("--------------------------------------------------")
}
+4
View File
@@ -20,4 +20,8 @@ var Commands = []CommandInfo{
Name: "resetpwd", Name: "resetpwd",
Description: "重置 admin 用户密码(需要二次确认)", Description: "重置 admin 用户密码(需要二次确认)",
}, },
{
Name: "restore",
Description: "从本地 zip 文件中全量恢复系统级备份数据",
},
} }