Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backup/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func buildRunCommand() *cobra.Command {
rsyncPath, _ := cmd.Flags().GetString("rsync-path")

cfg := internal.LoadResolvedConfig(configPath)
logger, logPath := internal.CreateMainLogger()
logger, logPath := internal.CreateMainLogger(configPath, false)
command := internal.NewSyncCommand(rsyncPath, logPath)

cfg.Apply(command, logger)
Expand Down
2 changes: 1 addition & 1 deletion backup/cmd/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func buildSimulateCommand() *cobra.Command {
rsyncPath, _ := cmd.Flags().GetString("rsync-path")

cfg := internal.LoadResolvedConfig(configPath)
logger, logPath := internal.CreateMainLogger()
logger, logPath := internal.CreateMainLogger(configPath, true)
command := internal.NewSimulateCommand(rsyncPath, logPath)

cfg.Apply(command, logger)
Expand Down
19 changes: 15 additions & 4 deletions backup/internal/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package internal
import (
"log"
"os"
"path/filepath"
"strings"
"time"
)
Expand All @@ -14,15 +15,25 @@ type Path struct {
Exclusions []string `yaml:"exclusions"`
}

func GetConfigTitle(configPath string) string {
filename := filepath.Base(configPath)
filename = strings.TrimSuffix(filename, ".yaml")

return filename
}

func NormalizePath(path string) string {
return strings.TrimSuffix(strings.ReplaceAll(path, "//", "/"), "/")
}

const LogFilePermission = 0644
const LogDirPermission = 0755

func getLogPath() string {
logPath := "logs/sync-" + time.Now().Format("2006-01-02T15-04-05")
func getLogPath(simulate bool, title string) string {
logPath := "logs/sync-" + time.Now().Format("2006-01-02T15-04-05") + "-" + GetConfigTitle(title)
if simulate {
logPath += "-sim"
}

err := os.MkdirAll(logPath, LogDirPermission)
if err != nil {
Expand All @@ -32,8 +43,8 @@ func getLogPath() string {
return logPath
}

func CreateMainLogger() (*log.Logger, string) {
logPath := getLogPath()
func CreateMainLogger(configPath string, simulate bool) (*log.Logger, string) {
logPath := getLogPath(simulate, configPath)

overallLogPath := logPath + "/summary.log"

Expand Down
21 changes: 21 additions & 0 deletions backup/internal/rsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -37,6 +38,26 @@ func (c SharedCommand) RunWithArgs(job Job, args []string) JobStatus {
return Success
}

func (c SharedCommand) RunWithArgsAndCaptureOutput(job Job, args []string, logPath string) JobStatus {
c.PrintArgs(job, args)

out, err := c.Shell.Execute(c.BinPath, args...)

// Write output to log file for simulate commands
if logPath != "" {
writeErr := os.WriteFile(logPath, out, LogFilePermission)
if writeErr != nil {
fmt.Printf("Warning: Failed to write output to log file %s: %v\n", logPath, writeErr)
}
}

if err != nil {
return Failure
}

return Success
}

func (c SharedCommand) GetVersionInfo() (string, string, error) {
rsyncPath := c.BinPath

Expand Down
7 changes: 4 additions & 3 deletions backup/internal/rsync_simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ func NewSimulateCommand(binPath string, logPath string) SimulateCommand {
return SimulateCommand{
SharedCommand: SharedCommand{
BinPath: binPath,
BaseLogPath: logPath + "-sim",
BaseLogPath: logPath,
Shell: &OsExec{},
},
}
}

func (c SimulateCommand) Run(job Job) JobStatus {
logPath := fmt.Sprintf("%s/job-%s.log", c.BaseLogPath, job.Name)
args := ArgumentsForJob(job, logPath, true)
// Don't use --log-file in simulate mode as rsync doesn't log file changes to it in dry-run
args := ArgumentsForJob(job, "", true)

return c.RunWithArgs(job, args)
return c.RunWithArgsAndCaptureOutput(job, args, logPath)
}
18 changes: 18 additions & 0 deletions backup/internal/test/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,21 @@ func TestNormalizePath(t *testing.T) {
assert.Equal(t, test.expected, result)
}
}

func TestCreateMainLogger_Title_IsPresent(t *testing.T) {
logger, logPath := CreateMainLogger("title", true)
assert.Contains(t, logPath, "title")
assert.NotNil(t, logger, "Logger should not be nil")
}

func TestCreateMainLogger_IsSimulate_HasSimSuffix(t *testing.T) {
logger, logPath := CreateMainLogger("", true)
assert.Contains(t, logPath, "-sim")
assert.NotNil(t, logger, "Logger should not be nil")
}

func TestCreateMainLogger_NotSimulate_HasNoSimSuffix(t *testing.T) {
logger, logPath := CreateMainLogger("", false)
assert.NotContains(t, logPath, "-sim")
assert.NotNil(t, logger, "Logger should not be nil")
}
9 changes: 0 additions & 9 deletions backup/internal/test/rsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,3 @@ func TestGetVersionInfo_IncompletePath(t *testing.T) {
assert.Empty(t, versionInfo)
assert.Empty(t, fullpath)
}

func TestNewSimulateCommand_BaseLogPath_ShallHaveSimSuffix(t *testing.T) {
binPath := "/usr/bin/simulate"
logPath := "/var/log/simulate"

simulateCmd := NewSimulateCommand(binPath, logPath)

assert.Equal(t, logPath+"-sim", simulateCmd.BaseLogPath)
}