add one time backup for sync #414

This commit is contained in:
tickstep 2024-04-21 11:32:49 +08:00
parent e9ae974017
commit 465d8fa4e9
3 changed files with 55 additions and 14 deletions

View File

@ -226,7 +226,13 @@ driveName - 网盘名称backup(备份盘)resource(资源盘)
} }
} }
RunSync(task, dp, up, downloadBlockSize, uploadBlockSize, syncOpt, c.Int("ldt")) cycleMode := syncdrive.CycleInfiniteLoop
if c.String("cycle") == "onetime" {
cycleMode = syncdrive.CycleOneTime
} else {
cycleMode = syncdrive.CycleInfiniteLoop
}
RunSync(task, cycleMode, dp, up, downloadBlockSize, uploadBlockSize, syncOpt, c.Int("ldt"))
return nil return nil
}, },
@ -254,6 +260,11 @@ driveName - 网盘名称backup(备份盘)resource(资源盘)
Usage: "备份策略, 支持两种: exclusive(排他备份文件,目标目录多余的文件会被删除),increment(增量备份文件,目标目录多余的文件不会被删除)", Usage: "备份策略, 支持两种: exclusive(排他备份文件,目标目录多余的文件会被删除),increment(增量备份文件,目标目录多余的文件不会被删除)",
Value: "increment", Value: "increment",
}, },
cli.StringFlag{
Name: "cycle",
Usage: "备份周期, 支持两种: infinity(永久循环备份),onetime(只运行一次备份)",
Value: "infinity",
},
cli.IntFlag{ cli.IntFlag{
Name: "dp", Name: "dp",
Usage: "download parallel, 下载并发数量即可以同时并发下载多少个文件。0代表跟从配置文件设置取值范围:1 ~ 10", Usage: "download parallel, 下载并发数量即可以同时并发下载多少个文件。0代表跟从配置文件设置取值范围:1 ~ 10",
@ -290,7 +301,7 @@ driveName - 网盘名称backup(备份盘)resource(资源盘)
} }
} }
func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadParallel int, downloadBlockSize, uploadBlockSize int64, func RunSync(defaultTask *syncdrive.SyncTask, cycleMode syncdrive.CycleMode, fileDownloadParallel, fileUploadParallel int, downloadBlockSize, uploadBlockSize int64,
flag syncdrive.SyncPriorityOption, localDelayTime int) { flag syncdrive.SyncPriorityOption, localDelayTime int) {
maxDownloadRate := config.Config.MaxDownloadRate maxDownloadRate := config.Config.MaxDownloadRate
maxUploadRate := config.Config.MaxUploadRate maxUploadRate := config.Config.MaxUploadRate
@ -353,7 +364,7 @@ func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadPa
fmt.Printf("备份配置文件:%s\n下载并发%d\n上传并发%d\n下载分片大小%s\n上传分片大小%s\n", fmt.Printf("备份配置文件:%s\n下载并发%d\n上传并发%d\n下载分片大小%s\n上传分片大小%s\n",
syncConfigFile, fileDownloadParallel, fileUploadParallel, converter.ConvertFileSize(downloadBlockSize, 2), syncConfigFile, fileDownloadParallel, fileUploadParallel, converter.ConvertFileSize(downloadBlockSize, 2),
converter.ConvertFileSize(uploadBlockSize, 2)) converter.ConvertFileSize(uploadBlockSize, 2))
if _, e := syncMgr.Start(tasks); e != nil { if _, e := syncMgr.Start(tasks, cycleMode); e != nil {
fmt.Println("启动任务失败:", e) fmt.Println("启动任务失败:", e)
return return
} }
@ -367,23 +378,33 @@ func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadPa
time.Sleep(60 * time.Second) time.Sleep(60 * time.Second)
} }
} else { } else {
if global.IsAppInCliMode { if cycleMode == syncdrive.CycleInfiniteLoop {
// in cmd mode if global.IsAppInCliMode {
c := "" // in cmd mode
fmt.Println("本命令不会退出如需要结束同步备份进程请输入y然后按Enter键进行停止。") c := ""
for strings.ToLower(c) != "y" { fmt.Println("本命令不会退出如需要结束同步备份进程请输入y然后按Enter键进行停止。")
fmt.Scan(&c) for strings.ToLower(c) != "y" {
fmt.Scan(&c)
}
} else {
fmt.Println("本命令不会退出,程序正在以非交互的方式运行。如需退出请借助运行环境提供的方式。")
logger.Verboseln("App not in CLI mode, not need to listen to input stream")
for {
time.Sleep(60 * time.Second)
}
} }
} else { } else {
fmt.Println("本命令不会退出,程序正在以非交互的方式运行。如需退出请借助运行环境提供的方式。")
logger.Verboseln("App not in CLI mode, not need to listen to input stream")
for { for {
time.Sleep(60 * time.Second) if syncMgr.IsAllTaskCompletely() {
fmt.Println("所有备份任务已完成")
break
}
time.Sleep(5 * time.Second)
} }
} }
} }
fmt.Println("正在停止同步备份任务,请稍等...") fmt.Println("正在退出同步备份任务,请稍等...")
// stop task // stop task
syncMgr.Stop() syncMgr.Stop()

View File

@ -236,6 +236,8 @@ func (t *SyncTask) Start() error {
go t.scanLocalFile(t.ctx) go t.scanLocalFile(t.ctx)
} else if t.Mode == Download { } else if t.Mode == Download {
go t.scanPanFile(t.ctx) go t.scanPanFile(t.ctx)
} else if t.Mode == SyncTwoWay {
go t.scanLocalFile(t.ctx)
} else { } else {
return fmt.Errorf("异常:暂不支持该模式。") return fmt.Errorf("异常:暂不支持该模式。")
} }
@ -299,6 +301,12 @@ func (t *SyncTask) SetScanLoopFlag(done bool) {
t.scanLoopIsDone = done t.scanLoopIsDone = done
} }
// IsTaskCompletely 任务是否已经完成
func (t *SyncTask) IsTaskCompletely() bool {
// 扫描完成+执行完成+一次运行模式
return t.IsScanLoopDone() && t.fileActionTaskManager.IsExecuteLoopIsDone() && t.CycleModeType == CycleOneTime
}
// panSyncDbFullPath 云盘文件数据库 // panSyncDbFullPath 云盘文件数据库
func (t *SyncTask) panSyncDbFullPath() string { func (t *SyncTask) panSyncDbFullPath() string {
dir := path.Join(t.syncDbFolderPath, t.Id) dir := path.Join(t.syncDbFolderPath, t.Id)

View File

@ -116,7 +116,7 @@ func (m *SyncTaskManager) ConfigFilePath() string {
} }
// Start 启动同步进程 // Start 启动同步进程
func (m *SyncTaskManager) Start(tasks []*SyncTask) (bool, error) { func (m *SyncTaskManager) Start(tasks []*SyncTask, cycleMode CycleMode) (bool, error) {
if tasks != nil && len(tasks) > 0 { if tasks != nil && len(tasks) > 0 {
m.syncDriveConfig = &SyncDriveConfig{ m.syncDriveConfig = &SyncDriveConfig{
ConfigVer: "1.0", ConfigVer: "1.0",
@ -139,6 +139,9 @@ func (m *SyncTaskManager) Start(tasks []*SyncTask) (bool, error) {
if len(task.Id) == 0 { if len(task.Id) == 0 {
task.Id = utils.UuidStr() task.Id = utils.UuidStr()
} }
// cycle mode
task.CycleModeType = cycleMode
// check driveId // check driveId
if strings.ToLower(task.DriveName) == "backup" { if strings.ToLower(task.DriveName) == "backup" {
task.DriveId = m.PanUser.DriveList.GetFileDriveId() task.DriveId = m.PanUser.DriveList.GetFileDriveId()
@ -221,3 +224,12 @@ func (m *SyncTaskManager) Stop() (bool, error) {
} }
return true, nil return true, nil
} }
func (m *SyncTaskManager) IsAllTaskCompletely() bool {
for _, task := range m.syncDriveConfig.SyncTaskList {
if !task.IsTaskCompletely() {
return false
}
}
return true
}