2021-09-14 20:59:51 +00:00
|
|
|
package backup
|
2019-10-03 09:29:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-10-20 16:45:33 +00:00
|
|
|
// Exporter defines a backup exporter methods.
|
|
|
|
type Exporter interface {
|
2021-06-18 01:37:58 +00:00
|
|
|
Backup(ctx context.Context, outputPath string, permissionOverride bool) error
|
2020-12-03 22:28:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 16:45:33 +00:00
|
|
|
// Handler for accepting requests to initiate a new database backup.
|
|
|
|
func Handler(bk Exporter, outputDir string) func(http.ResponseWriter, *http.Request) {
|
2019-10-03 09:29:49 +00:00
|
|
|
log := logrus.WithField("prefix", "db")
|
|
|
|
|
2021-06-18 01:37:58 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-12-03 22:28:57 +00:00
|
|
|
log.Debug("Creating database backup from HTTP webhook")
|
2019-10-03 09:29:49 +00:00
|
|
|
|
2021-06-18 01:37:58 +00:00
|
|
|
_, permissionOverride := r.URL.Query()["permissionOverride"]
|
|
|
|
|
|
|
|
if err := bk.Backup(context.Background(), outputDir, permissionOverride); err != nil {
|
2019-10-03 09:29:49 +00:00
|
|
|
log.WithError(err).Error("Failed to create backup")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, err := fmt.Fprint(w, "OK")
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to write OK")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|