-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] System backups (uploads, system.db, analytics.db) (#42)
- Loading branch information
Showing
8 changed files
with
275 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package upload | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
// Backup creates an archive of a project's uploads and writes it | ||
// to the response as a download | ||
func Backup(res http.ResponseWriter) error { | ||
ts := time.Now().Unix() | ||
filename := fmt.Sprintf("uploads-%d.bak.tar.gz", ts) | ||
tmp := os.TempDir() | ||
backup := filepath.Join(tmp, filename) | ||
|
||
// create uploads-{stamp}.bak.tar.gz | ||
f, err := os.Create(backup) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// loop through directory and gzip files | ||
// add all to uploads.bak.tar.gz tarball | ||
gz := gzip.NewWriter(f) | ||
tarball := tar.NewWriter(gz) | ||
|
||
err = filepath.Walk("uploads", func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hdr, err := tar.FileInfoHeader(info, "") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hdr.Name = path | ||
|
||
err = tarball.WriteHeader(hdr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !info.IsDir() { | ||
src, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer src.Close() | ||
_, err = io.Copy(tarball, src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = tarball.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = gz.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
return err | ||
} | ||
|
||
err = gz.Close() | ||
if err != nil { | ||
return err | ||
} | ||
err = tarball.Close() | ||
if err != nil { | ||
return err | ||
} | ||
err = f.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// write data to response | ||
data, err := os.Open(backup) | ||
if err != nil { | ||
return err | ||
} | ||
defer data.Close() | ||
defer os.Remove(backup) | ||
|
||
disposition := `attachment; filename=%s` | ||
info, err := data.Stat() | ||
if err != nil { | ||
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", info.Size())) | ||
|
||
_, err = io.Copy(res, data) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package analytics | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/boltdb/bolt" | ||
) | ||
|
||
// Backup writes a snapshot of the analytics.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="analytics-%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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package system | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ponzu-cms/ponzu/system/db" | ||
) | ||
|
||
// BasicAuth adds HTTP Basic Auth check for requests that should implement it | ||
func BasicAuth(next http.HandlerFunc) http.HandlerFunc { | ||
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
u := db.ConfigCache("backup_basic_auth_user").(string) | ||
p := db.ConfigCache("backup_basic_auth_password").(string) | ||
|
||
if u == "" || p == "" { | ||
res.WriteHeader(http.StatusForbidden) | ||
return | ||
} | ||
|
||
user, password, ok := req.BasicAuth() | ||
|
||
if !ok { | ||
res.WriteHeader(http.StatusForbidden) | ||
return | ||
} | ||
|
||
if u != user || p != password { | ||
res.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
next.ServeHTTP(res, req) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |