diff --git a/backive.go b/backive.go index 4ee9ffa..1bb5783 100644 --- a/backive.go +++ b/backive.go @@ -4,6 +4,7 @@ var config Configuration var runs Runs var database Database +// Init initializes backive with the two basic data structures required, the config, and the database func Init(cfg Configuration, db Database) { config = cfg database = db diff --git a/backup.go b/backup.go index 8bb24fc..48eb747 100644 --- a/backup.go +++ b/backup.go @@ -28,33 +28,35 @@ type Backup struct { // Backups is nothing else than a name to Backup type mapping type Backups map[string]*Backup -// findBackupsForDevice only finds the first backup which is configured for a given device. +// FindBackupsForDevice only finds the first backup which is configured for a given device. func (bs *Backups) FindBackupsForDevice(d Device) ([]*Backup, bool) { - var backups []*Backup = []*Backup{} + var backups = []*Backup{} for _, b := range *bs { if d.Name == b.TargetDevice { backups = append(backups, b) } } - var ret bool = len(backups) > 0 + var ret = len(backups) > 0 return backups, ret } +// CanRun Checks the configuration items required and checks the frequency setting with the run database if a Backup should run. func (b *Backup) CanRun() error { // target path MUST exist if b.TargetPath == "" { - return fmt.Errorf("The setting targetPath MUST exist within a backup configuration.") + return fmt.Errorf("the setting targetPath MUST exist within a backup configuration") } // script must exist, having only script means this is handled in the script if b.ScriptPath == "" { - return fmt.Errorf("The setting scriptPath must exist within a backup configuration.") + return fmt.Errorf("the setting scriptPath must exist within a backup configuration") } if !b.ShouldRun() { - return fmt.Errorf("Frequency (days inbetween) not reached.") + return fmt.Errorf("frequency (days inbetween) not reached") } return nil } +// PrepareRun prepares a run for a backup, creates a logger for the execution of the backup script and gives the rights of the directory recursively to the user specified. func (b *Backup) PrepareRun() error { backupPath := path.Join( config.Settings.SystemMountPoint, @@ -96,7 +98,7 @@ func (b *Backup) Run() error { if !strings.ContainsAny(b.ScriptPath, "/") || strings.HasPrefix(b.ScriptPath, ".") { //The scriptPath is a relative path, from the place of the config, so use the config as base log.Printf("ERROR: Script path is relative, aborting.") - return fmt.Errorf("Script path is relative, aborting.") + return fmt.Errorf("script path is relative, aborting") } cmd := exec.Command("/usr/bin/sh", b.ScriptPath) if b.ExeUser != "" { @@ -129,7 +131,7 @@ func (b *Backup) Run() error { return nil } // quit with error that the device is not available. - return fmt.Errorf("The device is not mounted") + return fmt.Errorf("the device is not mounted") } // Runs contains the Data for the scheduler: mapping from backups to a list of timestamps of the last 10 backups @@ -199,5 +201,5 @@ func (r *Runs) LastRun(b *Backup) (time.Time, error) { return t, nil } } - return time.Unix(0, 0), fmt.Errorf("Backup name not found and therefore has never run") + return time.Unix(0, 0), fmt.Errorf("backup name not found and therefore has never run") } diff --git a/cmd/backive/main.go b/cmd/backive/main.go index 4122bc5..e7e993a 100644 --- a/cmd/backive/main.go +++ b/cmd/backive/main.go @@ -11,8 +11,6 @@ import ( "github.com/qwc/backive" ) -var logfile os.File - func setupLogging() { logname := "/var/log/backive/backive.log" logdir, _ := path.Split(logname) diff --git a/config.go b/config.go index a395d84..1bed738 100644 --- a/config.go +++ b/config.go @@ -44,9 +44,9 @@ func (c *Configuration) Load() { vc := c.Vconfig if err := vc.ReadInConfig(); err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { - panic(fmt.Errorf("Fatal: No config file could be found: %w", err)) + panic(fmt.Errorf("fatal: No config file could be found: %w", err)) } - panic(fmt.Errorf("Fatal error config file: %w ", err)) + panic(fmt.Errorf("fatal error config file: %w ", err)) } log.Printf("Configuration file used: %s", vc.ConfigFileUsed()) diff --git a/database.go b/database.go index e5b78d1..dc506e9 100644 --- a/database.go +++ b/database.go @@ -12,7 +12,7 @@ type Database struct { data map[string]string } -var dbPath string = "/var/lib/backive/data.json" +var dbPath = "/var/lib/backive/data.json" // Save saves the database func (d *Database) Save() { @@ -29,7 +29,7 @@ func (d *Database) Save() { } } -// LoadDb loads the database +// Load loads the database func (d *Database) Load() { if _, err := os.Stat(dbPath); err == nil { data, rferr := os.ReadFile(dbPath) @@ -37,8 +37,7 @@ func (d *Database) Load() { panic(rferr) } json.Unmarshal(data, &d.data) - } else if os.IsNotExist(err) { + } /*else if os.IsNotExist(err) { // no data - - } + }*/ } diff --git a/device.go b/device.go index 97472e8..dc39c68 100644 --- a/device.go +++ b/device.go @@ -6,7 +6,7 @@ import ( "path" ) -var devsByUuid string = "/dev/disk/by-uuid" +var devsByUUID = "/dev/disk/by-uuid" // Devices is nothing else than a name to Device type mapping type Devices map[string]*Device @@ -29,7 +29,7 @@ func (d *Device) Mount() error { log.Printf("Executing mount command for %s", d.Name) cmd := exec.Command( "mount", - path.Join(devsByUuid, d.UUID), + path.Join(devsByUUID, d.UUID), path.Join(config.Settings.SystemMountPoint, d.Name), ) cmd.Stdout = log.Writer() @@ -69,6 +69,7 @@ func (d *Device) Unmount() error { return nil } +// IsMounted returns the mount state of the device func (d *Device) IsMounted() bool { return d.isMounted } diff --git a/events.go b/events.go index e779b57..e15f795 100644 --- a/events.go +++ b/events.go @@ -9,9 +9,10 @@ import ( "path" ) +// EventHandler holds the necessary elements to get an eventhandler setup and working. type EventHandler struct { - ls net.Listener - done <-chan struct{} + ls net.Listener + //done <-chan struct{} callbacks []func(map[string]string) } diff --git a/utils.go b/utils.go index 34909e4..0219a98 100644 --- a/utils.go +++ b/utils.go @@ -5,6 +5,7 @@ import ( "os" ) +// CreateDirectoryIfNotExists Checks for a directory string and creates the directory if it does not exist, must be a absolute path. func CreateDirectoryIfNotExists(dir string) { if _, err := os.Stat(dir); err == nil { //ignore