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
},
@ -254,6 +260,11 @@ driveName - 网盘名称backup(备份盘)resource(资源盘)
Usage: "备份策略, 支持两种: exclusive(排他备份文件,目标目录多余的文件会被删除),increment(增量备份文件,目标目录多余的文件不会被删除)",
Value: "increment",
},
cli.StringFlag{
Name: "cycle",
Usage: "备份周期, 支持两种: infinity(永久循环备份),onetime(只运行一次备份)",
Value: "infinity",
},
cli.IntFlag{
Name: "dp",
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) {
maxDownloadRate := config.Config.MaxDownloadRate
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",
syncConfigFile, fileDownloadParallel, fileUploadParallel, converter.ConvertFileSize(downloadBlockSize, 2),
converter.ConvertFileSize(uploadBlockSize, 2))
if _, e := syncMgr.Start(tasks); e != nil {
if _, e := syncMgr.Start(tasks, cycleMode); e != nil {
fmt.Println("启动任务失败:", e)
return
}
@ -367,6 +378,7 @@ func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadPa
time.Sleep(60 * time.Second)
}
} else {
if cycleMode == syncdrive.CycleInfiniteLoop {
if global.IsAppInCliMode {
// in cmd mode
c := ""
@ -381,9 +393,18 @@ func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadPa
time.Sleep(60 * time.Second)
}
}
} else {
for {
if syncMgr.IsAllTaskCompletely() {
fmt.Println("所有备份任务已完成")
break
}
time.Sleep(5 * time.Second)
}
}
}
fmt.Println("正在停止同步备份任务,请稍等...")
fmt.Println("正在退出同步备份任务,请稍等...")
// stop task
syncMgr.Stop()

View File

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

View File

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