summaryrefslogtreecommitdiff
path: root/system/db/backup.go
diff options
context:
space:
mode:
Diffstat (limited to 'system/db/backup.go')
-rw-r--r--system/db/backup.go35
1 files changed, 23 insertions, 12 deletions
diff --git a/system/db/backup.go b/system/db/backup.go
index 735abe4..4a2bf96 100644
--- a/system/db/backup.go
+++ b/system/db/backup.go
@@ -1,6 +1,7 @@
package db
import (
+ "context"
"fmt"
"net/http"
"time"
@@ -8,19 +9,29 @@ import (
"github.com/boltdb/bolt"
)
-// Backup writes a snapshot of the system.db database to an HTTP response
-func Backup(res http.ResponseWriter) error {
- err := store.View(func(tx *bolt.Tx) error {
- ts := time.Now().Unix()
- disposition := `attachment; filename="system-%d.db.bak"`
+// Backup writes a snapshot of the system.db database to an HTTP response. The
+// output is discarded if we get a cancellation signal.
+func Backup(ctx context.Context, res http.ResponseWriter) error {
+ errChan := make(chan error, 1)
- res.Header().Set("Content-Type", "application/octet-stream")
- res.Header().Set("Content-Disposition", fmt.Sprintf(disposition, ts))
- res.Header().Set("Content-Length", fmt.Sprintf("%d", int(tx.Size())))
+ go func() {
+ errChan <- store.View(func(tx *bolt.Tx) error {
+ ts := time.Now().Unix()
+ disposition := `attachment; filename="system-%d.db.bak"`
- _, err := tx.WriteTo(res)
- return err
- })
+ res.Header().Set("Content-Type", "application/octet-stream")
+ res.Header().Set("Content-Disposition", fmt.Sprintf(disposition, ts))
+ res.Header().Set("Content-Length", fmt.Sprintf("%d", int(tx.Size())))
+
+ _, err := tx.WriteTo(res)
+ return err
+ })
+ }()
- return err
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case err := <-errChan:
+ return err
+ }
}