-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
backup: add flag --summary-filename #3586
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"bufio" | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
|
@@ -81,6 +82,7 @@ type BackupOptions struct { | |
ExcludeLargerThan string | ||
Stdin bool | ||
StdinFilename string | ||
SummaryFilename string | ||
Tags restic.TagLists | ||
Host string | ||
FilesFrom []string | ||
|
@@ -115,6 +117,7 @@ func init() { | |
f.StringVar(&backupOptions.ExcludeLargerThan, "exclude-larger-than", "", "max `size` of the files to be backed up (allowed suffixes: k/K, m/M, g/G, t/T)") | ||
f.BoolVar(&backupOptions.Stdin, "stdin", false, "read backup from stdin") | ||
f.StringVar(&backupOptions.StdinFilename, "stdin-filename", "stdin", "`filename` to use when reading from stdin") | ||
f.StringVar(&backupOptions.SummaryFilename, "summary-filename", "", "`filename` to append summary data to") | ||
f.Var(&backupOptions.Tags, "tag", "add `tags` for the new snapshot in the format `tag[,tag,...]` (can be specified multiple times)") | ||
|
||
f.StringVarP(&backupOptions.Host, "host", "H", "", "set the `hostname` for the snapshot manually. To prevent an expensive rescan use the \"parent\" flag") | ||
|
@@ -710,7 +713,21 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, term *termstatus.Termina | |
if !gopts.JSON && !opts.DryRun { | ||
progressPrinter.P("snapshot %s saved\n", id.Str()) | ||
} | ||
if !success { | ||
if opts.SummaryFilename != "" { | ||
sf, err := os.OpenFile(opts.SummaryFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) | ||
if err != nil { | ||
return errors.Errorf("%s: appending to summary failed: %v", opts.SummaryFilename, err) | ||
} | ||
buf := new(bytes.Buffer) | ||
if err := json.NewEncoder(buf).Encode(progressReporter.FinishSummary(id)); err != nil { | ||
return errors.Errorf("encoding summary failed: %v", err) | ||
} | ||
fmt.Fprintf(sf, "%s", buf.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
if err := sf.Close(); err != nil { | ||
return errors.Errorf("%s: closing file failed: %v", opts.SummaryFilename, err) | ||
} | ||
} | ||
if !success { | ||
return ErrInvalidSourceData | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,3 +186,25 @@ func (b *TextProgress) Finish(snapshotID restic.ID, start time.Time, summary *Su | |
formatDuration(time.Since(start)), | ||
) | ||
} | ||
|
||
// Return finishing stats in a struct. | ||
func (b *TextProgress) FinishSummary(snapshotID restic.ID, start time.Time, summary *Summary, dryRun bool) summaryOutput { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function does not require any data stored in the TextProgress struct. Thus please extract it into a separate function and remove the duplication with the JsonProgress. |
||
return summaryOutput{ | ||
MessageType: "summary", | ||
FilesNew: summary.Files.New, | ||
FilesChanged: summary.Files.Changed, | ||
FilesUnmodified: summary.Files.Unchanged, | ||
DirsNew: summary.Dirs.New, | ||
DirsChanged: summary.Dirs.Changed, | ||
DirsUnmodified: summary.Dirs.Unchanged, | ||
DataBlobs: summary.ItemStats.DataBlobs, | ||
TreeBlobs: summary.ItemStats.TreeBlobs, | ||
DataAdded: summary.ItemStats.DataSize + summary.ItemStats.TreeSize, | ||
TotalFilesProcessed: summary.Files.New + summary.Files.Changed + summary.Files.Unchanged, | ||
TotalBytesProcessed: summary.ProcessedBytes, | ||
TotalDuration: time.Since(start).Seconds(), | ||
SnapshotID: snapshotID.Str(), | ||
DryRun: dryRun, | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to extract that code into a separate function.
runBackup
is already far too long.