'wdcli backup': Rework backup process

This commit reworks the backup process to dynamically find the list of
components.
This commit is contained in:
Tom Wiesing 2022-09-17 16:30:32 +02:00
parent 55bee7422d
commit 5cd5ae9be2
No known key found for this signature in database
32 changed files with 361 additions and 279 deletions

View file

@ -0,0 +1,35 @@
package sql
import (
"errors"
"os"
"github.com/tkw1536/goprogram/stream"
)
var errSQLBackup = errors.New("SQLBackup: Mysqldump returned non-zero exit code")
func (*SQL) BackupName() string {
return "sql.sql"
}
// Backup makes a backup of all SQL databases into the path dest.
func (sql *SQL) Backup(io stream.IOStream, dest string) error {
// open the file, TODO: Outsource this to context
writer, err := os.Create(dest)
if err != nil {
return err
}
defer writer.Close()
// run sqldump
io = io.Streams(writer, nil, nil, 0).NonInteractive()
code, err := sql.Stack().Exec(io, "sql", "mysqldump", "--all-databases")
if err != nil {
return err
}
if code != 0 {
return errSQLBackup
}
return nil
}

View file

@ -59,10 +59,8 @@ func (sql SQL) OpenBookkeeping(silent bool) (*gorm.DB, error) {
return table, nil
}
var errSQLBackup = errors.New("SQLBackup: Mysqldump returned non-zero exit code")
// Backup makes a backup of the sql database into dest.
func (sql SQL) Backup(io stream.IOStream, dest io.Writer, database string) error {
// Snapshot makes a backup of the sql database into dest.
func (sql SQL) Snapshot(io stream.IOStream, dest io.Writer, database string) error {
io = io.Streams(dest, nil, nil, 0).NonInteractive()
code, err := sql.Stack().Exec(io, "sql", "mysqldump", "--databases", database)
@ -75,20 +73,6 @@ func (sql SQL) Backup(io stream.IOStream, dest io.Writer, database string) error
return nil
}
// BackupAll makes a backup of all sql databases
func (sql SQL) BackupAll(io stream.IOStream, dest io.Writer) error {
io = stream.NewIOStream(dest, io.Stderr, nil, 0)
code, err := sql.Stack().Exec(io, "sql", "mysqldump", "--all-databases")
if err != nil {
return err
}
if code != 0 {
return errSQLBackup
}
return nil
}
// OpenShell executes a mysql shell command
func (sql SQL) OpenShell(io stream.IOStream, argv ...string) (int, error) {
return sql.Stack().Exec(io, "sql", "mysql", argv...)

View file

@ -22,13 +22,13 @@ func (SQL) Name() string {
return "sql"
}
//go:embed all:stack
//go:embed all:sql
var resources embed.FS
func (ssh SQL) Stack() component.Installable {
return ssh.ComponentBase.MakeStack(component.Installable{
func (ssh SQL) Stack() component.StackWithResources {
return ssh.ComponentBase.MakeStack(component.StackWithResources{
Resources: resources,
ContextPath: "stack",
ContextPath: "sql",
MakeDirsPerm: fs.ModeDir | fs.ModePerm,
MakeDirs: []string{