2023-07-18 12:05:22 +02:00
|
|
|
package cmd
|
2023-07-10 12:19:21 +02:00
|
|
|
|
|
|
|
import (
|
2023-07-18 12:05:22 +02:00
|
|
|
"context"
|
2023-07-10 12:19:21 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"gitea.urkob.com/urko/backblaze-backup/internal/services"
|
2023-07-10 14:45:52 +02:00
|
|
|
"gitea.urkob.com/urko/backblaze-backup/kit/config"
|
2023-07-10 12:19:21 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-07-18 12:05:22 +02:00
|
|
|
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) {
|
2023-07-18 12:05:22 +02:00
|
|
|
ctx, cancel := context.WithCancel(signalContext(cmd.Context()))
|
|
|
|
defer cancel()
|
|
|
|
|
2023-07-26 12:38:34 +02:00
|
|
|
log.SetFlags(log.Ldate)
|
2023-07-10 12:19:21 +02:00
|
|
|
filePath, err := cmd.Flags().GetString("file")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("file %w", err)
|
|
|
|
}
|
|
|
|
dir, err := cmd.Flags().GetString("dir")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("dir %w", err)
|
|
|
|
}
|
|
|
|
bucketName, err := cmd.Flags().GetString("bucket")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("bucket %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-10 14:45:52 +02:00
|
|
|
envFile := ""
|
|
|
|
if os.Getenv("BACKBLAZE_ENV") == "dev" {
|
|
|
|
envFile = ".env"
|
|
|
|
}
|
|
|
|
cfg := config.NewConfig(envFile)
|
2023-07-10 12:19:21 +02:00
|
|
|
|
2023-07-10 14:45:52 +02:00
|
|
|
bbService := services.NewBackBlaze(cfg.BbId, cfg.BbKey).WithBucket(bucketName).WithDir(dir).WithFile(filePath)
|
2023-07-18 12:05:22 +02:00
|
|
|
if err := bbService.Sync(ctx); err != nil {
|
2023-07-10 12:19:21 +02:00
|
|
|
log.Fatalln("bbService.Sync()", err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|