76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"gitea.urkob.com/urko/backblaze-backup/internal/services/backblaze"
|
|
"gitea.urkob.com/urko/backblaze-backup/kit"
|
|
"gitea.urkob.com/urko/backblaze-backup/kit/config"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var Sync = &cobra.Command{
|
|
Use: "sync",
|
|
Short: "Sync files or directories to Backblaze",
|
|
Long: `A tool to backup files and directories to Backblaze.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
ctx, cancel := context.WithCancel(signalContext(cmd.Context()))
|
|
defer cancel()
|
|
|
|
log.SetFlags(log.Ldate | log.Lmicroseconds)
|
|
filePath, err := cmd.Flags().GetString("file")
|
|
if err != nil {
|
|
panic(fmt.Errorf("file %w", err))
|
|
}
|
|
dir, err := cmd.Flags().GetString("dir")
|
|
if err != nil {
|
|
panic(fmt.Errorf("dir %w", err))
|
|
}
|
|
bucketName, err := cmd.Flags().GetString("bucket")
|
|
if err != nil {
|
|
panic(fmt.Errorf("bucket %w", err))
|
|
}
|
|
|
|
workers, err := cmd.Flags().GetInt("workers")
|
|
if err != nil {
|
|
panic(fmt.Errorf("workers %w", err))
|
|
}
|
|
|
|
concurrentUploads, err := cmd.Flags().GetInt("concurrent-uploads")
|
|
if err != nil {
|
|
panic(fmt.Errorf("workers %w", err))
|
|
}
|
|
|
|
envFile := ""
|
|
if os.Getenv("BACKBLAZE_ENV") == "dev" {
|
|
envFile = kit.RootDir() + "/.env"
|
|
}
|
|
|
|
cfg := config.NewConfig(envFile)
|
|
logger := logrus.New()
|
|
logger.SetLevel(logrus.Level(cfg.LogLevel))
|
|
|
|
bbService, err := backblaze.NewBackBlaze(ctx, logger, cfg.BbId, cfg.BbKey)
|
|
if err != nil {
|
|
panic(fmt.Errorf("NewBackBlaze %w", err))
|
|
}
|
|
|
|
bbService = bbService.WithOptions(backblaze.BackBlazeOptions{
|
|
Bucket: bucketName,
|
|
Dir: dir,
|
|
FilePath: filePath,
|
|
MaxWorkers: workers,
|
|
ConcurrentUploads: concurrentUploads,
|
|
})
|
|
|
|
if err := bbService.Sync(ctx); err != nil {
|
|
panic(fmt.Errorf("bbService.Sync %w", err))
|
|
}
|
|
},
|
|
}
|