feat(prune): add prune files feature

This feature allows you to specify the n latest backups to keep in the
bucket.
This commit is contained in:
2023-11-09 21:56:15 -05:00
parent d316b8ff4b
commit 3c325fd008
4 changed files with 126 additions and 48 deletions

View File

@@ -1,11 +1,10 @@
package uploader
import (
"backup-tool/helpers"
"context"
"errors"
"fmt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"log"
"os"
)
@@ -13,15 +12,15 @@ import (
func UploadFile(bucketName, endpoint, fileName string) error {
ctx := context.Background()
accessKeyID, secretAccessKey, err := getCredentials()
accessKeyID, secretAccessKey, err := helpers.GetCredentials()
if err != nil {
return (err)
}
client, err := createClient(accessKeyID, secretAccessKey, endpoint)
client, err := helpers.CreateClient(accessKeyID, secretAccessKey, endpoint)
if err != nil {
return (err)
}
err = verifyBucket(client, ctx, bucketName)
err = helpers.VerifyBucket(client, ctx, bucketName)
if err != nil {
return err
}
@@ -51,42 +50,3 @@ func validateUploadFile(fileName string) (string, string, error) {
return fileName, file.Name(), nil
}
func verifyBucket(client *minio.Client, ctx context.Context, bucketName string) error {
exists, err := client.BucketExists(ctx, bucketName)
if err != nil {
return err
}
if !exists {
return errors.New(fmt.Sprintf("bucket %s does not exist\n", bucketName))
}
return nil
}
func createClient(accessKeyID, secretAccessKey, endpoint string) (*minio.Client, error) {
// Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: true,
})
if err != nil {
return nil, err
}
return minioClient, nil
}
func getCredentials() (string, string, error) {
// get credentials from env vars
keyName := os.Getenv("KEY_ID")
if keyName == "" {
return "", "", errors.New("missing or empty KEY_ID")
}
applicationKey := os.Getenv("APPLICATION_KEY")
if applicationKey == "" {
return "", "", errors.New("missing or empty APPLICATION_KEY")
}
return keyName, applicationKey, nil
}