apply backup
This commit is contained in:
69
cmd/cli/commands/apply/apply.go
Normal file
69
cmd/cli/commands/apply/apply.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package apply
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/repository"
|
||||
"cloudsave/pkg/tools/archive"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
type (
|
||||
ListCmd struct {
|
||||
}
|
||||
)
|
||||
|
||||
func (*ListCmd) Name() string { return "apply" }
|
||||
func (*ListCmd) Synopsis() string { return "apply a backup" }
|
||||
func (*ListCmd) Usage() string {
|
||||
return `apply:
|
||||
Apply a backup
|
||||
`
|
||||
}
|
||||
|
||||
func (p *ListCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (p *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if f.NArg() != 2 {
|
||||
fmt.Fprintln(os.Stderr, "error: missing game ID and/or backup uuid")
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
|
||||
gameID := f.Arg(0)
|
||||
uuid := f.Arg(1)
|
||||
|
||||
g, err := repository.One(gameID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to open game metadata: %s\n", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
if err := repository.RestoreArchive(gameID, uuid); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to restore backup: %s\n", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(g.Path); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to remove old data: %s\n", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(filepath.Join(repository.DatastorePath(), gameID, "data.tar.gz"), os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to open archive: %s\n", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if err := archive.Untar(file, g.Path); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to extract archive: %s\n", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"cloudsave/cmd/cli/tools/prompt/credentials"
|
||||
"cloudsave/pkg/remote/client"
|
||||
"cloudsave/pkg/repository"
|
||||
"cloudsave/cmd/cli/tools/prompt/credentials"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
type (
|
||||
ListCmd struct {
|
||||
remote bool
|
||||
backup bool
|
||||
}
|
||||
)
|
||||
|
||||
@@ -28,6 +29,7 @@ func (*ListCmd) Usage() string {
|
||||
|
||||
func (p *ListCmd) SetFlags(f *flag.FlagSet) {
|
||||
f.BoolVar(&p.remote, "a", false, "list all including remote data")
|
||||
f.BoolVar(&p.backup, "include-backup", false, "include backup uuids in the output")
|
||||
}
|
||||
|
||||
func (p *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
@@ -43,20 +45,20 @@ func (p *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
if err := remote(f.Arg(0), username, password); err != nil {
|
||||
if err := remote(f.Arg(0), username, password, p.backup); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
if err := local(); err != nil {
|
||||
if err := local(p.backup); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
func local() error {
|
||||
func local(includeBackup bool) error {
|
||||
games, err := repository.All()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load datastore: %w", err)
|
||||
@@ -66,13 +68,25 @@ func local() error {
|
||||
fmt.Println("ID:", g.ID)
|
||||
fmt.Println("Name:", g.Name)
|
||||
fmt.Println("Last Version:", g.Date, "( Version Number", g.Version, ")")
|
||||
if includeBackup {
|
||||
bk, err := repository.Archives(g.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list backup files: %w", err)
|
||||
}
|
||||
if len(bk) > 0 {
|
||||
fmt.Println("Backup:")
|
||||
for _, b := range bk {
|
||||
fmt.Printf(" - %s (%s)\n", b.UUID, b.CreatedAt)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("---")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func remote(url, username, password string) error {
|
||||
func remote(url, username, password string, includeBackup bool) error {
|
||||
cli := client.New(url, username, password)
|
||||
|
||||
if err := cli.Ping(); err != nil {
|
||||
@@ -91,6 +105,22 @@ func remote(url, username, password string) error {
|
||||
fmt.Println("ID:", g.ID)
|
||||
fmt.Println("Name:", g.Name)
|
||||
fmt.Println("Last Version:", g.Date, "( Version Number", g.Version, ")")
|
||||
if includeBackup {
|
||||
bk, err := cli.ListArchives(g.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list backup files: %w", err)
|
||||
}
|
||||
if len(bk) > 0 {
|
||||
fmt.Println("Backup:")
|
||||
for _, uuid := range bk {
|
||||
b, err := cli.ArchiveInfo(g.ID, uuid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list backup files: %w", err)
|
||||
}
|
||||
fmt.Printf(" - %s (%s)\n", b.UUID, b.CreatedAt)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("---")
|
||||
}
|
||||
|
||||
|
||||
@@ -53,8 +53,10 @@ func (p *RunCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
|
||||
fmt.Fprintf(os.Stderr, "error: cannot process the data of %s: %s\n", metadata.ID, err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
fmt.Println("✅", metadata.Name)
|
||||
}
|
||||
|
||||
fmt.Println("done.")
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
@@ -63,7 +65,13 @@ func (p *RunCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
|
||||
// After archiving, it updates stateFile to the current time.
|
||||
func archiveIfChanged(gameID, srcDir, destTarGz, stateFile string) error {
|
||||
pg := progressbar.New(-1)
|
||||
defer pg.Close()
|
||||
destroyPg := func() {
|
||||
pg.Finish()
|
||||
pg.Clear()
|
||||
pg.Close()
|
||||
|
||||
}
|
||||
defer destroyPg()
|
||||
|
||||
pg.Describe("Scanning " + gameID + "...")
|
||||
|
||||
@@ -103,7 +111,7 @@ func archiveIfChanged(gameID, srcDir, destTarGz, stateFile string) error {
|
||||
|
||||
// make a backup
|
||||
pg.Describe("Backup current data...")
|
||||
if err := repository.Archive(gameID); err != nil {
|
||||
if err := repository.MakeArchive(gameID); err != nil {
|
||||
return fmt.Errorf("failed to archive data: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -51,13 +52,21 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
fmt.Fprintln(os.Stderr, "error: failed to load datastore:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
cli, err := connect(remoteCred, r)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: failed to connect to the remote:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
pg := progressbar.New(-1)
|
||||
destroyPg := func() {
|
||||
pg.Finish()
|
||||
pg.Clear()
|
||||
pg.Close()
|
||||
|
||||
}
|
||||
|
||||
pg.Describe(fmt.Sprintf("[%s] Checking status...", g.Name))
|
||||
exists, err := cli.Exists(r.GameID)
|
||||
if err != nil {
|
||||
slog.Error(err.Error())
|
||||
@@ -65,45 +74,61 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
}
|
||||
|
||||
if !exists {
|
||||
pg.Describe(fmt.Sprintf("[%s] Pushing data...", g.Name))
|
||||
if err := push(g, cli); err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
pg.Describe(fmt.Sprintf("[%s] Pushing backup...", g.Name))
|
||||
if err := pushBackup(g, cli); err != nil {
|
||||
destroyPg()
|
||||
slog.Warn("failed to push backup files", "err", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
pg.Describe(fmt.Sprintf("[%s] Fetching metadata...", g.Name))
|
||||
hlocal, err := repository.Hash(r.GameID)
|
||||
if err != nil {
|
||||
destroyPg()
|
||||
slog.Error(err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
hremote, err := cli.Hash(r.GameID)
|
||||
if err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "error: failed to get the file hash from the remote:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
vlocal, err := repository.Version(r.GameID)
|
||||
if err != nil {
|
||||
destroyPg()
|
||||
slog.Error(err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
remoteMetadata, err := cli.Metadata(r.GameID)
|
||||
if err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "error: failed to get the game metadata from the remote:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
pg.Describe(fmt.Sprintf("[%s] Pulling backup...", g.Name))
|
||||
if err := pullBackup(g, cli); err != nil {
|
||||
slog.Warn("failed to pull backup files", "err", err)
|
||||
}
|
||||
|
||||
pg.Describe(fmt.Sprintf("[%s] Pushing backup...", g.Name))
|
||||
if err := pushBackup(g, cli); err != nil {
|
||||
slog.Warn("failed to push backup files", "err", err)
|
||||
}
|
||||
|
||||
if hlocal == hremote {
|
||||
destroyPg()
|
||||
if vlocal != remoteMetadata.Version {
|
||||
slog.Debug("version is not the same, but the hash is equal. Updating local database")
|
||||
if err := repository.SetVersion(r.GameID, remoteMetadata.Version); err != nil {
|
||||
@@ -116,29 +141,38 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
}
|
||||
|
||||
if vlocal > remoteMetadata.Version {
|
||||
pg.Describe(fmt.Sprintf("[%s] Pushing data...", g.Name))
|
||||
if err := push(g, cli); err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
destroyPg()
|
||||
continue
|
||||
}
|
||||
|
||||
if vlocal < remoteMetadata.Version {
|
||||
destroyPg()
|
||||
if err := pull(r.GameID, cli); err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
if err := repository.SetVersion(r.GameID, remoteMetadata.Version); err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "error: failed to synchronize version number:", err)
|
||||
continue
|
||||
}
|
||||
if err := repository.SetDate(r.GameID, remoteMetadata.Date); err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "error: failed to synchronize date:", err)
|
||||
continue
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
destroyPg()
|
||||
|
||||
if vlocal == remoteMetadata.Version {
|
||||
if err := conflict(r.GameID, g, remoteMetadata, cli); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: failed to resolve conflict:", err)
|
||||
@@ -148,6 +182,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("done.")
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
@@ -222,6 +257,39 @@ func pushBackup(m repository.Metadata, cli *client.Client) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func pullBackup(m repository.Metadata, cli *client.Client) error {
|
||||
bs, err := cli.ListArchives(m.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, uuid := range bs {
|
||||
rinfo, err := cli.ArchiveInfo(m.ID, uuid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
linfo, err := repository.Archive(m.ID, uuid)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
path := filepath.Join(repository.DatastorePath(), m.ID, "hist", uuid)
|
||||
if err := os.MkdirAll(path, 0740); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rinfo.MD5 != linfo.MD5 {
|
||||
if err := cli.PullBackup(m.ID, uuid, filepath.Join(path, "data.tar.gz")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func pull(gameID string, cli *client.Client) error {
|
||||
archivePath := filepath.Join(repository.DatastorePath(), gameID, "data.tar.gz")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user