diff --git a/internal/component/backup.go b/internal/component/backup.go index 027303d..3a1c969 100644 --- a/internal/component/backup.go +++ b/internal/component/backup.go @@ -3,6 +3,7 @@ package component import ( "io" + "github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/tkw1536/goprogram/stream" ) @@ -14,11 +15,11 @@ type Backupable interface { BackupName() string // Backup backs up this component into the destination path path - Backup(context BackupContext) error + Backup(context StagingContext) error } -// BackupContext is the context for backups -type BackupContext interface { +// StagingContext represents a context for [Backupable] and [Snapshotable] +type StagingContext interface { // IO returns the input output stream belonging to this backup file IO() stream.IOStream @@ -41,3 +42,17 @@ type BackupContext interface { // AddFile will not return before op has returned. AddFile(path string, op func(file io.Writer) error) error } + +// Snapshotable represents a component with a Snapshot method. +type Snapshotable interface { + Component + + // SnapshotNeedsRunning returns if this Snapshotable requires a running instance. + SnapshotNeedsRunning() bool + + // SnapshotName returns a new name to be used as an argument for path. + SnapshotName() string + + // Snapshot snapshots a part of the instance + Snapshot(wisski models.Instance, context StagingContext) error +} diff --git a/internal/component/control/backup.go b/internal/component/control/backup.go index 4e3b0e7..3bad507 100644 --- a/internal/component/control/backup.go +++ b/internal/component/control/backup.go @@ -11,7 +11,7 @@ func (*Control) BackupName() string { } // Backup backups all control plane configuration files into dest -func (control *Control) Backup(context component.BackupContext) error { +func (control *Control) Backup(context component.StagingContext) error { files := control.backupFiles() return context.AddDirectory("", func() error { diff --git a/internal/component/snapshots/snapshot_make.go b/internal/component/snapshots/snapshot_make.go index 6eda8a8..ff301e7 100644 --- a/internal/component/snapshots/snapshot_make.go +++ b/internal/component/snapshots/snapshot_make.go @@ -212,7 +212,7 @@ func (snapshot *Snapshot) makeBlackbox(io stream.IOStream, snapshots *Manager, i defer nquads.Close() // directly store the result - _, err = snapshots.TS.Snapshot(nquads, instance.GraphDBRepository) + _, err = snapshots.TS.SnapshotDB(nquads, instance.GraphDBRepository) return err }, &snapshot.ErrTriplestore) @@ -233,7 +233,7 @@ func (snapshot *Snapshot) makeBlackbox(io stream.IOStream, snapshots *Manager, i defer sql.Close() // directly store the result - return snapshots.SQL.Snapshot(io, sql, instance.SqlDatabase) + return snapshots.SQL.SnapshotDB(io, sql, instance.SqlDatabase) }, &snapshot.ErrSQL) // wait for the group! diff --git a/internal/component/sql/backup.go b/internal/component/sql/backup.go index b50ed1e..94760ad 100644 --- a/internal/component/sql/backup.go +++ b/internal/component/sql/backup.go @@ -14,7 +14,7 @@ func (*SQL) BackupName() string { } // Backup makes a backup of all SQL databases into the path dest. -func (sql *SQL) Backup(context component.BackupContext) error { +func (sql *SQL) Backup(context component.StagingContext) error { return context.AddFile("", func(file io.Writer) error { io := context.IO().Streams(file, nil, nil, 0).NonInteractive() code, err := sql.Stack(sql.Environment).Exec(io, "sql", "mysqldump", "--all-databases") diff --git a/internal/component/sql/snapshot.go b/internal/component/sql/snapshot.go index 3a73f83..e70e03c 100644 --- a/internal/component/sql/snapshot.go +++ b/internal/component/sql/snapshot.go @@ -6,8 +6,8 @@ import ( "github.com/tkw1536/goprogram/stream" ) -// Snapshot makes a backup of the sql database into dest. -func (sql SQL) Snapshot(io stream.IOStream, dest io.Writer, database string) error { +// SnapshotDB makes a backup of the sql database into dest. +func (sql SQL) SnapshotDB(io stream.IOStream, dest io.Writer, database string) error { io = io.Streams(dest, nil, nil, 0).NonInteractive() code, err := sql.Stack(sql.Environment).Exec(io, "sql", "mysqldump", "--databases", database) diff --git a/internal/component/triplestore/backup.go b/internal/component/triplestore/backup.go index b28f027..a1a03ed 100644 --- a/internal/component/triplestore/backup.go +++ b/internal/component/triplestore/backup.go @@ -10,7 +10,7 @@ import ( func (ts *Triplestore) BackupName() string { return "triplestore" } // Backup makes a backup of all Triplestore repositories databases into the path dest. -func (ts *Triplestore) Backup(context component.BackupContext) error { +func (ts *Triplestore) Backup(context component.StagingContext) error { // list all the directories repos, err := ts.listRepositories() @@ -22,7 +22,7 @@ func (ts *Triplestore) Backup(context component.BackupContext) error { return context.AddDirectory("", func() error { for _, repo := range repos { if err := context.AddFile(repo.ID+".nq", func(file io.Writer) error { - _, err := ts.Snapshot(file, repo.ID) + _, err := ts.SnapshotDB(file, repo.ID) return err }); err != nil { return err diff --git a/internal/component/triplestore/database.go b/internal/component/triplestore/database.go index 1bc52d0..8fd4542 100644 --- a/internal/component/triplestore/database.go +++ b/internal/component/triplestore/database.go @@ -124,8 +124,8 @@ func (ts Triplestore) PurgeRepo(repo string) error { var errTSBackupWrongStatusCode = errors.New("Distillery.Backup: Wrong status code") -// Snapshot snapshots the provided repository into dst -func (ts Triplestore) Snapshot(dst io.Writer, repo string) (int64, error) { +// SnapshotDB snapshots the provided repository into dst +func (ts Triplestore) SnapshotDB(dst io.Writer, repo string) (int64, error) { res, err := ts.OpenRaw("GET", "/repositories/"+repo+"/statements?infer=false", nil, "", "application/n-quads") if err != nil { return 0, err