backups: restore local backups in-app

backups
Harvey Tindall 5 months ago
parent ff73c72b0e
commit eff313be41
No known key found for this signature in database
GPG Key ID: BBC65952848FB1A2

@ -565,7 +565,7 @@ func (app *appContext) CreateBackup(gc *gin.Context) {
// @Produce octet-stream
// @Produce json
// @Success 200 {body} file
// @Success 400 {object} boolResponse
// @Failure 400 {object} boolResponse
// @Security Bearer
// @tags Other
func (app *appContext) GetBackup(gc *gin.Context) {
@ -608,3 +608,27 @@ func (app *appContext) GetBackups(gc *gin.Context) {
}
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)
}

@ -207,7 +207,7 @@ func (app *appContext) loadPendingBackup() {
if LOADBAK == "" {
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)
err := os.Rename(app.storage.db_path, oldPath)
if err != nil {

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

@ -797,6 +797,12 @@ export class settingsList {
window.notifications.customPositive("pathCopied", "", window.lang.notif("pathCopied"));
});
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);
}
});

Loading…
Cancel
Save