added backup delete functionality

This commit is contained in:
BroodjeAap 2023-01-30 20:32:21 +00:00
parent e0ace15709
commit 0e98384b96
3 changed files with 67 additions and 4 deletions

55
main.go
View file

@ -199,6 +199,7 @@ func (web *Web) initRouter() {
web.router.GET("/backup/create", web.backupCreate)
web.router.POST("/backup/test", web.backupTest)
web.router.POST("/backup/restore", web.backupRestore)
web.router.POST("/backup/delete", web.backupDelete)
web.router.SetTrustedProxies(nil)
}
@ -1157,6 +1158,60 @@ func (web *Web) backupRestore(c *gin.Context) {
})
}
// backupRestore (/backup/restore/:id) either restores the filesInBackupDir[id] file or from an uploaded file
func (web *Web) backupDelete(c *gin.Context) {
importID, err := strconv.Atoi(c.PostForm("id"))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if importID <= 0 {
c.Redirect(http.StatusSeeOther, "/backup/view")
return
}
if !viper.IsSet("database.backup") {
c.HTML(http.StatusOK, "backupView", gin.H{"Error": "database.backup not set"})
return
}
if !viper.IsSet("database.backup.schedule") {
c.HTML(http.StatusOK, "backupView", gin.H{"Error": "database.backup.schedule not set"})
return
}
if !viper.IsSet("database.backup.path") {
c.HTML(http.StatusOK, "backupView", gin.H{"Error": "database.backup.path not set"})
return
}
backupPath := viper.GetString("database.backup.path")
backupDir, err := filepath.Abs(filepath.Dir(backupPath))
if err != nil {
c.HTML(http.StatusOK, "backupView", gin.H{"Error": err})
return
}
filesInBackupDir, err := ioutil.ReadDir(backupDir)
if err != nil {
c.HTML(http.StatusOK, "backupView", gin.H{"Error": err})
return
}
if importID >= len(filesInBackupDir) {
c.HTML(http.StatusOK, "backupView", gin.H{"Error": err})
return
}
backupFileName := filesInBackupDir[importID]
backupFullPath := filepath.Join(backupDir, backupFileName.Name())
err = os.Remove(backupFullPath)
if err != nil {
c.HTML(http.StatusOK, "backupView", gin.H{"Error": err})
return
}
c.Redirect(http.StatusSeeOther, "/backup/view")
}
// exportWatch (/watch/export/:id) creates a json export of the current watch
func (web *Web) exportWatch(c *gin.Context) {
watchID := c.Param("id")

View file

@ -23,6 +23,7 @@ GoWatch Backups
<th>File</th>
<th>Test</th>
<th>Restore</th>
<th>Delete</th>
<th>Download</th>
</tr>
</thead>
@ -38,9 +39,10 @@ GoWatch Backups
<input id="testSubmit" type="submit" class="btn btn-success" value="Test">
</td>
<td>
<input id="restoreSubmit" type="submit" class="btn btn-danger" value="Restore">
<input id="restoreSubmit" type="submit" class="btn btn-warning" value="Restore">
</td>
<td></td>
<td><button class="btn btn-danger" disabled>Delete</button></td>
<td><button class="btn btn-secondary" disabled>Download</button></td>
</tr>
{{ range $i, $backup := .Backups }}
<tr>
@ -54,7 +56,13 @@ GoWatch Backups
<td>
<form action="/backup/restore" enctype="multipart/form-data" method="POST">
<input type="hidden" value="{{ $i }}" name="id">
<button class="btn btn-danger">Restore</button>
<button class="btn btn-warning">Restore</button>
</form>
</td>
<td>
<form action="/backup/delete" enctype="multipart/form-data" method="POST">
<input type="hidden" value="{{ $i }}" name="id">
<button class="btn btn-danger">Delete</button>
</form>
</td>
<td>

View file

@ -8,7 +8,7 @@
- ~~scheduled backup~~
- ~~test~~
- ~~restore~~
- delete
- ~~delete~~
- download
- ~~restore from upload~~
- browserless function filters