backblaze-backup/cmd/sync.go

58 lines
1.4 KiB
Go
Raw Permalink Normal View History

package cmd
2023-07-10 12:19:21 +02:00
import (
"context"
"fmt"
2023-07-10 12:19:21 +02:00
"log"
"os"
"gitea.urkob.com/urko/backblaze-backup/internal/services/backblaze"
"gitea.urkob.com/urko/backblaze-backup/kit"
2023-07-10 14:45:52 +02:00
"gitea.urkob.com/urko/backblaze-backup/kit/config"
"github.com/sirupsen/logrus"
2023-07-10 12:19:21 +02:00
"github.com/spf13/cobra"
)
var Sync = &cobra.Command{
Use: "sync",
Short: "Sync files or directories to Backblaze",
2023-07-10 12:19:21 +02:00
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()
2023-07-26 17:59:14 +02:00
log.SetFlags(log.Ldate | log.Lmicroseconds)
2023-07-10 12:19:21 +02:00
filePath, err := cmd.Flags().GetString("file")
if err != nil {
panic(fmt.Errorf("file %w", err))
2023-07-10 12:19:21 +02:00
}
dir, err := cmd.Flags().GetString("dir")
if err != nil {
panic(fmt.Errorf("dir %w", err))
2023-07-10 12:19:21 +02:00
}
bucketName, err := cmd.Flags().GetString("bucket")
if err != nil {
panic(fmt.Errorf("bucket %w", err))
2023-07-10 12:19:21 +02:00
}
2023-07-10 14:45:52 +02:00
envFile := ""
if os.Getenv("BACKBLAZE_ENV") == "dev" {
envFile = kit.RootDir() + "/.env"
2023-07-10 14:45:52 +02:00
}
2023-07-10 14:45:52 +02:00
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))
}
2023-07-10 12:19:21 +02:00
bbService = bbService.WithBucket(bucketName).WithDir(dir).WithFile(filePath)
if err := bbService.Sync(ctx); err != nil {
panic(fmt.Errorf("bbService.Sync %w", err))
2023-07-10 12:19:21 +02:00
}
},
}