summaryrefslogtreecommitdiff
path: root/system/db/backup.go
blob: 735abe4ace62edce5dd60c2fd12f5aadbf71092b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package db

import (
	"fmt"
	"net/http"
	"time"

	"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"`

		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
}