Rename backups => snapshot

This commit is contained in:
Tom Wiesing 2022-09-07 11:02:50 +02:00
parent 611cbeebb9
commit d818cb93a5
No known key found for this signature in database
8 changed files with 178 additions and 166 deletions

52
env/backup.go vendored
View file

@ -1,52 +0,0 @@
package env
import (
"fmt"
"os"
"path/filepath"
"sync/atomic"
"time"
)
func (dis Distillery) BackupDir() string {
return filepath.Join(dis.Config.DeployRoot, "backups")
}
func (dis Distillery) InprogressBackupPath() string {
return filepath.Join(dis.BackupDir(), "inprogress")
}
func (dis Distillery) FinalBackupPath() string {
return filepath.Join(dis.BackupDir(), "final")
}
// NewFinalBackupFile returns the path to a new final backup file.
func (dis Distillery) FinalBackupArchive(prefix string) string {
counter := atomic.AddUint64(&globalBackupCounter, 1)
// generate a new name with the current time, a global counter, and the prefix
name := fmt.Sprintf("%s-%d-%d.tar.gz", prefix, time.Now().Unix(), counter)
path := filepath.Join(dis.FinalBackupPath(), name)
return path
}
var globalBackupCounter uint64
// NewInprogressBackupPath returns the path to a new inprogress backup directory.
// The directory is guaranteed to have been freshly created.
func (dis Distillery) NewInprogressBackupPath(prefix string) (string, error) {
counter := atomic.AddUint64(&globalBackupCounter, 1)
// generate a new name with the current time, a global counter, and the prefix
name := fmt.Sprintf("%s-%d-%d", prefix, time.Now().Unix(), counter)
path := filepath.Join(dis.InprogressBackupPath(), name)
// create the directory
if err := os.Mkdir(path, os.ModeDir); err != nil {
return "", err
}
// and it is here!
return path, nil
}

50
env/snapshot.go vendored
View file

@ -6,12 +6,62 @@ import (
"os"
"path/filepath"
"sync"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/fsx"
"github.com/FAU-CDI/wisski-distillery/internal/logging"
"github.com/FAU-CDI/wisski-distillery/internal/password"
"github.com/tkw1536/goprogram/stream"
)
// SnapshotsDir returns the path that contains all snapshot related data.
func (dis Distillery) SnapshotsDir() string {
return filepath.Join(dis.Config.DeployRoot, "snapshots")
}
// SnapshotsStagingPath returns the path to the directory containing a temporary staging area for snapshots.
// Use NewSnapshotStagingDir to generate a new staging area.
func (dis Distillery) SnapshotsStagingPath() string {
return filepath.Join(dis.SnapshotsDir(), "staging")
}
// SnapshotsArchivePath returns the path to the directory containing all exported archives.
// Use NewSnapshotArchivePath to generate a path to a new archive in this directory.
func (dis Distillery) SnapshotsArchivePath() string {
return filepath.Join(dis.SnapshotsDir(), "archives")
}
// NewSnapshotArchivePath returns the path to a new archive with the provided prefix.
// The path is guaranteed to not exist.
func (dis Distillery) NewSnapshotArchivePath(prefix string) (path string) {
// TODO: Consider moving these into a subdirectory with the provided prefix.
for path == "" || fsx.Exists(path) {
name := dis.newSnapshotName(prefix) + ".tar.gz"
path = filepath.Join(dis.SnapshotsArchivePath(), name)
}
return
}
// newSnapshot name returns a new basename for a snapshot with the provided prefix.
// The name is guaranteed to be unique within this process.
func (Distillery) newSnapshotName(prefix string) string {
suffix, _ := password.Password(64) // silently ignore any errors!
return fmt.Sprintf("%s-%d-%s", prefix, time.Now().Unix(), suffix)
}
// NewSnapshotStagingDir returns the path to a new snapshot directory.
// The directory is guaranteed to have been freshly created.
func (dis Distillery) NewSnapshotStagingDir(prefix string) (path string, err error) {
for path == "" || os.IsExist(err) {
path = filepath.Join(dis.SnapshotsStagingPath(), dis.newSnapshotName(prefix))
err = os.Mkdir(path, os.ModeDir)
}
if err != nil {
path = ""
}
return
}
type SnapshotReport struct {
Keepalive bool // was the instance alive while running the snapshot?
Panic interface{} // was there a panic?