2023-05-06 07:14:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-06 08:52:30 +00:00
|
|
|
"errors"
|
2023-05-06 07:14:37 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.wntrmute.dev/kyle/goutils/config"
|
2023-08-29 19:03:43 +00:00
|
|
|
"git.wntrmute.dev/kyle/goutils/log"
|
2023-05-06 07:14:37 +00:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
)
|
|
|
|
|
|
|
|
var minioClient *minio.Client
|
|
|
|
|
|
|
|
func connectMinio() (err error) {
|
|
|
|
minioClient, err = minio.New(config.Get("minio_host"), &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(
|
|
|
|
config.Get("minio_access_key"),
|
|
|
|
config.Get("minio_secret_key"), ""),
|
|
|
|
Secure: true,
|
|
|
|
})
|
|
|
|
|
2023-05-06 08:52:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errf("while connecting minio: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:14:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreDatabase(path string) error {
|
2023-08-29 20:23:38 +00:00
|
|
|
log.Debugln("restoring database from minio")
|
2023-05-06 08:52:30 +00:00
|
|
|
if minioClient == nil {
|
|
|
|
if err := connectMinio(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if minioClient == nil {
|
|
|
|
return errors.New("storage: minio client is not connected")
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:14:37 +00:00
|
|
|
obj, err := minioClient.GetObject(
|
|
|
|
context.Background(),
|
|
|
|
config.Get("minio_bucket"),
|
|
|
|
"nlink.db",
|
|
|
|
minio.GetObjectOptions{},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbFile, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dbFile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(dbFile, obj)
|
2023-08-29 20:15:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugln("database restored from minio")
|
|
|
|
return nil
|
2023-05-06 07:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveDatabase(path string) error {
|
2023-05-06 08:52:30 +00:00
|
|
|
if minioClient == nil {
|
|
|
|
if err := connectMinio(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:14:37 +00:00
|
|
|
dbFile, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dbFile.Close()
|
|
|
|
|
|
|
|
fi, err := dbFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = minioClient.PutObject(
|
|
|
|
context.Background(),
|
|
|
|
config.Get("minio_bucket"),
|
|
|
|
"nlink.db",
|
|
|
|
dbFile,
|
|
|
|
fi.Size(),
|
|
|
|
minio.PutObjectOptions{ContentType: "application/octet-stream"},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-29 20:15:50 +00:00
|
|
|
|
|
|
|
log.Debugln("database saved to minio")
|
2023-05-06 07:14:37 +00:00
|
|
|
return nil
|
|
|
|
}
|