mirror of https://github.com/qwc/backive.git
Moved socket location. MVP working (no scheduler...)
This commit is contained in:
parent
f51fb7042d
commit
0af8e5cda5
|
@ -15,7 +15,7 @@ backups:
|
||||||
user: qwc
|
user: qwc
|
||||||
targetDevice: dev_test
|
targetDevice: dev_test
|
||||||
frequency: 0
|
frequency: 0
|
||||||
scriptPath: ./testbackup.sh
|
scriptPath: /home/qwc/source/backive/testbackup.sh
|
||||||
sourcePath: /home/qwc/web/worktime
|
sourcePath: /home/qwc/web/worktime
|
||||||
targetPath: worktime
|
targetPath: worktime
|
||||||
scanner_usbstick_test:
|
scanner_usbstick_test:
|
||||||
|
@ -38,7 +38,7 @@ backups:
|
||||||
targetPath: worktime
|
targetPath: worktime
|
||||||
settings:
|
settings:
|
||||||
systemMountPoint: /mnt/backups
|
systemMountPoint: /mnt/backups
|
||||||
unixSocketLocation: /tmp/backive/backive.sock
|
unixSocketLocation: /var/local/backive/backive.sock
|
||||||
logLocation: /var/log/backive
|
logLocation: /var/log/backive
|
||||||
dbLocation: /var/lib/backive
|
dbLocation: /var/lib/backive
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -48,11 +49,11 @@ var (
|
||||||
events EventHandler
|
events EventHandler
|
||||||
)
|
)
|
||||||
var devsByUuid string = "/dev/disk/by-uuid"
|
var devsByUuid string = "/dev/disk/by-uuid"
|
||||||
|
var dbPath string = "/var/lib/backive/data.json"
|
||||||
|
|
||||||
// Database is a simple string to string mapping, where arbitrary strings can be stored and safed to disk or loaded
|
// Database is a simple string to string mapping, where arbitrary strings can be stored and safed to disk or loaded
|
||||||
type Database struct {
|
type Database struct {
|
||||||
data map[string]string
|
data map[string]string
|
||||||
path string "/var/lib/backive/data.json"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save saves the database
|
// Save saves the database
|
||||||
|
@ -62,9 +63,9 @@ func (d *Database) Save() {
|
||||||
panic(merr)
|
panic(merr)
|
||||||
}
|
}
|
||||||
log.Printf("Writing database output to file: %s", jsonstr)
|
log.Printf("Writing database output to file: %s", jsonstr)
|
||||||
saveDir, _ := path.Split(d.path)
|
saveDir, _ := path.Split(dbPath)
|
||||||
createDirectoryIfNotExists(saveDir)
|
createDirectoryIfNotExists(saveDir)
|
||||||
err := os.WriteFile(d.path, []byte(jsonstr), 0644)
|
err := os.WriteFile(dbPath, []byte(jsonstr), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -72,8 +73,8 @@ func (d *Database) Save() {
|
||||||
|
|
||||||
// LoadDb loads the database
|
// LoadDb loads the database
|
||||||
func (d *Database) Load() {
|
func (d *Database) Load() {
|
||||||
if _, err := os.Stat(d.path); err == nil {
|
if _, err := os.Stat(dbPath); err == nil {
|
||||||
data, rferr := os.ReadFile(d.path)
|
data, rferr := os.ReadFile(dbPath)
|
||||||
if rferr != nil {
|
if rferr != nil {
|
||||||
panic(rferr)
|
panic(rferr)
|
||||||
}
|
}
|
||||||
|
@ -98,7 +99,7 @@ func (d *Device) Mount() error {
|
||||||
createDirectoryIfNotExists(
|
createDirectoryIfNotExists(
|
||||||
path.Join(config.Settings.SystemMountPoint, d.Name),
|
path.Join(config.Settings.SystemMountPoint, d.Name),
|
||||||
)
|
)
|
||||||
time.Sleep(3000 * time.Millisecond)
|
//time.Sleep(3000 * time.Millisecond)
|
||||||
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",
|
||||||
|
@ -160,7 +161,7 @@ type Configuration struct {
|
||||||
Settings Settings `mapstructure:"settings"`
|
Settings Settings `mapstructure:"settings"`
|
||||||
Devices Devices `mapstructure:"devices"`
|
Devices Devices `mapstructure:"devices"`
|
||||||
Backups Backups `mapstructure:"backups"`
|
Backups Backups `mapstructure:"backups"`
|
||||||
vconfig *viper.Viper
|
Vconfig *viper.Viper
|
||||||
}
|
}
|
||||||
|
|
||||||
// Settings struct holds the global configuration items
|
// Settings struct holds the global configuration items
|
||||||
|
@ -199,19 +200,20 @@ func (c *Configuration) CreateViper() {
|
||||||
vconfig.AddConfigPath("/etc/backive/") // system config
|
vconfig.AddConfigPath("/etc/backive/") // system config
|
||||||
//vconfig.AddConfigPath("$HOME/.backive/")
|
//vconfig.AddConfigPath("$HOME/.backive/")
|
||||||
vconfig.AddConfigPath(".")
|
vconfig.AddConfigPath(".")
|
||||||
c.vconfig = vconfig
|
c.Vconfig = vconfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load loads the configuration from the disk
|
// Load loads the configuration from the disk
|
||||||
func (c *Configuration) Load() {
|
func (c *Configuration) Load() {
|
||||||
c.CreateViper()
|
c.CreateViper()
|
||||||
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())
|
||||||
|
|
||||||
//Unmarshal all into Configuration type
|
//Unmarshal all into Configuration type
|
||||||
err := vc.Unmarshal(c)
|
err := vc.Unmarshal(c)
|
||||||
|
@ -311,22 +313,30 @@ func (b *Backup) CanRun() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Backup) PrepareRun() error {
|
func (b *Backup) PrepareRun() error {
|
||||||
createDirectoryIfNotExists(path.Join(
|
backupPath := path.Join(
|
||||||
config.Settings.SystemMountPoint,
|
config.Settings.SystemMountPoint,
|
||||||
b.TargetDevice,
|
b.TargetDevice,
|
||||||
b.TargetPath,
|
b.TargetPath,
|
||||||
))
|
)
|
||||||
|
createDirectoryIfNotExists(backupPath)
|
||||||
// configure extra logger
|
// configure extra logger
|
||||||
logname := "/var/log/backive/backive.log"
|
logname := "/var/log/backive/backive.log"
|
||||||
logdir, _ := path.Split(logname)
|
logdir, _ := path.Split(logname)
|
||||||
createDirectoryIfNotExists(logdir)
|
createDirectoryIfNotExists(logdir)
|
||||||
logname = path.Join(logdir, b.Name)
|
logname = path.Join(logdir, b.Name) + ".log"
|
||||||
logfile, err := os.OpenFile(logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
logfile, err := os.OpenFile(logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Error creating logfile!")
|
log.Println("Error creating logfile!")
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
writer := io.MultiWriter(logfile)
|
writer := io.MultiWriter(logfile)
|
||||||
b.logger = log.New(writer, b.Name, log.LstdFlags)
|
b.logger = log.New(writer, b.Name, log.LstdFlags)
|
||||||
|
cmd := exec.Command("chown", "-R", b.ExeUser, backupPath)
|
||||||
|
err = cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
b.logger.Printf("chown for backup directory failed: %s", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,9 +350,19 @@ func (b *Backup) Run() error {
|
||||||
log.Printf("Device %s not found", b.TargetDevice)
|
log.Printf("Device %s not found", b.TargetDevice)
|
||||||
}
|
}
|
||||||
if ok && dev.IsMounted() {
|
if ok && dev.IsMounted() {
|
||||||
|
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.")
|
||||||
|
}
|
||||||
// setup script environment including user to use
|
// setup script environment including user to use
|
||||||
cmd := exec.Command("/usr/bin/sh", b.ScriptPath)
|
cmd := exec.Command("/usr/bin/sh", b.ScriptPath)
|
||||||
|
if b.ExeUser != "" {
|
||||||
|
cmd = exec.Command("sudo", "-E", "-u", b.ExeUser, "/usr/bin/sh", b.ScriptPath)
|
||||||
|
}
|
||||||
b.logger.Printf("Running backup script of '%s'", b.Name)
|
b.logger.Printf("Running backup script of '%s'", b.Name)
|
||||||
|
b.logger.Printf("Script is: %s", b.ScriptPath)
|
||||||
|
b.logger.Printf("Full command is: %s", cmd.String())
|
||||||
// does this work?
|
// does this work?
|
||||||
cmd.Stdout = b.logger.Writer()
|
cmd.Stdout = b.logger.Writer()
|
||||||
cmd.Stderr = b.logger.Writer()
|
cmd.Stderr = b.logger.Writer()
|
||||||
|
@ -353,10 +373,14 @@ func (b *Backup) Run() error {
|
||||||
),
|
),
|
||||||
fmt.Sprintf("BACKIVE_FROM=%s", b.SourcePath),
|
fmt.Sprintf("BACKIVE_FROM=%s", b.SourcePath),
|
||||||
}
|
}
|
||||||
|
log.Printf("Environment for process: %s", cmd.Env)
|
||||||
|
cmd.Dir = path.Join(config.Settings.SystemMountPoint, dev.Name)
|
||||||
|
|
||||||
|
log.Printf("About to run: %s", cmd.String())
|
||||||
// run script
|
// run script
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Backup '%s' run failed", b.Name)
|
log.Printf("Backup '%s' run failed", b.Name)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -467,15 +491,15 @@ func defaultCallback(envMap map[string]string) {
|
||||||
prepErr := backup.PrepareRun()
|
prepErr := backup.PrepareRun()
|
||||||
log.Println("Prepared run.")
|
log.Println("Prepared run.")
|
||||||
if prepErr != nil {
|
if prepErr != nil {
|
||||||
log.Fatalf("Error running the backup routine: %v", err)
|
log.Printf("Error running the backup routine: %v", err)
|
||||||
}
|
}
|
||||||
log.Println("Running backup.")
|
log.Println("Running backup.")
|
||||||
rerr := backup.Run()
|
rerr := backup.Run()
|
||||||
if rerr != nil {
|
if rerr != nil {
|
||||||
log.Fatalf("Error running the backup routine: %v", err)
|
log.Printf("Error running the backup routine: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Fatalf("Error running the backup routine: %v", err)
|
log.Printf("Error running the backup routine: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Println("No backup found, unmounting.")
|
log.Println("No backup found, unmounting.")
|
||||||
|
|
|
@ -35,7 +35,7 @@ func main() {
|
||||||
log.Println(e)
|
log.Println(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := net.Dial("unix", "/tmp/backive/backive.sock")
|
c, err := net.Dial("unix", "/var/local/backive/backive.sock")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Could not instantiate unix socket. Aborting")
|
log.Fatalln("Could not instantiate unix socket. Aborting")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
#! /usr/bin/env bash
|
#! /usr/bin/env bash
|
||||||
#
|
#
|
||||||
set -x
|
set -x
|
||||||
|
env
|
||||||
ls -la ${BACKIVE_TO}
|
ls -la ${BACKIVE_TO}
|
||||||
echo "This is a test..."
|
echo "This is a test..."
|
||||||
echo "mount/to ${BACKIVE_TO}"
|
echo "mount/to $BACKIVE_TO"
|
||||||
|
echo "mount/from $BACKIVE_FROM"
|
||||||
|
echo "mount $BACKIVE_MOUNT"
|
||||||
cp -Rv ${BACKIVE_FROM}/* ${BACKIVE_TO}/
|
cp -Rv ${BACKIVE_FROM}/* ${BACKIVE_TO}/
|
||||||
ls -la ${BACKIVE_MOUNT}
|
ls -la ${BACKIVE_MOUNT}
|
||||||
ls -la ${BACKIVE_TO}
|
ls -la ${BACKIVE_TO}
|
||||||
|
|
Loading…
Reference in New Issue