2021-11-22 21:43:59 +01:00
|
|
|
package runner
|
2021-10-12 23:00:24 +02:00
|
|
|
|
2021-11-14 15:07:20 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2021-11-22 21:43:59 +01:00
|
|
|
"github.com/qwc/backive/backup"
|
2021-11-14 15:07:20 +01:00
|
|
|
"github.com/qwc/backive/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run runs the backup script with appropriate rights.
|
2021-11-22 21:43:59 +01:00
|
|
|
func Run(b backup.Backup) error {
|
2021-11-14 15:07:20 +01:00
|
|
|
cfg := config.Get()
|
2021-11-22 21:43:59 +01:00
|
|
|
if cfg.Devices[b.Name].IsMounted() {
|
2021-11-14 15:07:20 +01:00
|
|
|
checkExistence := func(path string, name string) error {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("%s does not exist", name)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Error when checking %s: %w", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Check for existence of target dir
|
|
|
|
if err := checkExistence(b.TargetDir, "target directory"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// check for existence of source dir
|
|
|
|
if err := checkExistence(b.SourceDir, "source directory"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// check for existence of script path
|
|
|
|
if err := checkExistence(b.ScriptPath, "script path"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// setup script environment including user to use
|
|
|
|
// run script
|
|
|
|
}
|
|
|
|
// quit with error that the device is not available.
|
|
|
|
return fmt.Errorf("The device is not mounted")
|
|
|
|
}
|