Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Make parsing restic output resilient to non-json output #81

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type BackupError struct {
Item string `json:"item"`
}

type outFunc func(string) error
type outFunc func(string)

// New creates a writer which directly writes to the given logger function.
func New(out outFunc) io.Writer {
Expand Down Expand Up @@ -101,10 +101,7 @@ func (w writer) Write(p []byte) (int, error) {
scanner := bufio.NewScanner(bytes.NewReader(p))

for scanner.Scan() {
err := w.out(scanner.Text())
if err != nil {
return len(p), err
}
w.out(scanner.Text())
}

return len(p), nil
Expand All @@ -114,18 +111,16 @@ type LogInfoPrinter struct {
log logr.InfoLogger
}

func (l *LogInfoPrinter) out(s string) error {
func (l *LogInfoPrinter) out(s string) {
l.log.Info(s)
return nil
}

type LogErrPrinter struct {
Log logr.Logger
}

func (l *LogErrPrinter) out(s string) error {
func (l *LogErrPrinter) out(s string) {
l.Log.Error(fmt.Errorf("error during command"), s)
return nil
}

func NewBackupOutputParser(logger logr.Logger, folderName string, summaryFunc SummaryFunc) io.Writer {
Expand All @@ -148,13 +143,12 @@ func NewStdinBackupOutputParser(logger logr.Logger, folderName string, summaryFu
return New(bop.out)
}

func (b *BackupOutputParser) out(s string) error {
func (b *BackupOutputParser) out(s string) {
envelope := &BackupEnvelope{}

err := json.Unmarshal([]byte(s), envelope)
if err != nil {
b.log.Error(err, "can't decode restic json output", "string", s)
return err
}

switch envelope.MessageType {
Expand All @@ -172,7 +166,6 @@ func (b *BackupOutputParser) out(s string) error {
b.log.Info("stats", "time", envelope.TotalDuration, "bytes added", envelope.DataAdded, "bytes processed", envelope.TotalBytesProcessed)
b.summaryFunc(envelope.BackupSummary, b.errorCount, b.folder, 1, time.Now().Unix())
}
return nil
}

func PrintPercentage(logger logr.InfoLogger, p float64) {
Expand Down
29 changes: 16 additions & 13 deletions restic/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,31 @@ func (r *Restic) Restore(snapshotID string, options RestoreOptions, tags ArrayOp
}

func (r *Restic) getLatestSnapshot(snapshotID string, log logr.Logger) (Snapshot, error) {

snapshot := Snapshot{}

if len(r.snapshots) == 0 {
err := fmt.Errorf("no snapshots available")
log.Error(err, "no snapshots available")
return snapshot, err
}

if snapshotID == "" {
log.Info("no snapshot defined, using latest one")
snapshot = r.snapshots[len(r.snapshots)-1]
log.Info("found snapshot", "date", snapshot.Time)
} else {
for i := range r.snapshots {
// Doing substrings so we can also use short IDs here.
if r.snapshots[i].ID[0:len(snapshotID)] == snapshotID {
snapshot = r.snapshots[i]
break
}
}
if snapshot.ID == "" {
log.Error(fmt.Errorf("no Snapshot found with ID %v", snapshotID), "the snapshot does not exist")
return snapshot, fmt.Errorf("no Snapshot found with ID %v", snapshotID)
return snapshot, nil
}

for i := range r.snapshots {
// Doing substrings so we can also use short IDs here.
if strings.HasPrefix(r.snapshots[i].ID, snapshotID) {
return r.snapshots[i], nil
}
}

return snapshot, nil
err := fmt.Errorf("no Snapshot found with ID %v", snapshotID)
log.Error(err, "the snapshot does not exist")
return snapshot, err
}

func (r *Restic) folderRestore(restoreDir string, snapshot Snapshot, restoreFilter string, verify bool, log logr.Logger) error {
Expand Down
5 changes: 2 additions & 3 deletions restic/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ func (r *Restic) getLockList(log logr.Logger) ([]string, error) {
cmd := NewCommand(r.ctx, log, opts)
cmd.Run()

return []string(*list), cmd.FatalError
return *list, cmd.FatalError
}

type locklist []string

func (l *locklist) out(s string) error {
func (l *locklist) out(s string) {
*l = append(*l, s)
return nil
}