backup the cluster config files

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-27 06:05:49 -05:00
parent dc2dba2655
commit 3a9e77a4fb
2 changed files with 83 additions and 1 deletions

66
backup.go Normal file
View File

@ -0,0 +1,66 @@
package virtbuf
// thank chatgpt for this because why. why write this if you can have it
// kick this out in 30 seconds
import (
"io"
"log"
"os"
"path/filepath"
)
func backupFiles(srcDir string, destDir string) error {
// Create the destination directory
err := os.MkdirAll(destDir, os.ModePerm)
if err != nil {
log.Println("Failed to create directory: %v", err)
return err
}
// Walk through the source directory
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip if it's not a .test file or if it's a directory
if filepath.Ext(path) != ".json" || info.IsDir() {
return nil
}
// Destination file path
destPath := filepath.Join(destDir, info.Name())
// Copy the file
if err := copyFile(path, destPath); err != nil {
return err
}
return nil
})
if err != nil {
log.Println("Failed to copy files: %v", err)
return err
}
return nil
}
// copyFile copies a file from src to dest
func copyFile(src, dest string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
destFile, err := os.Create(dest)
if err != nil {
return err
}
defer destFile.Close()
// Copy the content
_, err = io.Copy(destFile, srcFile)
return err
}

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"time"
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/encoding/prototext"
@ -19,6 +20,12 @@ import (
func (c *Cluster) ConfigSave() error { func (c *Cluster) ConfigSave() error {
var d *Droplets var d *Droplets
d = new(Droplets) d = new(Droplets)
// try to backup the current cluster config files
if err := backupConfigFiles(); err != nil {
return err
}
d.Droplets = c.Droplets d.Droplets = c.Droplets
if err := ConfigWriteJSON(d, "newdroplets.json"); err != nil { if err := ConfigWriteJSON(d, "newdroplets.json"); err != nil {
fmt.Println("droplets.json write failed") fmt.Println("droplets.json write failed")
@ -71,6 +78,16 @@ func (c *Cluster) ConfigSave() error {
return nil return nil
} }
func backupConfigFiles() error {
// make a new dir to backup the files
now := time.Now()
timestamp := now.Format("2006.01.02.150405")
srcDir := filepath.Join(os.Getenv("VIRTIGO_HOME"))
destDir := filepath.Join(os.Getenv("VIRTIGO_HOME"), timestamp)
return backupFiles(srcDir, destDir)
}
func (c *Cluster) ConfigLoadOld() error { func (c *Cluster) ConfigLoadOld() error {
if c == nil { if c == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil cluster") return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
@ -108,7 +125,6 @@ func (c *Cluster) ConfigLoadOld() error {
return nil return nil
} }
func (c *Cluster) ConfigLoad() error { func (c *Cluster) ConfigLoad() error {
if c == nil { if c == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil cluster") return errors.New("It's not safe to run ConfigLoad() on a nil cluster")