@@ -123,8 +123,9 @@ func (r *resticWrapper) runProfile() error {
123
123
124
124
return nil
125
125
},
126
- func () {
127
- _ = r .runProfilePostFailCommand ()
126
+ // on failure
127
+ func (err error ) {
128
+ _ = r .runProfilePostFailCommand (err )
128
129
},
129
130
)
130
131
})
@@ -140,30 +141,45 @@ func (r *resticWrapper) runInitialize() error {
140
141
rCommand := r .prepareCommand (constants .CommandInit , args )
141
142
// don't display any error
142
143
rCommand .stderr = nil
143
- return runShellCommand (rCommand )
144
+ err := runShellCommand (rCommand )
145
+ if err != nil {
146
+ return fmt .Errorf ("repository initialization on profile '%s': %w" , r .profile .Name , err )
147
+ }
148
+ return nil
144
149
}
145
150
146
151
func (r * resticWrapper ) runCheck () error {
147
152
clog .Infof ("profile '%s': checking repository consistency" , r .profile .Name )
148
153
args := convertIntoArgs (r .profile .GetCommandFlags (constants .CommandCheck ))
149
154
rCommand := r .prepareCommand (constants .CommandCheck , args )
150
- return runShellCommand (rCommand )
155
+ err := runShellCommand (rCommand )
156
+ if err != nil {
157
+ return fmt .Errorf ("backup check on profile '%s': %w" , r .profile .Name , err )
158
+ }
159
+ return nil
151
160
}
152
161
153
162
func (r * resticWrapper ) runRetention () error {
154
163
clog .Infof ("profile '%s': cleaning up repository using retention information" , r .profile .Name )
155
164
args := convertIntoArgs (r .profile .GetRetentionFlags ())
156
165
rCommand := r .prepareCommand (constants .CommandForget , args )
157
- return runShellCommand (rCommand )
166
+ err := runShellCommand (rCommand )
167
+ if err != nil {
168
+ return fmt .Errorf ("backup retention on profile '%s': %w" , r .profile .Name , err )
169
+ }
170
+ return nil
158
171
}
159
172
160
173
func (r * resticWrapper ) runCommand (command string ) error {
161
174
clog .Infof ("profile '%s': starting '%s'" , r .profile .Name , command )
162
175
args := convertIntoArgs (r .profile .GetCommandFlags (command ))
163
176
rCommand := r .prepareCommand (command , args )
164
177
err := runShellCommand (rCommand )
178
+ if err != nil {
179
+ return fmt .Errorf ("%s on profile '%s': %w" , r .command , r .profile .Name , err )
180
+ }
165
181
clog .Infof ("profile '%s': finished '%s'" , r .profile .Name , command )
166
- return err
182
+ return nil
167
183
}
168
184
169
185
func (r * resticWrapper ) prepareCommand (command string , args []string ) shellCommandDefinition {
@@ -202,13 +218,18 @@ func (r *resticWrapper) runPreCommand(command string) error {
202
218
if r .profile .Backup == nil || r .profile .Backup .RunBefore == nil || len (r .profile .Backup .RunBefore ) == 0 {
203
219
return nil
204
220
}
221
+ env := append (os .Environ (), r .getEnvironment ()... )
222
+ env = append (env , r .getProfileEnvironment ()... )
223
+
205
224
for i , preCommand := range r .profile .Backup .RunBefore {
206
225
clog .Debugf ("starting pre-backup command %d/%d" , i + 1 , len (r .profile .Backup .RunBefore ))
207
- env := append (os .Environ (), r .getEnvironment ()... )
208
226
rCommand := newShellCommand (preCommand , nil , env , r .dryRun , r .sigChan )
227
+ // stdout are stderr are coming from the default terminal (in case they're redirected)
228
+ rCommand .stdout = term .GetOutput ()
229
+ rCommand .stderr = term .GetErrorOutput ()
209
230
err := runShellCommand (rCommand )
210
231
if err != nil {
211
- return err
232
+ return fmt . Errorf ( "run-before backup on profile '%s': %w" , r . profile . Name , err )
212
233
}
213
234
}
214
235
return nil
@@ -222,13 +243,18 @@ func (r *resticWrapper) runPostCommand(command string) error {
222
243
if r .profile .Backup == nil || r .profile .Backup .RunAfter == nil || len (r .profile .Backup .RunAfter ) == 0 {
223
244
return nil
224
245
}
246
+ env := append (os .Environ (), r .getEnvironment ()... )
247
+ env = append (env , r .getProfileEnvironment ()... )
248
+
225
249
for i , postCommand := range r .profile .Backup .RunAfter {
226
250
clog .Debugf ("starting post-backup command %d/%d" , i + 1 , len (r .profile .Backup .RunAfter ))
227
- env := append (os .Environ (), r .getEnvironment ()... )
228
251
rCommand := newShellCommand (postCommand , nil , env , r .dryRun , r .sigChan )
252
+ // stdout are stderr are coming from the default terminal (in case they're redirected)
253
+ rCommand .stdout = term .GetOutput ()
254
+ rCommand .stderr = term .GetErrorOutput ()
229
255
err := runShellCommand (rCommand )
230
256
if err != nil {
231
- return err
257
+ return fmt . Errorf ( "run-after backup on profile '%s': %w" , r . profile . Name , err )
232
258
}
233
259
}
234
260
return nil
@@ -238,13 +264,18 @@ func (r *resticWrapper) runProfilePreCommand() error {
238
264
if r .profile .RunBefore == nil || len (r .profile .RunBefore ) == 0 {
239
265
return nil
240
266
}
267
+ env := append (os .Environ (), r .getEnvironment ()... )
268
+ env = append (env , r .getProfileEnvironment ()... )
269
+
241
270
for i , preCommand := range r .profile .RunBefore {
242
271
clog .Debugf ("starting 'run-before' profile command %d/%d" , i + 1 , len (r .profile .RunBefore ))
243
- env := append (os .Environ (), r .getEnvironment ()... )
244
272
rCommand := newShellCommand (preCommand , nil , env , r .dryRun , r .sigChan )
273
+ // stdout are stderr are coming from the default terminal (in case they're redirected)
274
+ rCommand .stdout = term .GetOutput ()
275
+ rCommand .stderr = term .GetErrorOutput ()
245
276
err := runShellCommand (rCommand )
246
277
if err != nil {
247
- return err
278
+ return fmt . Errorf ( "run-before on profile '%s': %w" , r . profile . Name , err )
248
279
}
249
280
}
250
281
return nil
@@ -254,26 +285,37 @@ func (r *resticWrapper) runProfilePostCommand() error {
254
285
if r .profile .RunAfter == nil || len (r .profile .RunAfter ) == 0 {
255
286
return nil
256
287
}
288
+ env := append (os .Environ (), r .getEnvironment ()... )
289
+ env = append (env , r .getProfileEnvironment ()... )
290
+
257
291
for i , postCommand := range r .profile .RunAfter {
258
292
clog .Debugf ("starting 'run-after' profile command %d/%d" , i + 1 , len (r .profile .RunAfter ))
259
- env := append (os .Environ (), r .getEnvironment ()... )
260
293
rCommand := newShellCommand (postCommand , nil , env , r .dryRun , r .sigChan )
294
+ // stdout are stderr are coming from the default terminal (in case they're redirected)
295
+ rCommand .stdout = term .GetOutput ()
296
+ rCommand .stderr = term .GetErrorOutput ()
261
297
err := runShellCommand (rCommand )
262
298
if err != nil {
263
- return err
299
+ return fmt . Errorf ( "run-after on profile '%s': %w" , r . profile . Name , err )
264
300
}
265
301
}
266
302
return nil
267
303
}
268
304
269
- func (r * resticWrapper ) runProfilePostFailCommand () error {
305
+ func (r * resticWrapper ) runProfilePostFailCommand (fail error ) error {
270
306
if r .profile .RunAfterFail == nil || len (r .profile .RunAfterFail ) == 0 {
271
307
return nil
272
308
}
309
+ env := append (os .Environ (), r .getEnvironment ()... )
310
+ env = append (env , r .getProfileEnvironment ()... )
311
+ env = append (env , fmt .Sprintf ("ERROR=%s" , fail .Error ()))
312
+
273
313
for i , postCommand := range r .profile .RunAfterFail {
274
314
clog .Debugf ("starting 'run-after-fail' profile command %d/%d" , i + 1 , len (r .profile .RunAfterFail ))
275
- env := append (os .Environ (), r .getEnvironment ()... )
276
315
rCommand := newShellCommand (postCommand , nil , env , r .dryRun , r .sigChan )
316
+ // stdout are stderr are coming from the default terminal (in case they're redirected)
317
+ rCommand .stdout = term .GetOutput ()
318
+ rCommand .stderr = term .GetErrorOutput ()
277
319
err := runShellCommand (rCommand )
278
320
if err != nil {
279
321
return err
@@ -282,6 +324,7 @@ func (r *resticWrapper) runProfilePostFailCommand() error {
282
324
return nil
283
325
}
284
326
327
+ // getEnvironment returns the environment variables defined in the profile configuration
285
328
func (r * resticWrapper ) getEnvironment () []string {
286
329
if r .profile .Environment == nil || len (r .profile .Environment ) == 0 {
287
330
return nil
@@ -298,6 +341,15 @@ func (r *resticWrapper) getEnvironment() []string {
298
341
return env
299
342
}
300
343
344
+ // getProfileEnvironment returns some environment variables about the current profile
345
+ // (name and command for now)
346
+ func (r * resticWrapper ) getProfileEnvironment () []string {
347
+ return []string {
348
+ fmt .Sprintf ("PROFILE_NAME=%s" , r .profile .Name ),
349
+ fmt .Sprintf ("PROFILE_COMMAND=%s" , r .command ),
350
+ }
351
+ }
352
+
301
353
func convertIntoArgs (flags map [string ][]string ) []string {
302
354
args := make ([]string , 0 )
303
355
@@ -361,10 +413,10 @@ func lockRun(filename string, run func() error) error {
361
413
}
362
414
363
415
// runOnFailure will run the onFailure function if an error occurred in the run function
364
- func runOnFailure (run func () error , onFailure func ()) error {
416
+ func runOnFailure (run func () error , onFailure func (error )) error {
365
417
err := run ()
366
418
if err != nil {
367
- onFailure ()
419
+ onFailure (err )
368
420
}
369
421
return err
370
422
}
0 commit comments