Skip to content

Commit

Permalink
[♻️] refactor: improved project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaLaboratory committed May 11, 2024
1 parent cbc9580 commit 896a5d9
Show file tree
Hide file tree
Showing 28 changed files with 517 additions and 361 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ COPY go.mod go.sum ./
RUN go mod tidy

COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /app/app ./cmd
RUN CGO_ENABLED=0 go build -a -trimpath -ldflags="-s -w" -o /app/app .

FROM bitnami/minideb:latest

WORKDIR /app
RUN install_packages wget gnupg2 lsb-release ca-certificates && \
echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
apt remove -y wget gnupg2 lsb-release && apt autoremove -y && apt update
apt remove -y wget gnupg2 lsb-release && apt autoremove -y && apt update && install_packages postgresql-client-16

COPY --from=build /app/app /bin/postgres-backup
COPY --from=build /app/app /usr/local/bin/postgres-backup

CMD ["postgres-backup", "upload-schedule"]
CMD ["postgres-backup", "backup"]
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ docker run -v ./config.hcl:/etc/postgres_backup/config.hcl ghcr.io/deltalaborato
## docker compose
```yaml
backup:
image: ghcr.io/deltalaboratory/postgres-backup:v0.0.1
image: ghcr.io/deltalaboratory/postgres-backup:latest
volumes:
- ./postgres-backup.hcl:/etc/postgres_backup/config.hcl
restart: unless-stopped
Expand All @@ -38,7 +38,7 @@ postgres {
}
# backup storage configuration
upload {
storage {
# S3 storage configuration
s3 {
# S3 endpoint
Expand All @@ -59,18 +59,20 @@ upload {
}
}

# backup schedule, required when using `upload-schedule` command
compress {
# algorithm, support `zstd`, optional
algorithm = "zstd"
# compress level, optional
# for zstd, see https://github.com/klauspost/compress/tree/master/zstd#compressor for more information, default 3
compress_level = 12
}

# backup schedule, required when using `storage-schedule` command
# see https://pkg.go.dev/github.com/robfig/cron#hdr-CRON_Expression_Format for more information
schedule = [
"0 1 * * *",
]

# compress algorithm, support `zstd`, optional
compress_algorithm = "zstd"
# compress level, optional
# for zstd, see https://github.com/klauspost/compress/tree/master/zstd#compressor for more information, default 3
compress_level = 12

# verbose mode
verbose = false
```
Expand Down
21 changes: 21 additions & 0 deletions cmd/backup.go
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)
}
34 changes: 0 additions & 34 deletions cmd/main.go

This file was deleted.

54 changes: 54 additions & 0 deletions cmd/root.go
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)")
}
37 changes: 37 additions & 0 deletions cmd/schedule/run.go
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)
}
19 changes: 19 additions & 0 deletions cmd/schedule/schedule.go
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)
}
56 changes: 0 additions & 56 deletions cmd/upload.go

This file was deleted.

23 changes: 12 additions & 11 deletions go.mod
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
)
Loading

0 comments on commit 896a5d9

Please sign in to comment.