Skip to content

Commit

Permalink
Handle fatal errors while uploading data using kopia (#1149)
Browse files Browse the repository at this point in the history
* Enabled FailFast option on kopia uploader

* Handle fatal errors during upload
  • Loading branch information
pavannd1 committed Dec 1, 2021
1 parent dd7086c commit 7cf2321
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 20 additions & 4 deletions pkg/kopia/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package kopia
import (
"context"
"fmt"
"strings"
"time"

"github.com/kopia/kopia/fs"
Expand All @@ -39,7 +40,7 @@ func SnapshotSource(
) (string, int64, error) {
fmt.Printf("Snapshotting %v ...\n", sourceInfo)

t0 := time.Now()
snapshotStartTime := time.Now()

previous, err := findPreviousSnapshotManifest(ctx, rep, sourceInfo, nil)
if err != nil {
Expand All @@ -58,8 +59,7 @@ func SnapshotSource(

manifest.Description = description

manifestID, err := snapshot.SaveSnapshot(ctx, rep, manifest)
if err != nil {
if _, err := snapshot.SaveSnapshot(ctx, rep, manifest); err != nil {
return "", 0, errors.Wrap(err, "Failed to save kopia manifest")
}

Expand All @@ -76,9 +76,25 @@ func SnapshotSource(
return "", 0, errors.Wrap(ferr, "Failed to flush kopia repository")
}

return reportSnapshotStatus(ctx, snapshotStartTime, manifest)
}

func reportSnapshotStatus(ctx context.Context, snapshotStartTime time.Time, manifest *snapshot.Manifest) (string, int64, error) {
manifestID := manifest.ID
snapSize := manifest.Stats.TotalFileSize

fmt.Printf("\nCreated snapshot with root %v and ID %v in %v\n", manifest.RootObjectID(), manifestID, time.Since(t0).Truncate(time.Second))
fmt.Printf("\nCreated snapshot with root %v and ID %v in %v\n", manifest.RootObjectID(), manifestID, time.Since(snapshotStartTime).Truncate(time.Second))

// Even if the manifest is created, it might contain fatal errors and failed entries
var errs []string
if ds := manifest.RootEntry.DirSummary; ds != nil {
for _, ent := range ds.FailedEntries {
errs = append(errs, ent.Error)
}
}
if len(errs) != 0 {
return "", 0, errors.New(strings.Join(errs, "\n"))
}

return string(manifestID), snapSize, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func Push(ctx context.Context, configFile, dirPath, filePath, password, sourceEn

// Setup kopia uploader
u := snapshotfs.NewUploader(rep)
// Fail full snapshot if errors are encountered during upload
u.FailFast = true

// Populate the source info with source path and file
sourceInfo := snapshot.SourceInfo{
Expand Down

0 comments on commit 7cf2321

Please sign in to comment.