-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[♻️] refactor: improved project structure
- Loading branch information
1 parent
cbc9580
commit 896a5d9
Showing
28 changed files
with
517 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/DeltaLaboratory/postgres-backup/internal" | ||
) | ||
|
||
// backupCmd represents the backup command | ||
var backupCmd = &cobra.Command{ | ||
Use: "backup", | ||
Short: "Backup a PostgreSQL database", | ||
Long: `Backup a PostgreSQL database one and now`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
internal.Backup() | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(backupCmd) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/DeltaLaboratory/postgres-backup/internal/config" | ||
) | ||
|
||
var configFile string | ||
|
||
// RootCmd represents the base command when called without any subcommands | ||
var RootCmd = &cobra.Command{ | ||
Use: "postgres-backup", | ||
Short: "Backup PostgreSQL databases to somewhere", | ||
Long: `Backup PostgreSQL databases to somewhere. | ||
postgresl-backup is a tool to backup PostgreSQL databases to somewhere. It can backup to local filesystem, S3. | ||
example: | ||
"postgres-backup backup" to backup a PostgreSQL database now | ||
"postgres-backup schedule run" to run the backup schedule defined in the configuration file | ||
`, | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the RootCmd. | ||
func Execute() { | ||
err := RootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(func() { | ||
if configFile != "" { | ||
if err := config.LoadConfig(configFile); err != nil { | ||
panic(err) | ||
} | ||
return | ||
} | ||
|
||
if err := config.LoadConfig(""); err != nil { | ||
panic(err) | ||
} | ||
}) | ||
|
||
RootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "config file (default is /etc/postgres_backup/config.hcl)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package schedule | ||
|
||
import ( | ||
"github.com/robfig/cron/v3" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/DeltaLaboratory/postgres-backup/internal" | ||
"github.com/DeltaLaboratory/postgres-backup/internal/config" | ||
) | ||
|
||
// runCmd represents the run command | ||
var runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Run the backup schedule", | ||
Long: `Run the backup schedule defined in the configuration file.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(config.Loaded.Schedule) == 0 { | ||
log.Fatal().Msg("no schedule provided") | ||
} | ||
|
||
c := cron.New() | ||
for _, schedule := range config.Loaded.Schedule { | ||
if id, err := c.AddFunc(schedule, internal.Backup); err != nil { | ||
log.Fatal().Err(err).Str("schedule", schedule).Msg("failed to add schedule") | ||
} else { | ||
log.Info().Str("schedule", schedule).Str("next", c.Entry(id).Next.String()).Msg("schedule added") | ||
} | ||
} | ||
log.Info().Msg("starting cron") | ||
c.Run() | ||
}, | ||
} | ||
|
||
func init() { | ||
scheduleCmd.AddCommand(runCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package schedule | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/DeltaLaboratory/postgres-backup/cmd" | ||
) | ||
|
||
// scheduleCmd represents the schedule command | ||
var scheduleCmd = &cobra.Command{ | ||
Use: "schedule", | ||
Short: "Run, manage and monitor backup schedules.", | ||
Long: `Run, manage and monitor backup schedules. | ||
Schedules are defined in the configuration file.`, | ||
} | ||
|
||
func init() { | ||
cmd.RootCmd.AddCommand(scheduleCmd) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,39 @@ | ||
module postgres-backup | ||
module github.com/DeltaLaboratory/postgres-backup | ||
|
||
go 1.22.2 | ||
|
||
require ( | ||
github.com/hashicorp/hcl/v2 v2.20.1 | ||
github.com/klauspost/compress v1.17.8 | ||
github.com/minio/minio-go/v7 v7.0.69 | ||
github.com/minio/minio-go/v7 v7.0.70 | ||
github.com/robfig/cron/v3 v3.0.1 | ||
github.com/rs/zerolog v1.32.0 | ||
github.com/spf13/cobra v1.8.0 | ||
) | ||
|
||
require ( | ||
github.com/agext/levenshtein v1.2.3 // indirect | ||
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect | ||
github.com/dustin/go-humanize v1.0.1 // indirect | ||
github.com/goccy/go-json v0.10.2 // indirect | ||
github.com/google/go-cmp v0.6.0 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/minio/md5-simd v1.1.2 // indirect | ||
github.com/minio/sha256-simd v1.0.1 // indirect | ||
github.com/mitchellh/go-wordwrap v1.0.1 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/rs/xid v1.5.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/zclconf/go-cty v1.14.4 // indirect | ||
golang.org/x/crypto v0.22.0 // indirect | ||
golang.org/x/crypto v0.23.0 // indirect | ||
golang.org/x/mod v0.17.0 // indirect | ||
golang.org/x/net v0.24.0 // indirect | ||
golang.org/x/net v0.25.0 // indirect | ||
golang.org/x/sync v0.7.0 // indirect | ||
golang.org/x/sys v0.19.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
golang.org/x/tools v0.20.0 // indirect | ||
golang.org/x/sys v0.20.0 // indirect | ||
golang.org/x/text v0.15.0 // indirect | ||
golang.org/x/tools v0.21.0 // indirect | ||
gopkg.in/ini.v1 v1.67.0 // indirect | ||
) |
Oops, something went wrong.