support resource drive for sync command

This commit is contained in:
tickstep 2024-03-16 22:06:20 +08:00
parent df53c7f44b
commit 281ba818cd
3 changed files with 45 additions and 10 deletions

View File

@ -71,7 +71,8 @@ func CmdSync() cli.Command {
"name": "设计文档备份", "name": "设计文档备份",
"localFolderPath": "D:/tickstep/Documents/设计文档", "localFolderPath": "D:/tickstep/Documents/设计文档",
"panFolderPath": "/sync_drive/我的文档", "panFolderPath": "/sync_drive/我的文档",
"mode": "upload" "mode": "upload",
"driveName": "backup"
} }
] ]
} }
@ -80,13 +81,14 @@ name - 任务名称
localFolderPath - 本地目录 localFolderPath - 本地目录
panFolderPath - 网盘目录 panFolderPath - 网盘目录
mode - 模式支持两种: upload(备份本地文件到云盘),download(备份云盘文件到本地) mode - 模式支持两种: upload(备份本地文件到云盘),download(备份云盘文件到本地)
driveName - 网盘名称backup(备份盘)resource(资源盘)
例子: 例子:
1. 查看帮助 1. 查看帮助
aliyunpan sync start -h aliyunpan sync start -h
2. 使用命令行配置启动同步备份服务将本地目录 D:\tickstep\Documents\设计文档 中的文件备份上传到云盘目录 /sync_drive/我的文档 2. 使用命令行配置启动同步备份服务将本地目录 D:\tickstep\Documents\设计文档 中的文件备份上传到"备份盘"云盘目录 /sync_drive/我的文档
aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "upload" aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "upload" -drive "backup"
3. 使用命令行配置启动同步备份服务将云盘目录 /sync_drive/我的文档 中的文件备份下载到本地目录 D:\tickstep\Documents\设计文档 3. 使用命令行配置启动同步备份服务将云盘目录 /sync_drive/我的文档 中的文件备份下载到本地目录 D:\tickstep\Documents\设计文档
aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "download" aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "download"
@ -158,6 +160,7 @@ mode - 模式,支持两种: upload(备份本地文件到云盘),download(备
localDir := c.String("ldir") localDir := c.String("ldir")
panDir := c.String("pdir") panDir := c.String("pdir")
mode := c.String("mode") mode := c.String("mode")
driveName := c.String("drive")
if localDir != "" && panDir != "" { if localDir != "" && panDir != "" {
// make path absolute // make path absolute
if !utils.IsLocalAbsPath(localDir) { if !utils.IsLocalAbsPath(localDir) {
@ -199,6 +202,18 @@ mode - 模式,支持两种: upload(备份本地文件到云盘),download(备
task.Id = utils.Md5Str(task.LocalFolderPath) task.Id = utils.Md5Str(task.LocalFolderPath)
task.Priority = syncOpt task.Priority = syncOpt
task.UserId = activeUser.UserId task.UserId = activeUser.UserId
// drive id
task.DriveName = driveName
if strings.ToLower(task.DriveName) == "backup" {
task.DriveId = activeUser.DriveList.GetFileDriveId()
} else if strings.ToLower(task.DriveName) == "resource" {
task.DriveId = activeUser.DriveList.GetResourceDriveId()
}
if len(task.DriveId) == 0 {
task.DriveName = "backup"
task.DriveId = activeUser.DriveList.GetFileDriveId()
}
} }
RunSync(task, dp, up, downloadBlockSize, uploadBlockSize, syncOpt, c.Int("ldt")) RunSync(task, dp, up, downloadBlockSize, uploadBlockSize, syncOpt, c.Int("ldt"))
@ -206,6 +221,10 @@ mode - 模式,支持两种: upload(备份本地文件到云盘),download(备
return nil return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{
Name: "drive",
Usage: "drive name, 网盘名称backup(备份盘)resource(资源盘)",
},
cli.StringFlag{ cli.StringFlag{
Name: "ldir", Name: "ldir",
Usage: "local dir, 本地文件夹完整路径", Usage: "local dir, 本地文件夹完整路径",
@ -310,7 +329,7 @@ func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadPa
LocalFileModifiedCheckIntervalSec: localDelayTime, LocalFileModifiedCheckIntervalSec: localDelayTime,
FileRecorder: fileRecorder, FileRecorder: fileRecorder,
} }
syncMgr := syncdrive.NewSyncTaskManager(activeUser, activeUser.DriveList.GetFileDriveId(), panClient, syncFolderRootPath, option) syncMgr := syncdrive.NewSyncTaskManager(activeUser, panClient, syncFolderRootPath, option)
syncConfigFile := syncMgr.ConfigFilePath() syncConfigFile := syncMgr.ConfigFilePath()
if tasks != nil { if tasks != nil {
syncConfigFile = "(使用命令行配置)" syncConfigFile = "(使用命令行配置)"

View File

@ -32,6 +32,8 @@ type (
Id string `json:"id"` Id string `json:"id"`
// UserId 账号ID // UserId 账号ID
UserId string `json:"userId"` UserId string `json:"userId"`
// DriveName 网盘名称backup-备份盘resource-资源盘
DriveName string `json:"driveName"`
// DriveId 网盘ID目前支持文件网盘 // DriveId 网盘ID目前支持文件网盘
DriveId string `json:"-"` DriveId string `json:"-"`
// LocalFolderPath 本地目录 // LocalFolderPath 本地目录
@ -41,9 +43,9 @@ type (
// Mode 同步模式 // Mode 同步模式
Mode SyncMode `json:"mode"` Mode SyncMode `json:"mode"`
// CycleMode 循环模式OneTime-运行一次InfiniteLoop-无限循环模式 // CycleMode 循环模式OneTime-运行一次InfiniteLoop-无限循环模式
CycleModeType CycleMode `json:"cycleModeType"` CycleModeType CycleMode `json:"-"`
// Priority 优先级选项 // Priority 优先级选项
Priority SyncPriorityOption `json:"priority"` Priority SyncPriorityOption `json:"-"`
// LastSyncTime 上一次同步时间 // LastSyncTime 上一次同步时间
LastSyncTime string `json:"lastSyncTime"` LastSyncTime string `json:"lastSyncTime"`
@ -119,6 +121,11 @@ func (t *SyncTask) String() string {
} }
builder.WriteString("本地目录: " + t.LocalFolderPath + "\n") builder.WriteString("本地目录: " + t.LocalFolderPath + "\n")
builder.WriteString("云盘目录: " + t.PanFolderPath + "\n") builder.WriteString("云盘目录: " + t.PanFolderPath + "\n")
driveName := "备份盘"
if strings.ToLower(t.DriveName) == "resource" {
driveName = "资源盘"
}
builder.WriteString("目标网盘: " + driveName + "\n")
return builder.String() return builder.String()
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/tickstep/library-go/logger" "github.com/tickstep/library-go/logger"
"io/ioutil" "io/ioutil"
"path" "path"
"strings"
"time" "time"
) )
@ -38,7 +39,6 @@ type (
syncDriveConfig *SyncDriveConfig syncDriveConfig *SyncDriveConfig
syncOption SyncOption syncOption SyncOption
PanUser *config.PanUser PanUser *config.PanUser
DriveId string
PanClient *config.PanClient PanClient *config.PanClient
SyncConfigFolderPath string SyncConfigFolderPath string
@ -57,11 +57,10 @@ var (
ErrSyncTaskListEmpty error = fmt.Errorf("no sync task") ErrSyncTaskListEmpty error = fmt.Errorf("no sync task")
) )
func NewSyncTaskManager(user *config.PanUser, driveId string, panClient *config.PanClient, syncConfigFolderPath string, func NewSyncTaskManager(user *config.PanUser, panClient *config.PanClient, syncConfigFolderPath string,
option SyncOption) *SyncTaskManager { option SyncOption) *SyncTaskManager {
return &SyncTaskManager{ return &SyncTaskManager{
PanUser: user, PanUser: user,
DriveId: driveId,
PanClient: panClient, PanClient: panClient,
SyncConfigFolderPath: syncConfigFolderPath, SyncConfigFolderPath: syncConfigFolderPath,
syncOption: option, syncOption: option,
@ -80,6 +79,7 @@ func (m *SyncTaskManager) parseConfigFile() error {
"localFolderPath": "D:\\smb\\datadisk\\game", "localFolderPath": "D:\\smb\\datadisk\\game",
"panFolderPath": "/sync_drive/game", "panFolderPath": "/sync_drive/game",
"mode": "sync", "mode": "sync",
"driveName": "backup",
"lastSyncTime": "" "lastSyncTime": ""
} }
] ]
@ -139,6 +139,16 @@ 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()
} }
// check driveId
if strings.ToLower(task.DriveName) == "backup" {
task.DriveId = m.PanUser.DriveList.GetFileDriveId()
} else if strings.ToLower(task.DriveName) == "resource" {
task.DriveId = m.PanUser.DriveList.GetResourceDriveId()
}
if len(task.DriveId) == 0 {
task.DriveId = m.PanUser.DriveList.GetFileDriveId()
}
// check userId // check userId
if len(task.UserId) > 0 { if len(task.UserId) > 0 {
if task.UserId != m.PanUser.UserId { if task.UserId != m.PanUser.UserId {
@ -161,7 +171,6 @@ func (m *SyncTaskManager) Start(tasks []*SyncTask) (bool, error) {
continue continue
} }
task.panUser = m.PanUser task.panUser = m.PanUser
task.DriveId = m.DriveId
task.syncDbFolderPath = m.SyncConfigFolderPath task.syncDbFolderPath = m.SyncConfigFolderPath
task.panClient = m.PanClient task.panClient = m.PanClient
task.syncOption = m.syncOption task.syncOption = m.syncOption