Skip to content

Commit

Permalink
Merge pull request #7 from anhnmt/v2
Browse files Browse the repository at this point in the history
Release V2
  • Loading branch information
anhnmt authored Nov 24, 2023
2 parents 2bf96b5 + 0533256 commit ae56cb0
Show file tree
Hide file tree
Showing 17 changed files with 1,085 additions and 245 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
.env
README.md
Dockerfile
docker-compose.yml
docker-compose.yml
docker-compose-*.yml
*.gz
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
.env
.env
docker-compose-*.yml
*.gz
18 changes: 10 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@ RUN go mod download
COPY . .

# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o go-cron ./cmd/go-cron/main.go
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o pg2minio ./main.go

RUN go install github.com/minio/mc@latest

FROM alpine:3.18

WORKDIR /app
RUN apk add --update --no-cache postgresql-client curl bash && \
RUN apk add --update --no-cache postgresql-client && \
rm -rf /var/cache/apk/*

COPY --from=builder /app/go-cron /usr/local/bin/go-cron
WORKDIR /app

COPY --from=builder /app/pg2minio /usr/local/bin/pg2minio
RUN chmod +x /usr/local/bin/pg2minio

COPY --from=builder /go/bin/mc /usr/local/bin/mc
RUN chmod +x /usr/local/bin/mc && chmod +x /usr/local/bin/go-cron
RUN chmod +x /usr/local/bin/mc

COPY run.sh backup.sh ./
RUN chmod +x run.sh && chmod +x backup.sh
RUN chmod 0777 /app

RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

CMD ["bash", "run.sh"]
CMD ["/usr/local/bin/pg2minio"]
#ENTRYPOINT ["tail", "-f", "/dev/null"]
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ MINIO_BUCKET=minio
MINIO_SERVER=http://127.0.0.1:9000
```

```bash
docker build -f ./Dockerfile -t ghcr.io/anhnmt/backup-pg2minio:latest .
```

#### `docker-compose up -d`
```
version: '3'
Expand Down Expand Up @@ -48,17 +44,20 @@ services:

### Optional Environment Variables

- `SCHEDULE` - Cron schedule to run periodic backups.

- `POSTGRES_PASSWORD` - Password for the PostgreSQL user, if you are using a database on the same machine this isn't usually needed.
- `POSTGRES_PORT` - Port of the PostgreSQL database, uses the default 5432.
- `POSTGRES_EXTRA_OPTS` - Extra arguments to pass to the `pg_dump` command.

- `MINIO_API_VERSION` - you can change with S3v4 or S3v2.
- `MINIO_CLEAN` - Assign a value to activate, default is 0. For example: 7d, 14d, 1m, 30s
- `SCHEDULE` - Cron schedule to run periodic backups.
- `CUSTOM_DIR` - Allows you to change the path in the bucket. e.g. abc/def (without / at the beginning and end)
- `MINIO_BACKUP_DIR` - Allows you to change the path in the bucket. e.g. abc/def (without / at the beginning and end)

# some script from
- URL : https://github.com/michaloo/go-cron
- URL : https://github.com/wonderu/docker-backup-postgres-s3
- URL : https://github.com/schickling/dockerfiles/tree/master/postgres-backup-s3
- URL : https://github.com/minio/mc
- Cron parser : [https://elmah.io/tools/cron-parser](https://elmah.io/tools/cron-parser/#0_*/5_*_*_*_*)
- More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).
- Cron parser: [https://elmah.io/tools/cron-parser](https://elmah.io/tools/cron-parser/#0_*/5_*_*_*_*)
106 changes: 0 additions & 106 deletions backup.sh

This file was deleted.

88 changes: 88 additions & 0 deletions cmd/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cmd

import (
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"

"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
)

func scheduleBackup(schedule string) {
log.Info().Msgf("New cron: %s", schedule)

var opts []cron.Option
if len(strings.Split(schedule, " ")) >= 6 {
opts = append(opts, cron.WithSeconds())
}

wg := &sync.WaitGroup{}
c := cron.New(opts...)

_, err := c.AddFunc(schedule, func() {
wg.Add(1)
defer wg.Done()

log.Info().Msgf("Start backup at: %s", time.Now().Format(time.RFC3339))
if err := start(); err != nil {
log.Err(err).Msg("Failed to start backup")
}
})
if err != nil {
log.Panic().Err(err).Msg("Failed to add cron job")
return
}

c.Start()
log.Info().Msg("Cron start")

ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch

stop(c, wg)
return
}

func start() error {
defer func() {
if err := removeFile(PgDumpFile); err != nil {
log.Err(err).Msg("Failed to remove pg_dump file")
}
}()

err := pgDump()
if err != nil {
return err
}

err = storage()
if err != nil {
return err
}

return nil
}

func stop(c *cron.Cron, wg *sync.WaitGroup) {
log.Info().Msg("Stopping")
ctx := c.Stop()
select {
case <-ctx.Done():
// expected
case <-time.After(time.Millisecond):
log.Panic().Err(fmt.Errorf("context not done even when cron Stop is completed")).Msg("Failed to stop cron")
return
}

log.Info().Msg("Waiting")
wg.Wait()

log.Info().Msg("Exiting")
os.Exit(0)
}
Loading

0 comments on commit ae56cb0

Please sign in to comment.