2020-12-03 22:28:57 +00:00
|
|
|
package backuputil
|
2019-10-03 09:29:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// BackupExporter defines a backup exporter methods.
|
2020-12-03 22:28:57 +00:00
|
|
|
type BackupExporter interface {
|
|
|
|
Backup(ctx context.Context, outputPath string) error
|
|
|
|
}
|
|
|
|
|
2019-10-03 09:29:49 +00:00
|
|
|
// BackupHandler for accepting requests to initiate a new database backup.
|
2020-12-03 22:28:57 +00:00
|
|
|
func BackupHandler(bk BackupExporter, outputDir string) func(http.ResponseWriter, *http.Request) {
|
2019-10-03 09:29:49 +00:00
|
|
|
log := logrus.WithField("prefix", "db")
|
|
|
|
|
|
|
|
return func(w http.ResponseWriter, _ *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
|
|
|
|
2020-12-03 22:28:57 +00:00
|
|
|
if err := bk.Backup(context.Background(), outputDir); 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|