mirror of https://github.com/qwc/backive.git
Another deve status committed...
This commit is contained in:
parent
6479744b4e
commit
86a99224ac
|
@ -9,6 +9,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// main Simple main function for the udev callback executable, registered with the udev service.
|
||||||
func main() {
|
func main() {
|
||||||
f, err := os.OpenFile("/tmp/backive/udev.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
f, err := os.OpenFile("/tmp/backive/udev.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/qwc/backive/config"
|
||||||
|
)
|
||||||
|
|
||||||
// Backup contains all necessary information for executing a configured backup.
|
// Backup contains all necessary information for executing a configured backup.
|
||||||
type Backup struct {
|
type Backup struct {
|
||||||
Name string `mapstructure:",omitempty"`
|
Name string `mapstructure:",omitempty"`
|
||||||
|
@ -10,3 +17,36 @@ type Backup struct {
|
||||||
Frequency int `mapstructure:"frequency"`
|
Frequency int `mapstructure:"frequency"`
|
||||||
ExeUser string `mapstructure:"user,omitempty"`
|
ExeUser string `mapstructure:"user,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run runs the backup script with appropriate rights.
|
||||||
|
func (b Backup) Run() error {
|
||||||
|
cfg := config.Get()
|
||||||
|
if cfg.Devices[b.Name].isMounted() {
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
|
@ -5,12 +5,21 @@ type Device struct {
|
||||||
Name string `mapstructure:",omitempty"`
|
Name string `mapstructure:",omitempty"`
|
||||||
UUID string `mapstructure:"uuid"`
|
UUID string `mapstructure:"uuid"`
|
||||||
OwnerUser string `mapstructure:"owner,omitempty"`
|
OwnerUser string `mapstructure:"owner,omitempty"`
|
||||||
|
isMounted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount will mount a device
|
// Mount will mount a device
|
||||||
func (d Device) Mount() {
|
func (d Device) Mount() {
|
||||||
|
|
||||||
|
d.isMounted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmount will unmount a device
|
// Unmount will unmount a device
|
||||||
func (d Device) Unmount() {
|
func (d Device) Unmount() {
|
||||||
|
|
||||||
|
d.isMounted = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Device) IsMounted() bool {
|
||||||
|
return d.isMounted
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
package events
|
package events
|
||||||
|
|
||||||
import "net"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
var ls net.Listener
|
var ls net.Listener
|
||||||
var done <-chan struct{}
|
var done <-chan struct{}
|
||||||
|
var callbacks := make([]func(map[string]string), 3)
|
||||||
|
|
||||||
|
// Init initializes the unix socket.
|
||||||
func Init(socketPath string) {
|
func Init(socketPath string) {
|
||||||
ls, err = net.Listen(socketPath)
|
ls, err = net.Listen(socketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -12,26 +18,44 @@ func Init(socketPath string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunLoop() {
|
// Listen starts the event loop.
|
||||||
|
func Listen() {
|
||||||
for {
|
for {
|
||||||
go func() {
|
go func() {
|
||||||
process()
|
process()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterCallback adds a function to the list of callback functions for processing of events.
|
||||||
|
func RegisterCallback(cb func(map[string]string)){
|
||||||
|
append(callbacks, cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
// process processes each and every unix socket event, Unmarshals the json data and calls the list of callbacks.
|
||||||
func process() {
|
func process() {
|
||||||
client, err = ls.Accept()
|
client, err = ls.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
//TODO: rewrite to be safe regarding buffer length
|
data := make([]byte, 2048)
|
||||||
buf := make([]byte, 2048)
|
for {
|
||||||
nr, err := client.Read(buf)
|
buf := make([]byte, 512)
|
||||||
if err != nil {
|
nr, err := client.Read(buf)
|
||||||
return
|
if err != nil && err != io.EOF {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
append(data, buf[0:nr])
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
env := map[string]string{}
|
||||||
|
err := json.Unmarshal(data, &env)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for _, v = range(callbacks) {
|
||||||
|
v(env)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := buf[0:nr]
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue