Skip to content

Commit

Permalink
Merge pull request #330 from databacker/remove-source-target-sh
Browse files Browse the repository at this point in the history
remove unnecessary and undocumented source.sh and target.sh
  • Loading branch information
deitch authored Jul 16, 2024
2 parents 1a0cf69 + f3e4c62 commit c25c32e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 105 deletions.
86 changes: 0 additions & 86 deletions pkg/core/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
"github.com/databacker/mysql-backup/pkg/database"
)

const (
sourceRenameCmd = "/scripts.d/source.sh"
targetRenameCmd = "/scripts.d/target.sh"
)

// Dump run a single dump, based on the provided opts
func (e *Executor) Dump(opts DumpOptions) error {
targets := opts.Targets
Expand Down Expand Up @@ -111,24 +106,6 @@ func (e *Executor) Dump(opts DumpOptions) error {
return fmt.Errorf("error running pre-restore: %v", err)
}

// perform any renaming
newName, err := renameSource(timepart, path.Join(tmpdir, targetFilename), tmpdir, logger.Level == log.DebugLevel)
if err != nil {
return fmt.Errorf("failed rename source: %v", err)
}
if newName != "" {
sourceFilename = newName
}

// perform any renaming
newName, err = renameTarget(timepart, path.Join(tmpdir, targetFilename), tmpdir, logger.Level == log.DebugLevel)
if err != nil {
return fmt.Errorf("failed rename target: %v", err)
}
if newName != "" {
targetFilename = newName
}

// upload to each destination
for _, t := range targets {
logger.Debugf("uploading via protocol %s from %s", t.Protocol(), targetFilename)
Expand Down Expand Up @@ -164,66 +141,3 @@ func postBackup(timestamp, dumpfile, dumpdir, postBackupDir string, debug bool)
}
return runScripts(postBackupDir, env)
}

func renameSource(timestamp, dumpfile, dumpdir string, debug bool) (string, error) {
_, err := os.Stat(sourceRenameCmd)
if err != nil && os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", fmt.Errorf("error reading rename scrpt %s: %v", sourceRenameCmd, err)
}
env := map[string]string{
"NOW": timestamp,
"DUMPFILE": path.Join(dumpdir, dumpfile),
"DUMPDIR": dumpdir,
"DB_DUMP_DEBUG": fmt.Sprintf("%v", debug),
}

// it exists so try to run it
results, err := oneScript(sourceRenameCmd, env)
if err != nil {
return "", fmt.Errorf("error executing rename script %s: %v", sourceRenameCmd, err)
}
results = trimBadChars(results)
newName := strings.TrimSpace(string(results))

return newName, nil
}

func renameTarget(timestamp, dumpfile, dumpdir string, debug bool) (string, error) {
_, err := os.Stat(targetRenameCmd)
if err != nil && os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", fmt.Errorf("error reading rename script %s: %v", targetRenameCmd, err)
}
env := map[string]string{
"NOW": timestamp,
"DUMPFILE": path.Join(dumpdir, dumpfile),
"DUMPDIR": dumpdir,
"DB_DUMP_DEBUG": fmt.Sprintf("%v", debug),
}

// it exists so try to run it
results, err := oneScript(targetRenameCmd, env)
if err != nil {
return "", fmt.Errorf("error executing rename script %s: %v", targetRenameCmd, err)
}
results = trimBadChars(results)
newName := strings.TrimSpace(string(results))

return newName, nil
}

// trimBadChars eliminate these characters '\040\011\012\015'
func trimBadChars(b []byte) []byte {
out := make([]byte, 0)
for _, c := range b {
if c != 040 && c != 011 && c != 012 && c != 015 {
out = append(out, c)
}
}
return out
}
19 changes: 0 additions & 19 deletions pkg/core/scripts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -36,21 +35,3 @@ func runScripts(dir string, env map[string]string) error {
}
return nil
}

func oneScript(target string, env map[string]string) ([]byte, error) {
// execute the file
envSlice := os.Environ()
for k, v := range env {
envSlice = append(envSlice, fmt.Sprintf("%s=%s", k, v))
}
cmd := exec.Command(target)
cmd.Env = envSlice
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("error running file %s: %v", target, err)
}
return stdout.Bytes(), nil
}

0 comments on commit c25c32e

Please sign in to comment.