2021-09-12 14:15:40 +00:00
|
|
|
package backup
|
2021-01-26 19:52:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/archiver"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2022-12-29 11:31:20 +00:00
|
|
|
"github.com/restic/restic/internal/ui/progress"
|
2021-01-26 19:52:00 +00:00
|
|
|
)
|
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
// A ProgressPrinter can print various progress messages.
|
|
|
|
// It must be safe to call its methods from concurrent goroutines.
|
2021-01-26 19:52:00 +00:00
|
|
|
type ProgressPrinter interface {
|
|
|
|
Update(total, processed Counter, errors uint, currentFiles map[string]struct{}, start time.Time, secs uint64)
|
2022-05-20 22:31:26 +00:00
|
|
|
Error(item string, err error) error
|
|
|
|
ScannerError(item string, err error) error
|
2023-05-18 17:29:50 +00:00
|
|
|
CompleteItem(messageType string, item string, s archiver.ItemStats, d time.Duration)
|
|
|
|
ReportTotal(start time.Time, s archiver.ScanStats)
|
2024-02-22 21:14:48 +00:00
|
|
|
Finish(snapshotID restic.ID, start time.Time, summary *archiver.Summary, dryRun bool)
|
2021-01-26 19:52:00 +00:00
|
|
|
Reset()
|
|
|
|
|
|
|
|
P(msg string, args ...interface{})
|
|
|
|
V(msg string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Counter struct {
|
|
|
|
Files, Dirs, Bytes uint64
|
|
|
|
}
|
|
|
|
|
2021-08-17 23:27:22 +00:00
|
|
|
// Progress reports progress for the `backup` command.
|
2021-01-26 19:52:00 +00:00
|
|
|
type Progress struct {
|
2022-12-29 11:31:20 +00:00
|
|
|
progress.Updater
|
2022-10-21 11:38:10 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
|
2021-11-01 22:13:23 +00:00
|
|
|
start time.Time
|
|
|
|
estimator rateEstimator
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
scanStarted, scanFinished bool
|
|
|
|
|
|
|
|
currentFiles map[string]struct{}
|
|
|
|
processed, total Counter
|
|
|
|
errors uint
|
|
|
|
|
2021-01-26 19:52:00 +00:00
|
|
|
printer ProgressPrinter
|
|
|
|
}
|
|
|
|
|
2022-10-16 10:01:27 +00:00
|
|
|
func NewProgress(printer ProgressPrinter, interval time.Duration) *Progress {
|
2022-12-29 11:31:20 +00:00
|
|
|
p := &Progress{
|
|
|
|
start: time.Now(),
|
2022-10-21 11:38:10 +00:00
|
|
|
currentFiles: make(map[string]struct{}),
|
2022-12-29 11:31:20 +00:00
|
|
|
printer: printer,
|
2021-11-01 22:13:23 +00:00
|
|
|
estimator: *newRateEstimator(time.Now()),
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
2024-02-10 21:58:10 +00:00
|
|
|
p.Updater = *progress.NewUpdater(interval, func(_ time.Duration, final bool) {
|
2022-12-29 11:31:20 +00:00
|
|
|
if final {
|
|
|
|
p.printer.Reset()
|
|
|
|
} else {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
if !p.scanStarted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var secondsRemaining uint64
|
|
|
|
if p.scanFinished {
|
2021-11-01 22:13:23 +00:00
|
|
|
rate := p.estimator.rate(time.Now())
|
2023-06-08 18:24:21 +00:00
|
|
|
tooSlowCutoff := 1024.
|
|
|
|
if rate <= tooSlowCutoff {
|
2021-11-01 22:13:23 +00:00
|
|
|
secondsRemaining = 0
|
|
|
|
} else {
|
|
|
|
todo := float64(p.total.Bytes - p.processed.Bytes)
|
|
|
|
secondsRemaining = uint64(todo / rate)
|
|
|
|
}
|
2022-12-29 11:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.printer.Update(p.total, p.processed, p.errors, p.currentFiles, p.start, secondsRemaining)
|
2022-10-21 11:38:10 +00:00
|
|
|
}
|
2022-12-29 11:31:20 +00:00
|
|
|
})
|
|
|
|
return p
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error is the error callback function for the archiver, it prints the error and returns nil.
|
2022-05-20 22:31:26 +00:00
|
|
|
func (p *Progress) Error(item string, err error) error {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
p.errors++
|
|
|
|
p.scanStarted = true
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
return p.printer.Error(item, err)
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartFile is called when a file is being processed by a worker.
|
|
|
|
func (p *Progress) StartFile(filename string) {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
p.currentFiles[filename] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Progress) addProcessed(c Counter) {
|
|
|
|
p.processed.Files += c.Files
|
|
|
|
p.processed.Dirs += c.Dirs
|
|
|
|
p.processed.Bytes += c.Bytes
|
2021-11-01 22:13:23 +00:00
|
|
|
p.estimator.recordBytes(time.Now(), c.Bytes)
|
2022-10-21 11:38:10 +00:00
|
|
|
p.scanStarted = true
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteBlob is called for all saved blobs for files.
|
2022-10-15 13:21:17 +00:00
|
|
|
func (p *Progress) CompleteBlob(bytes uint64) {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
p.addProcessed(Counter{Bytes: bytes})
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteItem is the status callback function for the archiver when a
|
|
|
|
// file/dir has been saved successfully.
|
|
|
|
func (p *Progress) CompleteItem(item string, previous, current *restic.Node, s archiver.ItemStats, d time.Duration) {
|
|
|
|
if current == nil {
|
|
|
|
// error occurred, tell the status display to remove the line
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
delete(p.currentFiles, item)
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch current.Type {
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeDir:
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
p.addProcessed(Counter{Dirs: 1})
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
switch {
|
|
|
|
case previous == nil:
|
2023-05-18 17:29:50 +00:00
|
|
|
p.printer.CompleteItem("dir new", item, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
case previous.Equals(*current):
|
2023-05-18 17:29:50 +00:00
|
|
|
p.printer.CompleteItem("dir unchanged", item, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
default:
|
2023-05-18 17:29:50 +00:00
|
|
|
p.printer.CompleteItem("dir modified", item, s, d)
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeFile:
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
p.addProcessed(Counter{Files: 1})
|
|
|
|
delete(p.currentFiles, item)
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
switch {
|
|
|
|
case previous == nil:
|
2023-05-18 17:29:50 +00:00
|
|
|
p.printer.CompleteItem("file new", item, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
case previous.Equals(*current):
|
2023-05-18 17:29:50 +00:00
|
|
|
p.printer.CompleteItem("file unchanged", item, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
default:
|
2023-05-18 17:29:50 +00:00
|
|
|
p.printer.CompleteItem("file modified", item, s, d)
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReportTotal sets the total stats up to now
|
|
|
|
func (p *Progress) ReportTotal(item string, s archiver.ScanStats) {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
p.total = Counter{Files: uint64(s.Files), Dirs: uint64(s.Dirs), Bytes: s.Bytes}
|
2022-11-02 20:26:15 +00:00
|
|
|
p.scanStarted = true
|
2021-01-26 19:52:00 +00:00
|
|
|
|
|
|
|
if item == "" {
|
2023-01-21 20:25:08 +00:00
|
|
|
p.scanFinished = true
|
2023-05-18 17:29:50 +00:00
|
|
|
p.printer.ReportTotal(p.start, s)
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish prints the finishing messages.
|
2024-02-22 21:14:48 +00:00
|
|
|
func (p *Progress) Finish(snapshotID restic.ID, summary *archiver.Summary, dryrun bool) {
|
2021-08-17 23:27:22 +00:00
|
|
|
// wait for the status update goroutine to shut down
|
2022-12-29 11:31:20 +00:00
|
|
|
p.Updater.Done()
|
2024-02-22 21:14:48 +00:00
|
|
|
p.printer.Finish(snapshotID, p.start, summary, dryrun)
|
2021-08-18 11:03:08 +00:00
|
|
|
}
|