@@ -33,25 +33,30 @@ const (
33
33
)
34
34
35
35
type Hooks struct {
36
+ RestoreOption HooksList `mapstructure:"restore,omitempty"`
37
+ BackupOption HooksList `mapstructure:"backup,omitempty"`
38
+ }
39
+
40
+ type LocationCopy = map [string ][]string
41
+
42
+ type HooksList struct {
36
43
Dir string `mapstructure:"dir"`
37
44
Before HookArray `mapstructure:"before,omitempty"`
38
45
After HookArray `mapstructure:"after,omitempty"`
39
46
Success HookArray `mapstructure:"success,omitempty"`
40
47
Failure HookArray `mapstructure:"failure,omitempty"`
41
48
}
42
49
43
- type LocationCopy = map [string ][]string
44
-
45
50
type Location struct {
46
- name string `mapstructure:",omitempty"`
47
- From []string `mapstructure:"from,omitempty"`
48
- Type string `mapstructure:"type,omitempty"`
49
- To []string `mapstructure:"to,omitempty"`
50
- Hooks Hooks `mapstructure:"hooks,omitempty"`
51
- Cron string `mapstructure:"cron,omitempty"`
52
- Options Options `mapstructure:"options,omitempty"`
53
- ForgetOption LocationForgetOption `mapstructure:"forget,omitempty"`
54
- CopyOption LocationCopy `mapstructure:"copy,omitempty"`
51
+ name string `mapstructure:",omitempty"`
52
+ From []string `mapstructure:"from,omitempty"`
53
+ Type string `mapstructure:"type,omitempty"`
54
+ To []string `mapstructure:"to,omitempty"`
55
+ Hooks Hooks `mapstructure:"hooks,omitempty"`
56
+ Cron string `mapstructure:"cron,omitempty"`
57
+ Options Options `mapstructure:"options,omitempty"`
58
+ ForgetOption LocationForgetOption `mapstructure:"forget,omitempty"`
59
+ CopyOption LocationCopy `mapstructure:"copy,omitempty"`
55
60
}
56
61
57
62
func GetLocation (name string ) (Location , bool ) {
@@ -123,12 +128,12 @@ func (l Location) validate() error {
123
128
return nil
124
129
}
125
130
126
- func (l Location ) ExecuteHooks (commands []string , options ExecuteOptions ) error {
131
+ func (l Location ) ExecuteHooks (commands []string , directory string , options ExecuteOptions ) error {
127
132
if len (commands ) == 0 {
128
133
return nil
129
134
}
130
- if l . Hooks . Dir != "" {
131
- if dir , err := GetPathRelativeToConfig (l . Hooks . Dir ); err != nil {
135
+ if directory != "" {
136
+ if dir , err := GetPathRelativeToConfig (directory ); err != nil {
132
137
return err
133
138
} else {
134
139
options .Dir = dir
@@ -190,7 +195,7 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
190
195
}
191
196
192
197
// Hooks
193
- if err := l .ExecuteHooks (l .Hooks .Before , options ); err != nil {
198
+ if err := l .ExecuteHooks (l .Hooks .BackupOption . Before , l . Hooks . BackupOption . Dir , options ); err != nil {
194
199
errors = append (errors , err )
195
200
goto after
196
201
}
@@ -290,19 +295,19 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
290
295
}
291
296
292
297
// After hooks
293
- if err := l .ExecuteHooks (l .Hooks .After , options ); err != nil {
298
+ if err := l .ExecuteHooks (l .Hooks .BackupOption . After , l . Hooks . BackupOption . Dir , options ); err != nil {
294
299
errors = append (errors , err )
295
300
}
296
301
297
302
after:
298
303
var commands []string
299
304
var isSuccess = len (errors ) == 0
300
305
if isSuccess {
301
- commands = l .Hooks .Success
306
+ commands = l .Hooks .BackupOption . Success
302
307
} else {
303
- commands = l .Hooks .Failure
308
+ commands = l .Hooks .BackupOption . Failure
304
309
}
305
- if err := l .ExecuteHooks (commands , options ); err != nil {
310
+ if err := l .ExecuteHooks (commands , l . Hooks . BackupOption . Dir , options ); err != nil {
306
311
errors = append (errors , err )
307
312
}
308
313
@@ -367,11 +372,35 @@ func buildRestoreCommand(l Location, to string, snapshot string, options []strin
367
372
return base
368
373
}
369
374
370
- func (l Location ) Restore (to , from string , force bool , snapshot string , options []string ) error {
375
+ func (l Location ) Restore (to , from string , force bool , snapshot string , options []string ) (errors []error ) {
376
+ cwd , _ := GetPathRelativeToConfig ("." )
377
+ hooksOptions := ExecuteOptions {
378
+ Command : "bash" ,
379
+ Dir : cwd ,
380
+ Envs : map [string ]string {
381
+ "AUTORESTIC_LOCATION" : l .name ,
382
+ },
383
+ }
384
+
385
+ defer func () {
386
+ var commands []string
387
+ var isSuccess = len (errors ) == 0
388
+ if isSuccess {
389
+ commands = l .Hooks .RestoreOption .Success
390
+ } else {
391
+ commands = l .Hooks .RestoreOption .Failure
392
+ }
393
+ if err := l .ExecuteHooks (commands , l .Hooks .RestoreOption .Dir , hooksOptions ); err != nil {
394
+ errors = append (errors , err )
395
+ }
396
+
397
+ colors .Success .Println ("Done" )
398
+ }()
399
+
371
400
if from == "" {
372
401
from = l .To [0 ]
373
402
} else if ! l .hasBackend (from ) {
374
- return fmt .Errorf ("invalid backend: \" %s\" " , from )
403
+ errors = append ( errors , fmt .Errorf ("invalid backend: \" %s\" " , from ) )
375
404
}
376
405
377
406
if snapshot == "" {
@@ -382,15 +411,23 @@ func (l Location) Restore(to, from string, force bool, snapshot string, options
382
411
backend , _ := GetBackend (from )
383
412
colors .Secondary .Printf ("Restoring %s@%s → %s\n " , snapshot , backend .name , to )
384
413
414
+ // Before Hooks for restore
415
+ if err := l .ExecuteHooks (l .Hooks .RestoreOption .Before , l .Hooks .RestoreOption .Dir , hooksOptions ); err != nil {
416
+ errors = append (errors , err )
417
+ return
418
+ }
419
+
385
420
t , err := l .getType ()
386
421
if err != nil {
387
- return err
422
+ errors = append (errors , err )
423
+ return
388
424
}
389
425
switch t {
390
426
case TypeLocal :
391
427
to , err = filepath .Abs (to )
392
428
if err != nil {
393
- return err
429
+ errors = append (errors , err )
430
+ return
394
431
}
395
432
// Check if target is empty
396
433
if ! force {
@@ -399,14 +436,17 @@ func (l Location) Restore(to, from string, force bool, snapshot string, options
399
436
if err == nil {
400
437
files , err := ioutil .ReadDir (to )
401
438
if err != nil {
402
- return err
439
+ errors = append (errors , err )
440
+ return
403
441
}
404
442
if len (files ) > 0 {
405
- return notEmptyError
443
+ errors = append (errors , notEmptyError )
444
+ return
406
445
}
407
446
} else {
408
447
if ! os .IsNotExist (err ) {
409
- return err
448
+ errors = append (errors , err )
449
+ return
410
450
}
411
451
}
412
452
}
@@ -415,10 +455,17 @@ func (l Location) Restore(to, from string, force bool, snapshot string, options
415
455
_ , _ , err = backend .ExecDocker (l , buildRestoreCommand (l , "/" , snapshot , options ))
416
456
}
417
457
if err != nil {
418
- return err
458
+ errors = append (errors , err )
459
+ return
419
460
}
420
- colors .Success .Println ("Done" )
421
- return nil
461
+
462
+ // After Hooks for restore
463
+ if err := l .ExecuteHooks (l .Hooks .RestoreOption .After , l .Hooks .RestoreOption .Dir , hooksOptions ); err != nil {
464
+ errors = append (errors , err )
465
+ return
466
+ }
467
+
468
+ return
422
469
}
423
470
424
471
func (l Location ) RunCron () error {
0 commit comments