New POST /backup/download (admin-only) tars the whole jsondb directory (all servers/clients/users/settings) and streams it back as a file download. If a passphrase is given, the archive is encrypted first (AES-256-GCM, scrypt-derived key, backup/encrypt.go) - a small self-contained format, not gpg/OpenPGP-compatible, to avoid shelling out to an external binary or adding a PGP dependency. Deliberately does NOT upload anywhere automatically (e.g. to Nextcloud) - the archive only ever leaves the server as this one HTTP response to the requesting admin, who is responsible for storing it themselves. New "Download Backup" button + passphrase modal on the Global Settings page.
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
// Package backup builds a downloadable, optionally encrypted snapshot of
|
|
// the jsondb directory (servers, clients, users, settings - everything
|
|
// needed to restore this installation). It never transmits data anywhere
|
|
// on its own; the archive is only ever returned to an authenticated admin
|
|
// as an HTTP download, never uploaded automatically.
|
|
package backup
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// BuildArchive tars+gzips every regular file under dbPath into memory,
|
|
// preserving paths relative to dbPath so restoring means extracting into a
|
|
// fresh, empty db directory.
|
|
func BuildArchive(dbPath string) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
gzw := gzip.NewWriter(&buf)
|
|
tw := tar.NewWriter(gzw)
|
|
|
|
err := filepath.Walk(dbPath, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
relPath, err := filepath.Rel(dbPath, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
header := &tar.Header{
|
|
Name: relPath,
|
|
Mode: int64(info.Mode().Perm()),
|
|
Size: int64(len(data)),
|
|
ModTime: info.ModTime(),
|
|
}
|
|
if err := tw.WriteHeader(header); err != nil {
|
|
return err
|
|
}
|
|
_, err = tw.Write(data)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := tw.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := gzw.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|