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
|
/* Restore from a series of restic snapshots. */
package main
import (
"fmt"
"os"
"io"
"log"
"strings"
"slices"
)
type Snapshot struct {
id string
dirs []string
}
func main() {
body, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
var shots []Snapshot
var current Snapshot
lines := strings.Split(string(body), "\n")
for _, s := range lines[2:len(lines) - 3] {
split := strings.Split(s, " ")
if len(s) == 0 {
shots = append(shots, current)
// Old snapshot is garbage collected.
current = Snapshot{}
} else if s[0] == ' ' {
// Directory at the end of line.
current.dirs = append(current.dirs, split[len(split) - 1])
} else {
// New snapshot.
current.id = split[0]
current.dirs = []string{split[len(split) - 1]}
}
}
// Last not followed by blank line.
shots = append(shots, current)
/*
for _, shot := range shots {
fmt.Println(shot.id)
for _, dir := range shot.dirs {
fmt.Println("\t" + dir)
}
} */
/* Traverse through the snapshots in reverse order.
The earlier entries covered by current are removed. */
seen := []string{}
list := make(map[string]string)
for i := len(shots) - 1; i >= 0; i-- {
for _, dir := range(shots[i].dirs) {
if !slices.Contains(seen, dir) {
seen = append(seen, dir)
list[shots[i].id] += dir + ","
}
}
}
for id, paths := range list {
for _, dir := range strings.Split(paths, ",") {
if dir != "" {
fmt.Println("restic restore " + id + ":" + dir + " --overwrite=if-newer --target=" + dir)
}
}
}
}
|