/
save.go
178 lines (149 loc) · 4.09 KB
/
save.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/jacobsa/comeback/internal/config"
"github.com/jacobsa/comeback/internal/registry"
"github.com/jacobsa/comeback/internal/save"
"github.com/jacobsa/comeback/internal/wiring"
"github.com/jacobsa/timeutil"
)
var cmdSave = &Command{
Name: "save",
}
var g_discardScoreCache = cmdSave.Flags.Bool(
"discard_score_cache",
false,
"If set, always recompute file hashes; don't rely on stat info.",
)
var fListOnly = cmdSave.Flags.Bool(
"list_only",
false,
"If set, list the files that would be backed up but do nothing further.")
func init() {
cmdSave.Run = runSave // Break flag-related dependency loop.
}
func saveStatePeriodically(
ctx context.Context,
c <-chan time.Time) {
for _ = range c {
log.Println("Writing out state file.")
saveState(ctx)
}
}
func doList(ctx context.Context, job config.Job) (err error) {
err = save.List(
ctx,
os.Stdout,
job.BasePath,
job.Excludes)
if err != nil {
err = fmt.Errorf("save.List: %v", err)
return
}
return
}
func runSave(ctx context.Context, args []string) (err error) {
cfg := getConfig()
// Extract arguments.
if len(args) != 1 {
err = fmt.Errorf("Usage: %s save job_name", os.Args[0])
return
}
jobName := args[0]
// Look for the specified job.
job, ok := cfg.Jobs[jobName]
if !ok {
err = fmt.Errorf("Unknown job: %q", jobName)
return
}
// Resolve any symlinks in the job's base path. This saves us from a race in
// the particular case of copying from a Time Machine volume, where the
// 'Latest' symlink may be updated while we work.
job.BasePath, err = filepath.EvalSymlinks(job.BasePath)
if err != nil {
err = fmt.Errorf("EvalSymlinks(%q): %v", job.BasePath, err)
return
}
log.Printf("Using base path: %s", job.BasePath)
// Special case: visit the file system only if --list_only is set.
if *fListOnly {
err = doList(ctx, job)
if err != nil {
err = fmt.Errorf("doList: %v", err)
return
}
return
}
// Grab dependencies. Make sure to get the registry first, because otherwise
// the user will have to wait for bucket keys to be listed before being
// prompted for a crypto password.
//
// Make sure to do this before setting up state saving below, because these
// calls may modify the state struct.
reg := getRegistry(ctx)
bucket := getBucket(ctx)
crypter := getCrypter(ctx)
state := getState(ctx)
clock := timeutil.RealClock()
// Periodically save state.
const saveStatePeriod = 15 * time.Second
saveStateTicker := time.NewTicker(saveStatePeriod)
go saveStatePeriodically(ctx, saveStateTicker.C)
// Choose a start time for the job.
startTime := clock.Now()
// Call the saving pipeline.
score, err := save.Save(
ctx,
job.BasePath,
job.Excludes,
bucket,
wiring.BlobObjectNamePrefix,
crypter,
state.ExistingScores,
state.ScoresForFiles,
log.New(os.Stderr, "Save progress: ", 0),
clock)
if err != nil {
err = fmt.Errorf("save.Save: %v", err)
return
}
// Register the successful backup.
completedJob := registry.CompletedJob{
StartTime: startTime,
Name: jobName,
Score: score,
}
err = reg.RecordBackup(ctx, completedJob)
if err != nil {
err = fmt.Errorf("RecordBackup: %v", err)
return
}
log.Printf(
"Successfully backed up with score %v. Start time: %v\n",
score.Hex(),
startTime.UTC())
// Store state for next time.
saveStateTicker.Stop()
log.Println("Writing out final state file...")
saveState(ctx)
return
}