{backup,snapshot}: Log and display in control
This commit is contained in:
parent
3b112f1b8e
commit
630da9e12f
17 changed files with 294 additions and 44 deletions
|
|
@ -44,7 +44,6 @@ type Backup struct {
|
|||
// BackupDescription provides a description for a backup
|
||||
type BackupDescription struct {
|
||||
Dest string // Destination path
|
||||
Auto bool // Was the path created automatically?
|
||||
|
||||
ConcurrentSnapshots int // maximum number of concurrent snapshots
|
||||
}
|
||||
|
|
|
|||
17
internal/component/snapshots/log.go
Normal file
17
internal/component/snapshots/log.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package snapshots
|
||||
|
||||
import "github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
|
||||
func (backup *Backup) LogEntry() models.Snapshot {
|
||||
return models.Snapshot{
|
||||
Created: backup.StartTime,
|
||||
Slug: "",
|
||||
}
|
||||
}
|
||||
|
||||
func (snapshot *Snapshot) LogEntry() models.Snapshot {
|
||||
return models.Snapshot{
|
||||
Created: snapshot.StartTime,
|
||||
Slug: snapshot.Instance.Slug,
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/component/instances"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/component/sql"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/password"
|
||||
|
|
@ -15,6 +16,8 @@ import (
|
|||
// Manager manages snapshots and backups
|
||||
type Manager struct {
|
||||
component.ComponentBase
|
||||
|
||||
SQL *sql.SQL
|
||||
Instances *instances.Instances
|
||||
|
||||
Snapshotable []component.Snapshotable
|
||||
|
|
@ -54,7 +57,7 @@ func (dis *Manager) NewArchivePath(prefix string) (path string) {
|
|||
// newSnapshot name returns a new basename for a snapshot with the provided prefix.
|
||||
// The name is guaranteed to be unique within this process.
|
||||
func (*Manager) newSnapshotName(prefix string) string {
|
||||
suffix, _ := password.Password(64) // silently ignore any errors!
|
||||
suffix, _ := password.Password(10) // silently ignore any errors!
|
||||
if prefix == "" {
|
||||
prefix = "backup"
|
||||
} else {
|
||||
|
|
@ -68,7 +71,6 @@ func (*Manager) newSnapshotName(prefix string) string {
|
|||
func (dis *Manager) NewStagingDir(prefix string) (path string, err error) {
|
||||
for path == "" || environment.IsExist(err) {
|
||||
path = filepath.Join(dis.StagingPath(), dis.newSnapshotName(prefix))
|
||||
fmt.Println("path =>", prefix, "err => ", err)
|
||||
err = dis.Core.Environment.Mkdir(path, environment.DefaultFilePerm)
|
||||
}
|
||||
if err != nil {
|
||||
|
|
|
|||
54
internal/component/snapshots/prune.go
Normal file
54
internal/component/snapshots/prune.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package snapshots
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
// ShouldPrune determines if a file with the provided modtime
|
||||
func (manager *Manager) ShouldPrune(modtime time.Time) bool {
|
||||
return time.Since(modtime) > time.Duration(manager.Config.MaxBackupAge)*24*time.Hour
|
||||
}
|
||||
|
||||
// Prune prunes all backups and snapshots older than the maximum backup age
|
||||
func (manager *Manager) PruneBackups(io stream.IOStream) error {
|
||||
sPath := manager.ArchivePath()
|
||||
|
||||
// list all the files
|
||||
entries, err := manager.Core.Environment.ReadDir(sPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
// skip directories
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
// grab info about the file
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// check if it should be pruned!
|
||||
if !manager.ShouldPrune(info.ModTime()) {
|
||||
continue
|
||||
}
|
||||
|
||||
// assemble path, and then remove the file!
|
||||
path := filepath.Join(sPath, entry.Name())
|
||||
io.Printf("Removing %s cause it is older than %d days", path, manager.Config.MaxBackupAge)
|
||||
|
||||
if err := manager.Core.Environment.Remove(path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// prune the snapshot log!
|
||||
_, err = manager.Instances.SnapshotLog()
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue