/
commandRestore.go
109 lines (89 loc) · 2.13 KB
/
commandRestore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"errors"
"flag"
"fmt"
"os"
"path"
"sync"
"github.com/golang/glog"
"github.com/rokeller/bart/domain"
)
type cmdRestore struct {
cmdBase
wg *sync.WaitGroup
queue chan domain.Entry
}
// Finished implements Command.
func (c *cmdRestore) Finished() <-chan bool {
return c.finished
}
// Run implements Command.
func (c *cmdRestore) Run() {
defer c.signalFinished()
for i := 0; i < c.args.degreeOfParallelism; i++ {
c.wg.Add(1)
go func(id int) {
defer c.wg.Done()
c.handleRestoreQueue(id)
}(i)
}
// Find files that are missing locally.
c.archive.FindLocallyMissing(func(entry domain.Entry) {
// The item is present in the backup, but not locally.
absLocalPath := path.Join(c.args.localRoot, entry.RelPath)
_, err := os.Stat(absLocalPath)
if errors.Is(err, os.ErrNotExist) {
c.queue <- entry
} else if nil != err {
glog.Errorf("Failed to check for local file '%s': %v",
entry.RelPath, err)
}
})
}
// Stop implements Command.
func (c *cmdRestore) Stop() {
close(c.queue)
c.wg.Wait()
c.stop()
}
func newRestoreCommand(args []string) Command {
restoreFlags := flag.NewFlagSet("restore", flag.ExitOnError)
commonArgs := addCommonArgs(restoreFlags)
restoreFlags.Parse(args)
return &cmdRestore{
cmdBase: cmdBase{
args: *commonArgs,
archive: newArchive(*commonArgs),
finished: make(chan bool),
},
wg: &sync.WaitGroup{},
queue: make(chan domain.Entry, commonArgs.degreeOfParallelism*2),
}
}
func (c *cmdRestore) handleRestoreQueue(id int) {
numSuccessful, numFailed := 0, 0
for {
entry, isOpen := <-c.queue
if !isOpen {
break
}
glog.V(1).Infof("[Restorer-%d] Restoring file '%s' ...",
id, entry.RelPath)
if c.args.whatIf {
numSuccessful++
fmt.Println(entry.RelPath)
continue
}
if err := c.archive.Restore(entry); nil != err {
numFailed++
glog.Errorf("[Restorer-%d] Restore of file '%s' failed: %v",
id, entry.RelPath, err)
} else {
numSuccessful++
fmt.Println(entry.RelPath)
}
}
glog.Infof("[Restore-%d] Finished. Successfully restored %d file(s), failed to restore %d file(s).",
id, numSuccessful, numFailed)
}