Compare commits

..

No commits in common. "09f5f93022616d49c40e87d8a03080d67254dac0" and "50bc0f262db8a3770ffdfcd5f80c5a330dae778d" have entirely different histories.

5 changed files with 15 additions and 69 deletions

View File

@ -1,26 +1,9 @@
version: '3'
vars:
NAME: code.jhot.me/jhot/docker-s3-backup
tasks:
default:
vars:
TAG: latest
build:
cmds:
- docker buildx build --platform linux/amd64,linux/arm64 -t "{{.NAME}}:{{.TAG}}" .
push:
cmds:
- docker push "{{.NAME}}:{{.TAG}}"
tag:
prompt: This will push a git tag and docker image versioned {{.TAG}}... Do you want to continue?
cmds:
- task: build
- docker buildx build -t {{.NAME}}:{{.TAG}} .
vars:
NAME: "{{.NAME}}"
- task: push
vars:
NAME: "{{.NAME}}"
- git tag "{{.TAG}}"
- git push origin "{{.TAG}}"
NAME: docker-s3-backup
TAG: latest

2
go.mod
View File

@ -1,6 +1,6 @@
module code.jhot.me/jhot/docker-s3-backup
go 1.21
go 1.20
require (
github.com/go-co-op/gocron v1.33.1

View File

@ -11,49 +11,28 @@ import (
var (
MinioClient *minio.Client
backupBuckets = []string{}
backupRoot string
s3Host string
s3Secure bool
s3Region string
s3User string
s3Pass string
)
func init() {
for _, bucket := range strings.Split(GetEnvWithDefault("BUCKET_NAMES", ""), ",") {
backupBuckets = append(backupBuckets, strings.TrimSpace(bucket))
}
backupRoot = GetEnvWithDefault("BACKUP_ROOT", "/backup")
s3Host = GetEnvWithDefault("S3_HOST", "localhost:9000")
s3Secure = GetEnvWithDefault("S3_USE_SSL", "") == "true"
s3Region = GetEnvWithDefault("S3_REGION", "us-east-1")
s3User = GetEnvWithDefault("S3_ACCESS_KEY_ID", "")
s3Pass = GetEnvWithDefault("S3_ACCESS_KEY_SECRET", "")
}
func Backup(ctx context.Context) error {
logger.Info("Starting backup")
var err error
var getErrors = []string{}
bucketNames := GetEnvWithDefault("BUCKET_NAMES", "")
backupRoot := GetEnvWithDefault("BACKUP_ROOT", "/backup")
MinioClient, err = minio.New(s3Host, &minio.Options{
Creds: credentials.NewStaticV4(s3User, s3Pass, ""),
Secure: s3Secure,
Region: s3Region,
MinioClient, err = minio.New(GetEnvWithDefault("S3_HOST", "localhost"), &minio.Options{
Creds: credentials.NewStaticV4(GetEnvWithDefault("S3_ACCESS_KEY_ID", ""), GetEnvWithDefault("S3_ACCESS_KEY_SECRET", ""), ""),
Secure: GetEnvWithDefault("S3_USE_SSL", "") == "true",
Region: GetEnvWithDefault("S3_REGION", "us-east-1"),
})
if err != nil {
logger.Error("Error connecting to S3", "error", err)
return err
}
logger.Debug("Connected to S3", "host", s3Host)
for _, bucket := range backupBuckets {
logger.Debug("Backing up bucket", "bucket", bucket)
for _, bucket := range strings.Split(bucketNames, ",") {
bucket = strings.TrimSpace(bucket)
for object := range MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{Recursive: true}) {
getError := MinioClient.FGetObject(ctx, bucket, object.Key, fmt.Sprintf("%s/%s/%s", backupRoot, bucket, object.Key), minio.GetObjectOptions{})
if getError != nil {
logger.Error("Error getting object", "bucket", bucket, "error", getError)
getErrors = append(getErrors, getError.Error())
}
}

View File

@ -1,14 +0,0 @@
package internal
import (
"log/slog"
"os"
)
var (
logger *slog.Logger
)
func init() {
logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
}

View File

@ -12,14 +12,12 @@ var (
)
func StartSchedule(ctx context.Context) error {
logger.Info("Starting schedule")
cronExpression := GetEnvWithDefault("SCHEDULE_CRON", "0 0 * * *")
scheduler = gocron.NewScheduler(time.Local)
job, err := scheduler.Cron(cronExpression).Do(Backup, ctx)
_, err := scheduler.Cron(cronExpression).Do(Backup, ctx)
if err != nil {
return err
}
logger.Debug("Next scheduled backup", "time", job.NextRun())
scheduler.StartBlocking()
return nil
}