47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
|
package internal
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/minio/minio-go/v7"
|
||
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
MinioClient *minio.Client
|
||
|
)
|
||
|
|
||
|
func Backup(ctx context.Context) error {
|
||
|
var err error
|
||
|
var getErrors = []string{}
|
||
|
bucketNames := GetEnvWithDefault("BUCKET_NAMES", "")
|
||
|
backupRoot := GetEnvWithDefault("BACKUP_ROOT", "/backup")
|
||
|
|
||
|
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 {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
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 {
|
||
|
getErrors = append(getErrors, getError.Error())
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if len(getErrors) > 0 {
|
||
|
err = fmt.Errorf("%d errors retrieving objects: %s", len(getErrors), strings.Join(getErrors, ", "))
|
||
|
}
|
||
|
|
||
|
return err
|
||
|
}
|