Compare commits
3 Commits
cab35ddfdd
...
refactor-u
| Author | SHA1 | Date | |
|---|---|---|---|
| bd3a0e7a8a | |||
| 14d2165daf | |||
| a0081fab99 |
127
main.go
127
main.go
@@ -1,90 +1,73 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"backup-tool/s3helper"
|
||||||
"context"
|
"context"
|
||||||
"github.com/joho/godotenv"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/joho/godotenv"
|
||||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ENDPOINT = "s3.us-west-004.backblazeb2.com"
|
|
||||||
BUCKETNAME = "ducimon-db-backups"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
accessKeyID, secretAccessKey := getCredentials()
|
|
||||||
client := createClient(accessKeyID, secretAccessKey)
|
|
||||||
verifyBucket(client, ctx)
|
|
||||||
absolutePath, basename := validateUploadFile()
|
|
||||||
|
|
||||||
_, err := client.FPutObject(ctx, BUCKETNAME, "test", absolutePath, minio.PutObjectOptions{})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("upload of %s complete\n", basename)
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateUploadFile() (string, string) {
|
|
||||||
if len(os.Args) < 2 {
|
|
||||||
log.Fatalln("upload file not specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Stat(os.Args[1])
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if file.IsDir() {
|
|
||||||
log.Fatalln("upload of directories is not supported")
|
|
||||||
}
|
|
||||||
|
|
||||||
return os.Args[1], file.Name()
|
|
||||||
}
|
|
||||||
|
|
||||||
func verifyBucket(client *minio.Client, ctx context.Context) {
|
|
||||||
exists, err := client.BucketExists(ctx, BUCKETNAME)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !exists {
|
|
||||||
log.Fatalf("bucket %s does not exist\n", BUCKETNAME)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func createClient(accessKeyID string, secretAccessKey string) *minio.Client {
|
|
||||||
// Initialize minio client object.
|
|
||||||
minioClient, err := minio.New(ENDPOINT, &minio.Options{
|
|
||||||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
|
||||||
Secure: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return minioClient
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCredentials() (string, string) {
|
|
||||||
// get credentials from env vars
|
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error loading .env file: (%s)", err.Error())
|
log.Fatalf("error loading .env file: (%s)", err.Error())
|
||||||
}
|
}
|
||||||
keyName := os.Getenv("KEY_ID")
|
|
||||||
if keyName == "" {
|
path, err := exec.LookPath("restic")
|
||||||
log.Fatal("missing or empty KEY_ID")
|
if err != nil {
|
||||||
|
log.Fatalf("could not find restic in PATH %s", err)
|
||||||
}
|
}
|
||||||
applicationKey := os.Getenv("APPLICATION_KEY")
|
|
||||||
if applicationKey == "" {
|
cmd := exec.Command(path, "backup", "--dry-run", "/media/piwigo")
|
||||||
log.Fatal("missing or empty APPLICATION_KEY")
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error connecting to stdout: %q: %s", cmd.String(), err)
|
||||||
}
|
}
|
||||||
return keyName, applicationKey
|
|
||||||
|
err = cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error running command: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = exec.Command(path, "forget", "--dry-run", "--keep-last", "7")
|
||||||
|
stdout, err = cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error connecting to stdout: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error running command %q: %s", cmd.String(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Print(stdout)
|
||||||
|
|
||||||
|
if len(os.Args) > 1 {
|
||||||
|
// only do this if we have been given a file to upload (for now)
|
||||||
|
// TODO: have this tool take the backup of the DB itself
|
||||||
|
b2Creds, err := getCredentials([]string{"B2_KEY_ID", "B2_APPLICATION_KEY"})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("could not get B2 credentials: %s", err)
|
||||||
|
}
|
||||||
|
s3helper.UploadFile(context.Background(), b2Creds)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCredentials(credNames []string) (map[string]string, error) {
|
||||||
|
creds := map[string]string{}
|
||||||
|
for _, name := range credNames {
|
||||||
|
value := os.Getenv(name)
|
||||||
|
if value == "" {
|
||||||
|
return nil, fmt.Errorf("missing or empty ENV var: %s", name)
|
||||||
|
}
|
||||||
|
creds[name] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return creds, nil
|
||||||
}
|
}
|
||||||
|
|||||||
72
s3helper/s3helper.go
Normal file
72
s3helper/s3helper.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package s3helper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/minio/minio-go/v7"
|
||||||
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ENDPOINT = "s3.us-west-004.backblazeb2.com"
|
||||||
|
BUCKETNAME = "ducimon-db-backups"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UploadFile(ctx context.Context, b2Creds map[string]string) error {
|
||||||
|
|
||||||
|
client := createClient(b2Creds["B2_KEY_ID"], b2Creds["B2_APPLICATION_KEY"])
|
||||||
|
verifyBucket(client, ctx)
|
||||||
|
absolutePath, basename := validateUploadFile()
|
||||||
|
_, err := client.FPutObject(ctx, BUCKETNAME, "test", absolutePath, minio.PutObjectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("upload of %s complete\n", basename)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createClient(accessKeyID string, secretAccessKey string) *minio.Client {
|
||||||
|
// Initialize minio client object.
|
||||||
|
minioClient, err := minio.New(ENDPOINT, &minio.Options{
|
||||||
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
||||||
|
Secure: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return minioClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func verifyBucket(client *minio.Client, ctx context.Context) {
|
||||||
|
exists, err := client.BucketExists(ctx, BUCKETNAME)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
log.Fatalf("bucket %s does not exist\n", BUCKETNAME)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateUploadFile() (string, string) {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
log.Fatalln("upload file not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Stat(os.Args[1])
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if file.IsDir() {
|
||||||
|
log.Fatalln("upload of directories is not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.Args[1], file.Name()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user