@@ -42,16 +42,21 @@ type Hooks struct {
42
42
43
43
type LocationCopy = map [string ][]string
44
44
45
- 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"`
45
+ type Restore struct {
50
46
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"`
47
+ }
48
+
49
+ type Location struct {
50
+ name string `mapstructure:",omitempty"`
51
+ From []string `mapstructure:"from,omitempty"`
52
+ Type string `mapstructure:"type,omitempty"`
53
+ To []string `mapstructure:"to,omitempty"`
54
+ Hooks Hooks `mapstructure:"hooks,omitempty"`
55
+ Cron string `mapstructure:"cron,omitempty"`
56
+ Options Options `mapstructure:"options,omitempty"`
57
+ ForgetOption LocationForgetOption `mapstructure:"forget,omitempty"`
58
+ CopyOption LocationCopy `mapstructure:"copy,omitempty"`
59
+ RestoreOption Restore `mapstructure:"restore,omitempty"`
55
60
}
56
61
57
62
func GetLocation (name string ) (Location , bool ) {
@@ -366,11 +371,35 @@ func buildRestoreCommand(l Location, to string, snapshot string, options []strin
366
371
return base
367
372
}
368
373
369
- func (l Location ) Restore (to , from string , force bool , snapshot string , options []string ) error {
374
+ func (l Location ) Restore (to , from string , force bool , snapshot string , options []string ) (errors []error ) {
375
+ cwd , _ := GetPathRelativeToConfig ("." )
376
+ hooksOptions := ExecuteOptions {
377
+ Command : "bash" ,
378
+ Dir : cwd ,
379
+ Envs : map [string ]string {
380
+ "AUTORESTIC_LOCATION" : l .name ,
381
+ },
382
+ }
383
+
384
+ defer func () {
385
+ var commands []string
386
+ var isSuccess = len (errors ) == 0
387
+ if isSuccess {
388
+ commands = l .RestoreOption .Hooks .Success
389
+ } else {
390
+ commands = l .RestoreOption .Hooks .Failure
391
+ }
392
+ if err := l .ExecuteHooks (commands , hooksOptions ); err != nil {
393
+ errors = append (errors , err )
394
+ }
395
+
396
+ colors .Success .Println ("Done" )
397
+ }()
398
+
370
399
if from == "" {
371
400
from = l .To [0 ]
372
401
} else if ! l .hasBackend (from ) {
373
- return fmt .Errorf ("invalid backend: \" %s\" " , from )
402
+ errors = append ( errors , fmt .Errorf ("invalid backend: \" %s\" " , from ) )
374
403
}
375
404
376
405
if snapshot == "" {
@@ -381,15 +410,23 @@ func (l Location) Restore(to, from string, force bool, snapshot string, options
381
410
backend , _ := GetBackend (from )
382
411
colors .Secondary .Printf ("Restoring %s@%s → %s\n " , snapshot , backend .name , to )
383
412
413
+ // Before Hooks for restore
414
+ if err := l .ExecuteHooks (l .RestoreOption .Hooks .Before , hooksOptions ); err != nil {
415
+ errors = append (errors , err )
416
+ return
417
+ }
418
+
384
419
t , err := l .getType ()
385
420
if err != nil {
386
- return err
421
+ errors = append (errors , err )
422
+ return
387
423
}
388
424
switch t {
389
425
case TypeLocal :
390
426
to , err = filepath .Abs (to )
391
427
if err != nil {
392
- return err
428
+ errors = append (errors , err )
429
+ return
393
430
}
394
431
// Check if target is empty
395
432
if ! force {
@@ -398,14 +435,17 @@ func (l Location) Restore(to, from string, force bool, snapshot string, options
398
435
if err == nil {
399
436
files , err := ioutil .ReadDir (to )
400
437
if err != nil {
401
- return err
438
+ errors = append (errors , err )
439
+ return
402
440
}
403
441
if len (files ) > 0 {
404
- return notEmptyError
442
+ errors = append (errors , notEmptyError )
443
+ return
405
444
}
406
445
} else {
407
446
if ! os .IsNotExist (err ) {
408
- return err
447
+ errors = append (errors , err )
448
+ return
409
449
}
410
450
}
411
451
}
@@ -414,10 +454,17 @@ func (l Location) Restore(to, from string, force bool, snapshot string, options
414
454
_ , _ , err = backend .ExecDocker (l , buildRestoreCommand (l , "/" , snapshot , options ))
415
455
}
416
456
if err != nil {
417
- return err
457
+ errors = append (errors , err )
458
+ return
418
459
}
419
- colors .Success .Println ("Done" )
420
- return nil
460
+
461
+ // Before Hooks for restore
462
+ if err := l .ExecuteHooks (l .RestoreOption .Hooks .After , hooksOptions ); err != nil {
463
+ errors = append (errors , err )
464
+ return
465
+ }
466
+
467
+ return
421
468
}
422
469
423
470
func (l Location ) RunCron () error {
0 commit comments