feat: add dumpfiles info in web console
This commit is contained in:
@@ -1,16 +1,47 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"ddbb/pkg/backup"
|
||||
"ddbb/pkg/docker"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ServeBackupPage(c *gin.Context) {
|
||||
allBackups, _ := docker.GetBackupConfigsFromContainers()
|
||||
setBackupDumpFiles(allBackups, "/backups/")
|
||||
|
||||
c.HTML(http.StatusOK, "backups.tmpl", gin.H{
|
||||
"Backups": allBackups,
|
||||
})
|
||||
}
|
||||
|
||||
func setBackupDumpFiles(allBackups []backup.Config, dumpDir string) error {
|
||||
for i := range allBackups {
|
||||
host := allBackups[i].Host
|
||||
// Try to find a dump file matching host.* in dumpDir
|
||||
pattern := filepath.Join(dumpDir, host+".*")
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
continue // no dump found for this host
|
||||
}
|
||||
dumpFile := matches[0] // take first match
|
||||
|
||||
fi, err := os.Stat(dumpFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
modTime := fi.ModTime().Format(time.RFC822)
|
||||
allBackups[i].DumpFile = dumpFile
|
||||
allBackups[i].DumpFileModTime = modTime
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,13 +8,15 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Id string
|
||||
Kind string
|
||||
Host string
|
||||
Port string
|
||||
Database string
|
||||
User string
|
||||
Password string
|
||||
Id string
|
||||
Kind string
|
||||
Host string
|
||||
Port string
|
||||
Database string
|
||||
User string
|
||||
Password string
|
||||
DumpFile string
|
||||
DumpFileModTime string
|
||||
}
|
||||
|
||||
type ConfigList struct {
|
||||
|
||||
@@ -72,13 +72,11 @@ func restorePostgres(cfg backup.Config) error {
|
||||
}
|
||||
|
||||
func restoreMongo(cfg backup.Config) error {
|
||||
dumpFileName := dumFileRoot + cfg.Host + ".sql"
|
||||
dumpFileName := dumFileRoot + cfg.Host + ".gz"
|
||||
cmd := exec.Command(
|
||||
"mongorestore", "--gzip", "--drop", "--archive="+dumpFileName,
|
||||
"--host", cfg.Host,
|
||||
"--username", cfg.User,
|
||||
"--password", cfg.Password,
|
||||
"--db", cfg.Database,
|
||||
"mongorestore", "--archive", "--gzip", "--drop", "--archive="+dumpFileName,
|
||||
"--uri",
|
||||
fmt.Sprintf("mongodb://%s:%s@%s:%s/%s?authSource=admin", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database),
|
||||
)
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
@@ -134,6 +134,8 @@
|
||||
<th>Port</th>
|
||||
<th>Database</th>
|
||||
<th>User</th>
|
||||
<th>Dump file</th>
|
||||
<th>Dump date</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -146,6 +148,8 @@
|
||||
<td>{{.Port}}</td>
|
||||
<td>{{.Database}}</td>
|
||||
<td>{{.User}}</td>
|
||||
<td>{{.DumpFile}}</td>
|
||||
<td>{{.DumpFileModTime}}</td>
|
||||
<td>
|
||||
<form method="POST" action="/backup" style="display:inline;">
|
||||
<input type="hidden" name="id" value="{{.Id}}">
|
||||
|
||||
Reference in New Issue
Block a user