From 11d9310f2da916a1fbf4f40e8110b0554b8dbed7 Mon Sep 17 00:00:00 2001 From: Urko Date: Mon, 10 Jul 2023 12:19:21 +0200 Subject: [PATCH] feat: tune up backblaze script --- cmd/main.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 14 ++++++++++++++ go.sum | 14 ++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 cmd/main.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..75851bd --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "log" + "os" + + "gitea.urkob.com/urko/backblaze-backup/internal/services" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "backblaze-backup", + Short: "Backblaze backup tool", + Long: `A tool to backup files and directories to Backblaze.`, + Run: func(cmd *cobra.Command, args []string) { + 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) + } + + id := os.Getenv("BB_ID") + key := os.Getenv("BB_KEY") + + bbService := services.NewBackBlaze(id, key).WithBucket(bucketName).WithDir(dir).WithFile(filePath) + if err := bbService.Sync(); err != nil { + log.Fatalln("bbService.Sync()", err) + } + }, +} + +func init() { + rootCmd.PersistentFlags().String("file", "", "absolute path of the file you want to upload to backblaze") + rootCmd.PersistentFlags().String("dir", "", "absolute path of the directory you want to upload to backblaze") + rootCmd.PersistentFlags().String("bucket", "", "backblaze bucket name") +} + +func main() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..83e76c6 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module gitea.urkob.com/urko/backblaze-backup + +go 1.20 + +require ( + github.com/kurin/blazer v0.5.3 + github.com/spf13/cobra v1.7.0 + golang.org/x/sync v0.3.0 +) + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0caf7bd --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kurin/blazer v0.5.3 h1:SAgYv0TKU0kN/ETfO5ExjNAPyMt2FocO2s/UlCHfjAk= +github.com/kurin/blazer v0.5.3/go.mod h1:4FCXMUWo9DllR2Do4TtBd377ezyAJ51vB5uTBjt0pGU= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=