mirror of https://github.com/qwc/backive.git
Satisfying GoLint
This commit is contained in:
parent
17960a9e2f
commit
d9405890d5
|
@ -4,6 +4,7 @@ var config Configuration
|
||||||
var runs Runs
|
var runs Runs
|
||||||
var database Database
|
var database Database
|
||||||
|
|
||||||
|
// Init initializes backive with the two basic data structures required, the config, and the database
|
||||||
func Init(cfg Configuration, db Database) {
|
func Init(cfg Configuration, db Database) {
|
||||||
config = cfg
|
config = cfg
|
||||||
database = db
|
database = db
|
||||||
|
|
20
backup.go
20
backup.go
|
@ -28,33 +28,35 @@ type Backup struct {
|
||||||
// Backups is nothing else than a name to Backup type mapping
|
// Backups is nothing else than a name to Backup type mapping
|
||||||
type Backups map[string]*Backup
|
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) {
|
func (bs *Backups) FindBackupsForDevice(d Device) ([]*Backup, bool) {
|
||||||
var backups []*Backup = []*Backup{}
|
var backups = []*Backup{}
|
||||||
for _, b := range *bs {
|
for _, b := range *bs {
|
||||||
if d.Name == b.TargetDevice {
|
if d.Name == b.TargetDevice {
|
||||||
backups = append(backups, b)
|
backups = append(backups, b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var ret bool = len(backups) > 0
|
var ret = len(backups) > 0
|
||||||
return backups, ret
|
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 {
|
func (b *Backup) CanRun() error {
|
||||||
// target path MUST exist
|
// target path MUST exist
|
||||||
if b.TargetPath == "" {
|
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
|
// script must exist, having only script means this is handled in the script
|
||||||
if b.ScriptPath == "" {
|
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() {
|
if !b.ShouldRun() {
|
||||||
return fmt.Errorf("Frequency (days inbetween) not reached.")
|
return fmt.Errorf("frequency (days inbetween) not reached")
|
||||||
}
|
}
|
||||||
return nil
|
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 {
|
func (b *Backup) PrepareRun() error {
|
||||||
backupPath := path.Join(
|
backupPath := path.Join(
|
||||||
config.Settings.SystemMountPoint,
|
config.Settings.SystemMountPoint,
|
||||||
|
@ -96,7 +98,7 @@ func (b *Backup) Run() error {
|
||||||
if !strings.ContainsAny(b.ScriptPath, "/") || strings.HasPrefix(b.ScriptPath, ".") {
|
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
|
//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.")
|
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)
|
cmd := exec.Command("/usr/bin/sh", b.ScriptPath)
|
||||||
if b.ExeUser != "" {
|
if b.ExeUser != "" {
|
||||||
|
@ -129,7 +131,7 @@ func (b *Backup) Run() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// quit with error that the device is not available.
|
// 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
|
// 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 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")
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,6 @@ import (
|
||||||
"github.com/qwc/backive"
|
"github.com/qwc/backive"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logfile os.File
|
|
||||||
|
|
||||||
func setupLogging() {
|
func setupLogging() {
|
||||||
logname := "/var/log/backive/backive.log"
|
logname := "/var/log/backive/backive.log"
|
||||||
logdir, _ := path.Split(logname)
|
logdir, _ := path.Split(logname)
|
||||||
|
|
|
@ -44,9 +44,9 @@ func (c *Configuration) Load() {
|
||||||
vc := c.Vconfig
|
vc := c.Vconfig
|
||||||
if err := vc.ReadInConfig(); err != nil {
|
if err := vc.ReadInConfig(); err != nil {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
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())
|
log.Printf("Configuration file used: %s", vc.ConfigFileUsed())
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ type Database struct {
|
||||||
data map[string]string
|
data map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
var dbPath string = "/var/lib/backive/data.json"
|
var dbPath = "/var/lib/backive/data.json"
|
||||||
|
|
||||||
// Save saves the database
|
// Save saves the database
|
||||||
func (d *Database) Save() {
|
func (d *Database) Save() {
|
||||||
|
@ -29,7 +29,7 @@ func (d *Database) Save() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadDb loads the database
|
// Load loads the database
|
||||||
func (d *Database) Load() {
|
func (d *Database) Load() {
|
||||||
if _, err := os.Stat(dbPath); err == nil {
|
if _, err := os.Stat(dbPath); err == nil {
|
||||||
data, rferr := os.ReadFile(dbPath)
|
data, rferr := os.ReadFile(dbPath)
|
||||||
|
@ -37,8 +37,7 @@ func (d *Database) Load() {
|
||||||
panic(rferr)
|
panic(rferr)
|
||||||
}
|
}
|
||||||
json.Unmarshal(data, &d.data)
|
json.Unmarshal(data, &d.data)
|
||||||
} else if os.IsNotExist(err) {
|
} /*else if os.IsNotExist(err) {
|
||||||
// no data
|
// no data
|
||||||
|
}*/
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"path"
|
"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
|
// Devices is nothing else than a name to Device type mapping
|
||||||
type Devices map[string]*Device
|
type Devices map[string]*Device
|
||||||
|
@ -29,7 +29,7 @@ func (d *Device) Mount() error {
|
||||||
log.Printf("Executing mount command for %s", d.Name)
|
log.Printf("Executing mount command for %s", d.Name)
|
||||||
cmd := exec.Command(
|
cmd := exec.Command(
|
||||||
"mount",
|
"mount",
|
||||||
path.Join(devsByUuid, d.UUID),
|
path.Join(devsByUUID, d.UUID),
|
||||||
path.Join(config.Settings.SystemMountPoint, d.Name),
|
path.Join(config.Settings.SystemMountPoint, d.Name),
|
||||||
)
|
)
|
||||||
cmd.Stdout = log.Writer()
|
cmd.Stdout = log.Writer()
|
||||||
|
@ -69,6 +69,7 @@ func (d *Device) Unmount() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsMounted returns the mount state of the device
|
||||||
func (d *Device) IsMounted() bool {
|
func (d *Device) IsMounted() bool {
|
||||||
return d.isMounted
|
return d.isMounted
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,10 @@ import (
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// EventHandler holds the necessary elements to get an eventhandler setup and working.
|
||||||
type EventHandler struct {
|
type EventHandler struct {
|
||||||
ls net.Listener
|
ls net.Listener
|
||||||
done <-chan struct{}
|
//done <-chan struct{}
|
||||||
callbacks []func(map[string]string)
|
callbacks []func(map[string]string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
utils.go
1
utils.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"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) {
|
func CreateDirectoryIfNotExists(dir string) {
|
||||||
if _, err := os.Stat(dir); err == nil {
|
if _, err := os.Stat(dir); err == nil {
|
||||||
//ignore
|
//ignore
|
||||||
|
|
Loading…
Reference in New Issue