1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

backups: restore local backups in-app

This commit is contained in:
Harvey Tindall 2023-12-21 17:42:07 +00:00
parent ff73c72b0e
commit eff313be41
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
4 changed files with 33 additions and 2 deletions

26
api.go
View File

@ -565,7 +565,7 @@ func (app *appContext) CreateBackup(gc *gin.Context) {
// @Produce octet-stream // @Produce octet-stream
// @Produce json // @Produce json
// @Success 200 {body} file // @Success 200 {body} file
// @Success 400 {object} boolResponse // @Failure 400 {object} boolResponse
// @Security Bearer // @Security Bearer
// @tags Other // @tags Other
func (app *appContext) GetBackup(gc *gin.Context) { func (app *appContext) GetBackup(gc *gin.Context) {
@ -608,3 +608,27 @@ func (app *appContext) GetBackups(gc *gin.Context) {
} }
gc.JSON(200, resp) gc.JSON(200, resp)
} }
// @Summary Restore a backup file stored locally to the server.
// @Param fname path string true "backup filename"
// @Router /backups/restore/{fname} [get]
// @Produce octet-stream
// @Produce json
// @Failure 400 {object} boolResponse
// @Security Bearer
// @tags Other
func (app *appContext) RestoreLocalBackup(gc *gin.Context) {
fname := gc.Param("fname")
// Hopefully this is enough to ensure the path isn't malicious. Hidden behind bearer auth anyway so shouldn't matter too much I guess.
ok := strings.HasPrefix(fname, BACKUP_PREFIX) && strings.HasSuffix(fname, BACKUP_SUFFIX)
t, err := time.Parse(BACKUP_DATEFMT, strings.TrimSuffix(strings.TrimPrefix(fname, BACKUP_PREFIX), BACKUP_SUFFIX))
if !ok || err != nil || t.IsZero() {
app.debug.Printf("Ignoring backup DL request due to fname: %v\n", err)
respondBool(400, false, gc)
return
}
path := app.config.Section("backups").Key("path").String()
fullpath := filepath.Join(path, fname)
LOADBAK = fullpath
app.restart(gc)
}

View File

@ -207,7 +207,7 @@ func (app *appContext) loadPendingBackup() {
if LOADBAK == "" { if LOADBAK == "" {
return return
} }
oldPath := filepath.Join(app.dataPath, "db-pre-"+filepath.Base(LOADBAK)) oldPath := filepath.Join(app.dataPath, "db-"+string(time.Now().Unix())+"-pre-"+filepath.Base(LOADBAK))
app.info.Printf("Moving existing database to \"%s\"\n", oldPath) app.info.Printf("Moving existing database to \"%s\"\n", oldPath)
err := os.Rename(app.storage.db_path, oldPath) err := os.Rename(app.storage.db_path, oldPath)
if err != nil { if err != nil {

View File

@ -211,6 +211,7 @@ func (app *appContext) loadRoutes(router *gin.Engine) {
api.POST(p+"/backups", app.CreateBackup) api.POST(p+"/backups", app.CreateBackup)
api.GET(p+"/backups/:fname", app.GetBackup) api.GET(p+"/backups/:fname", app.GetBackup)
api.GET(p+"/backups", app.GetBackups) api.GET(p+"/backups", app.GetBackups)
api.POST(p+"/backups/restore/:fname", app.RestoreLocalBackup)
if telegramEnabled || discordEnabled || matrixEnabled { if telegramEnabled || discordEnabled || matrixEnabled {
api.GET(p+"/telegram/pin", app.TelegramGetPin) api.GET(p+"/telegram/pin", app.TelegramGetPin)
api.GET(p+"/telegram/verified/:pin", app.TelegramVerified) api.GET(p+"/telegram/verified/:pin", app.TelegramVerified)

View File

@ -797,6 +797,12 @@ export class settingsList {
window.notifications.customPositive("pathCopied", "", window.lang.notif("pathCopied")); window.notifications.customPositive("pathCopied", "", window.lang.notif("pathCopied"));
}); });
tr.querySelector(".backup-download").addEventListener("click", () => _download("/backups/" + b.name, b.name)); tr.querySelector(".backup-download").addEventListener("click", () => _download("/backups/" + b.name, b.name));
tr.querySelector(".backup-restore").addEventListener("click", () => {
_post("/backups/restore/"+b.name, null, () => {});
window.modals.backups.close();
window.modals.settingsRefresh.modal.querySelector("span.heading").textContent = window.lang.strings("settingsRestarting");
window.modals.settingsRefresh.show();
});
table.appendChild(tr); table.appendChild(tr);
} }
}); });