Skip to content

Commit

Permalink
Refactor: Remove r.{include,exclude}File.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopaganini committed Apr 25, 2022
1 parent 8b1aaeb commit 540463e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
21 changes: 11 additions & 10 deletions transports/rdiff_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ package transports

import (
"fmt"
"os"
"strings"

"github.com/marcopaganini/logger"
"github.com/marcopaganini/netbackup/config"
"github.com/marcopaganini/netbackup/execute"
"os"
"strings"
)

const (
Expand Down Expand Up @@ -72,17 +73,17 @@ func (r *RdiffBackupTransport) Run() error {
var cmds [][]string

// Create exclude/include lists, if needed
err := r.createExcludeFile(r.config.Exclude)
excludeFile, err := r.createExcludeFile(r.config.Exclude)
if err != nil {
return err
}
defer os.Remove(r.excludeFile)
defer os.Remove(excludeFile)

err = r.createIncludeFile(r.config.Include)
includeFile, err := r.createIncludeFile(r.config.Include)
if err != nil {
return err
}
defer os.Remove(r.includeFile)
defer os.Remove(includeFile)

// Build the full rdiff-backup command line.
cmd := []string{rdiffBackupCmd}
Expand All @@ -91,11 +92,11 @@ func (r *RdiffBackupTransport) Run() error {
}
cmd = append(cmd, "--verbosity=5", "--terminal-verbosity=5", "--preserve-numerical-ids", "--exclude-sockets", "--force")

if r.excludeFile != "" {
cmd = append(cmd, fmt.Sprintf("--exclude-globbing-filelist=%s", r.excludeFile))
if len(r.config.Exclude) != 0 {
cmd = append(cmd, fmt.Sprintf("--exclude-globbing-filelist=%s", excludeFile))
}
if r.includeFile != "" {
cmd = append(cmd, fmt.Sprintf("--include-globbing-filelist=%s", r.includeFile))
if len(r.config.Include) != 0 {
cmd = append(cmd, fmt.Sprintf("--include-globbing-filelist=%s", includeFile))
}
cmd = append(cmd, r.config.ExtraArgs...)

Expand Down
19 changes: 10 additions & 9 deletions transports/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ package transports

import (
"fmt"
"os"
"strings"

"github.com/marcopaganini/logger"
"github.com/marcopaganini/netbackup/config"
"github.com/marcopaganini/netbackup/execute"
"os"
"strings"
)

const (
Expand Down Expand Up @@ -74,26 +75,26 @@ func (r *ResticTransport) Run() error {
var cmds [][]string

// Create exclude list, if needed.
err := r.createExcludeFile(r.config.Exclude)
excludeFile, err := r.createExcludeFile(r.config.Exclude)
if err != nil {
return err
}
defer os.Remove(r.excludeFile)
defer os.Remove(excludeFile)

// Make restic command-line.
resticBin := resticCmd
if r.config.CustomBin != "" {
resticBin = r.config.CustomBin
}
cmd := r.makeResticCmd(resticBin)
cmd := r.makeResticCmd(resticBin, excludeFile)
cmd = append(cmd, "backup", r.config.SourceDir)

// Add to list of commands.
cmds = append(cmds, cmd)

// Create expiration command, if required.
if r.config.ExpireDays != 0 {
cmd = r.makeResticCmd(resticBin)
cmd = r.makeResticCmd(resticBin, excludeFile)
cmd = append(cmd, []string{"forget", fmt.Sprintf("--keep-within=%dd", r.config.ExpireDays), "--prune"}...)
cmds = append(cmds, cmd)
}
Expand All @@ -115,13 +116,13 @@ func (r *ResticTransport) Run() error {
}

// makeResticCmd creates a basic restic command with the binary and extra options.
func (r *ResticTransport) makeResticCmd(resticBin string) []string {
func (r *ResticTransport) makeResticCmd(resticBin, excludeFile string) []string {
cmd := strings.Split(resticBin, " ")
cmd = append(cmd, "-v", "-v")

// Add exclude, if defined.
if r.excludeFile != "" {
cmd = append(cmd, fmt.Sprintf("--exclude-file=%s", r.excludeFile))
if excludeFile != "" {
cmd = append(cmd, fmt.Sprintf("--exclude-file=%s", excludeFile))
}

cmd = append(cmd, r.config.ExtraArgs...)
Expand Down
29 changes: 12 additions & 17 deletions transports/transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import (

// Transport represents all transports
type Transport struct {
config *config.Config
execute execute.Executor
log *logger.Logger
dryRun bool
excludeFile string
includeFile string
config *config.Config
execute execute.Executor
log *logger.Logger
dryRun bool
}

// writeList writes the desired list of exclusions/inclusions into a file, in a
Expand Down Expand Up @@ -74,41 +72,38 @@ func (t *Transport) checkConfig() error {

// createExcludeFile creates a file with the list of patterns to be excluded.
// The file is only created if config.Exclude is set.
func (t *Transport) createExcludeFile(paths []string) error {
func (t *Transport) createExcludeFile(paths []string) (string, error) {
if len(t.config.Exclude) == 0 {
return nil
return "", nil
}
fname, err := writeList("exclude", paths)
if err != nil {
return err
return "", err
}
t.log.Verbosef(2, "Exclude file: %q\n", fname)
// Display file contents to log if dryRun mode
if t.dryRun {
displayFile(t.log, fname)
}
t.excludeFile = fname

return nil
return fname, nil
}

// createIncludeFile creates a file with the list of patterns to be included.
// The file is only created if config.Include is set.
func (t *Transport) createIncludeFile(paths []string) error {
func (t *Transport) createIncludeFile(paths []string) (string, error) {
if len(t.config.Include) == 0 {
return nil
return "", nil
}
fname, err := writeList("include", paths)
if err != nil {
return err
return "", err
}
t.log.Verbosef(2, "Include file: %q\n", fname)
// Display file contents to log if dryRun mode
if t.dryRun {
displayFile(t.log, fname)
}
t.includeFile = fname
return nil
return fname, nil
}

// createFilterFile creates a filter file, in the rsync/rclone style, with the
Expand Down

0 comments on commit 540463e

Please sign in to comment.