aliyunpan/internal/command/sync.go

434 lines
15 KiB
Go
Raw Permalink Normal View History

2022-06-09 19:58:16 +08:00
// Copyright (c) 2020 tickstep.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
2024-03-02 00:55:46 +08:00
// http://www.apache.org/licenses/LICENSE-2.0
2022-06-09 19:58:16 +08:00
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package command
import (
"fmt"
2022-06-10 13:55:00 +08:00
"github.com/tickstep/aliyunpan-api/aliyunpan"
2022-06-09 19:58:16 +08:00
"github.com/tickstep/aliyunpan/cmder"
"github.com/tickstep/aliyunpan/internal/config"
2024-03-02 00:55:46 +08:00
"github.com/tickstep/aliyunpan/internal/global"
2022-12-19 20:41:05 +08:00
"github.com/tickstep/aliyunpan/internal/log"
2022-06-09 19:58:16 +08:00
"github.com/tickstep/aliyunpan/internal/syncdrive"
"github.com/tickstep/aliyunpan/internal/utils"
2022-06-10 14:36:52 +08:00
"github.com/tickstep/library-go/converter"
2022-06-09 19:58:16 +08:00
"github.com/tickstep/library-go/logger"
"github.com/urfave/cli"
"os"
2022-06-17 10:27:53 +08:00
"path"
2022-06-09 19:58:16 +08:00
"strings"
"time"
)
func CmdSync() cli.Command {
return cli.Command{
Name: "sync",
Usage: "同步备份功能(Beta)",
UsageText: cmder.App().Name + " sync",
Description: `
2024-03-16 17:06:55 +08:00
备份功能支持备份本地文件到云盘备份云盘文件到本地两种模式支持JavaScript插件对备份文件进行过滤
指定本地目录和对应的一个网盘目录以备份文件网盘目录必须和本地目录独占使用不要用作其他用途不然备份可能会有问题
2022-06-17 10:27:53 +08:00
2024-03-17 16:17:13 +08:00
备份功能支持以下模式
2022-06-09 20:05:30 +08:00
1. upload
备份本地文件即上传本地文件到网盘始终保持本地文件有一个完整的备份在网盘
2. download
备份云盘文件即下载网盘文件到本地始终保持网盘的文件有一个完整的备份在本地
2022-06-09 19:58:16 +08:00
2022-06-10 22:39:54 +08:00
请输入以下命令查看如何配置和启动
aliyunpan sync start -h
2022-06-09 19:58:16 +08:00
`,
Category: "阿里云盘",
2022-12-10 14:07:28 +08:00
Before: ReloadConfigFunc,
2022-06-09 19:58:16 +08:00
Action: func(c *cli.Context) error {
2022-06-10 22:39:54 +08:00
cli.ShowCommandHelp(c, c.Command.Name)
return nil
},
Subcommands: []cli.Command{
{
Name: "start",
Usage: "启动sync同步备份任务",
UsageText: cmder.App().Name + " sync start [arguments...]",
Description: `
2022-06-17 10:27:53 +08:00
备份本地文件到文件网盘或者备份文件网盘的文件到本地支持命令行配置启动或者使用备份配置文件启动同步备份任务
配置文件保存在(配置目录)/sync_drive/sync_drive_config.json样例如下
2022-06-10 22:39:54 +08:00
{
"configVer": "1.0",
"syncTaskList": [
{
"name": "设计文档备份",
"localFolderPath": "D:/tickstep/Documents/设计文档",
2022-06-10 22:39:54 +08:00
"panFolderPath": "/sync_drive/我的文档",
"mode": "upload",
"policy" "increment"
"driveName": "backup"
2022-06-10 22:39:54 +08:00
}
]
}
相关字段说明如下
name - 任务名称
localFolderPath - 本地目录
panFolderPath - 网盘目录
mode - 备份模式支持两种: upload(备份本地文件到云盘),download(备份云盘文件到本地)
policy - 备份策略, 支持两种: exclusive(排他备份文件目标目录多余的文件会被删除),increment(增量备份文件目标目录多余的文件不会被删除)
driveName - 网盘名称backup(备份盘)resource(资源盘)
2022-06-17 10:27:53 +08:00
2022-06-10 22:39:54 +08:00
例子:
1. 查看帮助
aliyunpan sync start -h
2022-06-17 10:27:53 +08:00
2. 使用命令行配置启动同步备份服务将本地目录 D:\tickstep\Documents\设计文档 中的文件备份上传到"备份盘"的云盘目录 /sync_drive/我的文档
aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "upload" -drive "backup"
2022-06-17 10:27:53 +08:00
3. 使用命令行配置启动同步备份服务将云盘目录 /sync_drive/我的文档 中的文件备份下载到本地目录 D:\tickstep\Documents\设计文档
aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "download"
2022-06-10 13:55:00 +08:00
2024-03-16 17:06:55 +08:00
4. 使用命令行配置启动同步备份服务将本地目录 D:\tickstep\Documents\设计文档 中的文件备份到云盘目录 /sync_drive/我的文档
2022-06-17 10:27:53 +08:00
同时配置下载并发为2上传并发为1下载分片大小为256KB上传分片大小为1MB
aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "upload" -dp 2 -up 1 -dbs 256 -ubs 1024
2024-03-16 17:06:55 +08:00
5. 使用配置文件启动同步备份服务使用配置文件可以支持同时启动多个备份任务配置文件必须存在否则启动失败
2022-06-10 22:39:54 +08:00
aliyunpan sync start
2022-06-10 13:55:00 +08:00
2024-03-16 17:06:55 +08:00
6. 使用配置文件启动同步备份服务并配置下载并发为2上传并发为1下载分片大小为256KB上传分片大小为1MB
aliyunpan sync start -dp 2 -up 1 -dbs 256 -ubs 1024
2022-06-10 13:55:00 +08:00
2022-06-10 22:39:54 +08:00
`,
Action: func(c *cli.Context) error {
if config.Config.ActiveUser() == nil {
fmt.Println("未登录账号")
return nil
}
activeUser := GetActiveUser()
2022-08-13 19:38:10 +08:00
if c.String("log") == "true" {
syncdrive.LogPrompt = true
} else {
syncdrive.LogPrompt = false
}
2022-06-10 22:39:54 +08:00
dp := c.Int("dp")
if dp == 0 {
dp = config.Config.MaxDownloadParallel
}
if dp == 0 {
dp = 2
}
up := c.Int("up")
if up == 0 {
up = config.Config.MaxUploadParallel
}
if up == 0 {
up = 2
}
downloadBlockSize := int64(c.Int("dbs") * 1024)
if downloadBlockSize == 0 {
downloadBlockSize = int64(config.Config.CacheSize)
}
2022-06-10 22:39:54 +08:00
if downloadBlockSize == 0 {
downloadBlockSize = int64(256 * 1024)
}
uploadBlockSize := int64(c.Int("ubs") * 1024)
if uploadBlockSize == 0 {
uploadBlockSize = aliyunpan.DefaultChunkSize
}
2022-08-13 18:38:29 +08:00
var syncOpt syncdrive.SyncPriorityOption = syncdrive.SyncPriorityTimestampFirst
2024-03-16 17:13:10 +08:00
//opt := c.String("pri")
//if opt == "local" {
// syncOpt = syncdrive.SyncPriorityLocalFirst
//} else if opt == "pan" {
// syncOpt = syncdrive.SyncPriorityPanFirst
//} else {
// syncOpt = syncdrive.SyncPriorityTimestampFirst
//}
2022-08-13 18:38:29 +08:00
2022-06-17 10:27:53 +08:00
var task *syncdrive.SyncTask
localDir := c.String("ldir")
panDir := c.String("pdir")
2022-06-17 23:37:32 +08:00
mode := c.String("mode")
policy := c.String("policy")
driveName := c.String("drive")
2022-06-17 10:27:53 +08:00
if localDir != "" && panDir != "" {
2022-08-13 18:38:29 +08:00
// make path absolute
if !utils.IsLocalAbsPath(localDir) {
2022-08-13 18:38:29 +08:00
pwd, _ := os.Getwd()
localDir = path.Join(pwd, path.Clean(localDir))
}
panDir = activeUser.PathJoin(activeUser.ActiveDriveId, panDir)
if !utils.IsLocalAbsPath(localDir) {
fmt.Println("本地目录请指定绝对路径")
return nil
}
if !utils.IsPanAbsPath(panDir) {
fmt.Println("网盘目录请指定绝对路径")
return nil
}
2022-06-17 23:37:32 +08:00
//if b, e := utils.PathExists(localDir); e == nil {
// if !b {
// fmt.Println("本地文件夹不存在:", localDir)
// return nil
// }
//} else {
// fmt.Println("本地文件夹不存在:", localDir)
// return nil
//}
2022-06-17 10:27:53 +08:00
task = &syncdrive.SyncTask{}
task.LocalFolderPath = path.Clean(strings.ReplaceAll(localDir, "\\", "/"))
task.PanFolderPath = panDir
task.Mode = syncdrive.Upload
if mode == string(syncdrive.Upload) {
task.Mode = syncdrive.Upload
} else if mode == string(syncdrive.Download) {
task.Mode = syncdrive.Download
2022-06-17 23:37:32 +08:00
} else if mode == string(syncdrive.SyncTwoWay) {
task.Mode = syncdrive.SyncTwoWay
} else {
task.Mode = syncdrive.Upload
}
if policy == string(syncdrive.SyncPolicyExclusive) {
task.Policy = syncdrive.SyncPolicyExclusive
} else {
task.Policy = syncdrive.SyncPolicyIncrement
2022-06-17 10:27:53 +08:00
}
task.Name = path.Base(task.LocalFolderPath)
task.Id = utils.Md5Str(task.LocalFolderPath)
2022-08-13 18:38:29 +08:00
task.Priority = syncOpt
2022-10-28 10:40:59 +08:00
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()
}
}
2022-06-17 10:27:53 +08:00
2024-04-21 11:32:49 +08:00
cycleMode := syncdrive.CycleInfiniteLoop
if c.String("cycle") == "onetime" {
cycleMode = syncdrive.CycleOneTime
} else {
cycleMode = syncdrive.CycleInfiniteLoop
}
scanIntervalTime := int64(c.Int("sit") * 60)
if scanIntervalTime == 0 {
// 默认1分钟
scanIntervalTime = 60
}
RunSync(task, cycleMode, dp, up, downloadBlockSize, uploadBlockSize, syncOpt, c.Int("ldt"), scanIntervalTime)
2022-06-10 22:39:54 +08:00
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "drive",
Usage: "drive name, 网盘名称backup(备份盘)resource(资源盘)",
2024-03-17 16:17:13 +08:00
Value: "backup",
},
2022-06-17 10:27:53 +08:00
cli.StringFlag{
Name: "ldir",
Usage: "local dir, 本地文件夹完整路径",
},
cli.StringFlag{
Name: "pdir",
Usage: "pan dir, 云盘文件夹完整路径",
},
cli.StringFlag{
Name: "mode",
2024-03-16 17:13:10 +08:00
Usage: "备份模式, 支持两种: upload(备份本地文件到云盘),download(备份云盘文件到本地)",
2022-06-17 10:27:53 +08:00
Value: "upload",
},
cli.StringFlag{
Name: "policy",
Usage: "备份策略, 支持两种: exclusive(排他备份文件,目标目录多余的文件会被删除),increment(增量备份文件,目标目录多余的文件不会被删除)",
Value: "increment",
},
2024-04-21 18:37:46 +08:00
//cli.StringFlag{
// Name: "pri",
// Usage: "同步优先级只对sync模式有效。当网盘和本地存在同名文件优先使用哪个选项支持三种: time-时间优先local-本地优先pan-网盘优先",
// Value: "time",
//},
2024-04-21 11:32:49 +08:00
cli.StringFlag{
Name: "cycle",
Usage: "备份周期, 支持两种: infinity(永久循环备份),onetime(只运行一次备份)",
Value: "infinity",
},
2022-06-10 22:39:54 +08:00
cli.IntFlag{
Name: "dp",
Usage: "download parallel, 下载并发数量即可以同时并发下载多少个文件。0代表跟从配置文件设置取值范围:1 ~ 10",
Value: 0,
},
cli.IntFlag{
Name: "up",
Usage: "upload parallel, 上传并发数量即可以同时并发上传多少个文件。0代表跟从配置文件设置取值范围:1 ~ 10",
Value: 0,
},
cli.IntFlag{
Name: "dbs",
2022-06-12 17:03:05 +08:00
Usage: "download block size下载分片大小单位KB。推荐值1024 ~ 10240",
Value: 1024,
},
2022-06-10 22:39:54 +08:00
cli.IntFlag{
Name: "ubs",
2022-12-13 09:54:22 +08:00
Usage: "upload block size上传分片大小单位KB。推荐值1024 ~ 10240。当上传极大单文件时候请适当调高该值",
2022-06-10 22:39:54 +08:00
Value: 10240,
},
2022-08-13 19:38:10 +08:00
cli.StringFlag{
Name: "log",
Usage: "是否显示文件备份过程日志true-显示false-不显示",
Value: "false",
},
cli.IntFlag{
Name: "ldt",
Usage: "local delay time本地文件修改检测延迟间隔单位秒。如果本地文件会被频繁修改例如录制视频文件配置好该时间可以避免上传未录制好的文件。",
Value: 3,
},
cli.IntFlag{
Name: "sit",
Usage: "scan interval time扫描文件间隔时间单位分钟。",
Value: 1,
},
2022-06-10 22:39:54 +08:00
},
2022-06-09 19:58:16 +08:00
},
},
}
}
2024-04-21 11:32:49 +08:00
func RunSync(defaultTask *syncdrive.SyncTask, cycleMode syncdrive.CycleMode, fileDownloadParallel, fileUploadParallel int, downloadBlockSize, uploadBlockSize int64,
flag syncdrive.SyncPriorityOption, localDelayTime int, scanTimeInterval int64) {
2022-06-12 17:03:05 +08:00
maxDownloadRate := config.Config.MaxDownloadRate
maxUploadRate := config.Config.MaxUploadRate
2022-06-09 19:58:16 +08:00
activeUser := GetActiveUser()
panClient := activeUser.PanClient()
2024-03-13 15:31:28 +08:00
panClient.OpenapiPanClient().ClearCache()
2024-03-03 10:12:44 +08:00
panClient.OpenapiPanClient().DisableCache()
2022-06-09 19:58:16 +08:00
2024-03-03 10:12:44 +08:00
//// pan token expired checker
//continueFlag := int32(0)
//atomic.StoreInt32(&continueFlag, 0)
//defer func() {
// atomic.StoreInt32(&continueFlag, 1)
//}()
//go func(flag *int32) {
// for atomic.LoadInt32(flag) == 0 {
// time.Sleep(time.Duration(1) * time.Minute)
// if RefreshWebTokenInNeed(activeUser, config.Config.DeviceName) {
// logger.Verboseln("update access token for sync task")
// userWebToken := NewWebLoginToken(activeUser.WebapiToken.AccessToken, activeUser.WebapiToken.Expired)
// panClient.WebapiPanClient().UpdateToken(userWebToken)
// }
// }
//}(&continueFlag)
2022-06-09 19:58:16 +08:00
syncFolderRootPath := config.GetSyncDriveDir()
if b, e := utils.PathExists(syncFolderRootPath); e == nil {
if !b {
2022-06-16 22:30:12 +08:00
os.MkdirAll(syncFolderRootPath, 0755)
2022-06-09 19:58:16 +08:00
}
}
2022-06-17 10:27:53 +08:00
var tasks []*syncdrive.SyncTask
if defaultTask != nil {
tasks = []*syncdrive.SyncTask{}
tasks = append(tasks, defaultTask)
}
2022-06-09 19:58:16 +08:00
fmt.Println("启动同步备份进程")
2022-12-19 20:41:05 +08:00
// 文件同步记录器
fileRecorder := log.NewFileRecorder(config.GetLogDir() + "/sync_file_records.csv")
2022-08-13 16:54:42 +08:00
option := syncdrive.SyncOption{
FileDownloadParallel: fileDownloadParallel,
FileUploadParallel: fileUploadParallel,
FileDownloadBlockSize: downloadBlockSize,
FileUploadBlockSize: uploadBlockSize,
MaxDownloadRate: maxDownloadRate,
MaxUploadRate: maxUploadRate,
SyncPriority: flag,
LocalFileModifiedCheckIntervalSec: localDelayTime,
2022-12-19 20:41:05 +08:00
FileRecorder: fileRecorder,
2022-08-13 16:54:42 +08:00
}
syncMgr := syncdrive.NewSyncTaskManager(activeUser, panClient, syncFolderRootPath, option)
2022-06-17 10:27:53 +08:00
syncConfigFile := syncMgr.ConfigFilePath()
if tasks != nil {
syncConfigFile = "(使用命令行配置)"
}
fmt.Printf("备份配置文件:%s\n下载并发%d\n上传并发%d\n下载分片大小%s\n上传分片大小%s\n",
syncConfigFile, fileDownloadParallel, fileUploadParallel, converter.ConvertFileSize(downloadBlockSize, 2),
2022-06-10 22:48:29 +08:00
converter.ConvertFileSize(uploadBlockSize, 2))
if _, e := syncMgr.Start(tasks, cycleMode, scanTimeInterval); e != nil {
2022-06-09 19:58:16 +08:00
fmt.Println("启动任务失败:", e)
return
}
2024-03-13 15:31:28 +08:00
_, ok := os.LookupEnv("ALIYUNPAN_DOCKER")
if ok {
// in docker container
2024-04-21 18:37:46 +08:00
if cycleMode == syncdrive.CycleInfiniteLoop {
// 使用休眠以节省CPU资源
fmt.Println("本命令不会退出程序正在以Docker的方式运行。如需退出请借助Docker提供的方式。")
for {
time.Sleep(60 * time.Second)
}
} else {
for {
if syncMgr.IsAllTaskCompletely() {
fmt.Println("所有备份任务已完成")
break
}
time.Sleep(5 * time.Second)
}
2024-03-13 15:31:28 +08:00
}
} else {
2024-04-21 11:32:49 +08:00
if cycleMode == syncdrive.CycleInfiniteLoop {
if global.IsAppInCliMode {
// in cmd mode
c := ""
fmt.Println("本命令不会退出如需要结束同步备份进程请输入y然后按Enter键进行停止。")
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 {
2024-03-13 15:31:28 +08:00
for {
2024-04-21 11:32:49 +08:00
if syncMgr.IsAllTaskCompletely() {
fmt.Println("所有备份任务已完成")
break
}
time.Sleep(5 * time.Second)
}
}
2022-06-09 19:58:16 +08:00
}
2024-04-21 11:32:49 +08:00
fmt.Println("正在退出同步备份任务,请稍等...")
2024-03-13 15:31:28 +08:00
// stop task
syncMgr.Stop()
2022-06-09 19:58:16 +08:00
}