Skip to content

Commit

Permalink
add dynamic exclusions and smb automount
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Oct 31, 2024
1 parent 7a47e00 commit 0205860
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
48 changes: 42 additions & 6 deletions controllers/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,54 @@ func RunJob(job *store.Job, storeInstance *store.Store, token *store.Token) (*st
return nil, err
}

fmt.Printf("Target found: %s\n", target.Name)
srcPath := target.Path

if strings.HasPrefix(target.Path, "smb://") {
smbPath := strings.TrimPrefix(target.Path, "smb:")

srcPath = fmt.Sprintf("/mnt/pbs-d2d-mounts/%s", strings.ReplaceAll(target.Name, " ", "-"))

err := os.MkdirAll(srcPath, 0700)
if err != nil {
return nil, err
}

mountArgs := []string{
"-t",
"cifs",
smbPath,
srcPath,
"-o",
fmt.Sprintf("username=%s,password=%s", os.Getenv("DOMAIN_USER"), os.Getenv("DOMAIN_PASS")),
}

mnt := exec.Command("mount", mountArgs...)
mnt.Env = os.Environ()

mnt.Stdout = os.Stdout
mnt.Stderr = os.Stderr

fmt.Printf("Mount command composed: %s\n", mnt.String())

err = mnt.Start()
if err != nil {
return nil, err
}
}

cmdArgs := []string{
"backup",
fmt.Sprintf("%s.pxar:%s", strings.ReplaceAll(job.Target, " ", "-"), target.Path),
fmt.Sprintf("%s.pxar:%s", strings.ReplaceAll(job.Target, " ", "-"), srcPath),
"--repository",
job.Store,
"--change-detection-mode=metadata",
"--exclude",
"System Volume Information",
"--exclude",
"$RECYCLE.BIN",
}

exclusions, err := utils.GetExclusions(srcPath)

for _, exclusion := range exclusions {
cmdArgs = append(cmdArgs, "--exclude")
cmdArgs = append(cmdArgs, exclusion)
}

if job.Namespace != "" {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ require (
github.com/mattn/go-sqlite3 v1.14.24
github.com/robfig/cron v1.2.0
)

require github.com/charlievieth/fastwalk v1.0.9 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/charlievieth/fastwalk v1.0.9 h1:Odb92AfoReO3oFBfDGT5J+nwgzQPF/gWAw6E6/lkor0=
github.com/charlievieth/fastwalk v1.0.9/go.mod h1:yGy1zbxog41ZVMcKA/i8ojXLFsuayX5VvwhQVoj9PBI=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
Expand Down
55 changes: 55 additions & 0 deletions utils/get_exclusions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package utils

import (
"io/fs"
"os"
"path/filepath"
"sync"
"syscall"

"github.com/charlievieth/fastwalk"
)

func GetExclusions(root string) ([]string, error) {
var excludedPaths []string
storeMutex := new(sync.Mutex)

conf := fastwalk.Config{
Follow: false,
}

walkFn := func(path string, d fs.DirEntry, err error) error {
if err != nil {
var skipDir bool
if pathErr, ok := err.(*os.PathError); ok {
if errno, ok := pathErr.Err.(syscall.Errno); ok && errno == syscall.ENOTSUP {
storeMutex.Lock()
excludedPaths = append(excludedPaths, convertToGlobPattern(path, root))
storeMutex.Unlock()
skipDir = true
}
} else {
storeMutex.Lock()
excludedPaths = append(excludedPaths, convertToGlobPattern(path, root))
storeMutex.Unlock()
}
if skipDir {
return filepath.SkipDir
}
return nil
}
return nil
}

err := fastwalk.Walk(&conf, root, walkFn)

return excludedPaths, err
}

func convertToGlobPattern(path, root string) string {
relPath, _ := filepath.Rel(root, path)
if d := filepath.Dir(relPath); d == "." {
return "*" + filepath.Base(relPath)
}
return relPath + (string(os.PathSeparator) + "*")
}

0 comments on commit 0205860

Please sign in to comment.