From 6e303a42c6252e94c95fec1a1dac45e67abf073e Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Thu, 12 Sep 2024 22:02:20 +0200 Subject: [PATCH] switch to different copy --- backup.go | 78 +++++++++++-------------------- copy.go | 134 ------------------------------------------------------ go.mod | 10 +++- go.sum | 6 +++ 4 files changed, 42 insertions(+), 186 deletions(-) delete mode 100644 copy.go diff --git a/backup.go b/backup.go index d65540e..1620507 100644 --- a/backup.go +++ b/backup.go @@ -5,12 +5,14 @@ import ( "bufio" "fmt" "io" + "log" "os" "os/exec" "path/filepath" "strings" "time" + "github.com/otiai10/copy" "github.com/pelletier/go-toml" ) @@ -95,62 +97,15 @@ func zipIt(pathToZip string, pathToFiles string) error { return nil } -func _CopyDir(source string, destination string) error { - srcDir, err := os.Stat(source) - if err != nil { - return err - } - - if !srcDir.IsDir() { - return fmt.Errorf("source is not a directory") - } - - dstDir, err := os.Stat(destination) - if err != nil { - return err - } - - if !dstDir.IsDir() { - return fmt.Errorf("destination is not a directory") - } - - return nil -} - -func _CopyFile(source string, destination string) error { - srcFile, err := os.Open(source) - dstFile, err := os.Create(destination) - - _, err = io.Copy(dstFile, srcFile) - if err != nil { - return err - } - - err = dstFile.Sync() - if err != nil { - return err - } - - err = srcFile.Close() - if err != nil { - return err - } - - err = dstFile.Close() - if err != nil { - return err - } - - return nil -} - func backup(when string) error { var err error tmpPath := fmt.Sprint("/tmp/update/", when, "_backup") + log.Printf("tmpPath is: %s\n", tmpPath) pacmanDb, err := parsePacmanConf() if err != nil { return err } + log.Printf("pacmanDB is at: %s\n", pacmanDb) err = os.MkdirAll(tmpPath, 0755) if err != nil { @@ -171,7 +126,7 @@ func backup(when string) error { } if strings.Compare(when, "post") != 0 { - err = _CopyDir(pacmanDb, fmt.Sprint(tmpPath, "/", pacmanDb)) + err = copy.Copy(pacmanDb, fmt.Sprint(tmpPath, "/", pacmanDb)) if err != nil { return err } @@ -179,13 +134,16 @@ func backup(when string) error { current_time := time.Now().Format(time.RFC3339) pathToZip := fmt.Sprint(tmpPath, "_", current_time, ".zip") + log.Printf("pathToZip is: %s\n", pathToZip) err = zipIt(pathToZip, tmpPath) if err != nil { return err } for _, backup_location := range backup_locations { - err = _CopyFile(pathToZip, fmt.Sprint(backup_location, "/", when, "_backup_", current_time, ".zip")) + backup_path := fmt.Sprintf("%s/%s/%s_backup_%s.zip", backup_location, when, when, current_time) + log.Printf("backup_path is: %s\n", backup_path) + err = copy.Copy(pathToZip, backup_path) if err != nil { return err } @@ -195,6 +153,23 @@ func backup(when string) error { } func main() { + if strings.Compare(os.Args[1], "debug") != 0 { + log.SetOutput(io.Discard) + } else { + logFile, err := os.OpenFile("/tmp/update.txt", os.O_CREATE | os.O_APPEND | os.O_RDWR, 0666) + if err != nil { + panic(err) + } + defer logFile.Close() + + mw := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(mw) + + fmt.Println("Logfile is /tmp/update.txt") + } + + log.Printf("Cmdline argument is: %s\n", os.Args[1]) + os.RemoveAll("/tmp/update") tree, err := toml.LoadFile("/etc/update.toml") @@ -204,6 +179,7 @@ func main() { } backup_locations = tree.GetArray("backup.locations").([]string) + log.Printf("backup_locations are: %s\n", backup_locations) err = backup(os.Args[1]) if err != nil { diff --git a/copy.go b/copy.go deleted file mode 100644 index 3f9042a..0000000 --- a/copy.go +++ /dev/null @@ -1,134 +0,0 @@ -/* MIT License - * - * Copyright (c) 2017 Roland Singer [roland.singer@desertbit.com] - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package main - -import ( - "fmt" - "io" - "io/ioutil" - "os" - "path/filepath" -) - -// CopyFile copies the contents of the file named src to the file named -// by dst. The file will be created if it does not already exist. If the -// destination file exists, all it's contents will be replaced by the contents -// of the source file. The file mode will be copied from the source and -// the copied data is synced/flushed to stable storage. -func CopyFile(src, dst string) (err error) { - in, err := os.Open(src) - if err != nil { - return - } - defer in.Close() - - out, err := os.Create(dst) - if err != nil { - return - } - defer func() { - if e := out.Close(); e != nil { - err = e - } - }() - - _, err = io.Copy(out, in) - if err != nil { - return - } - - err = out.Sync() - if err != nil { - return - } - - si, err := os.Stat(src) - if err != nil { - return - } - err = os.Chmod(dst, si.Mode()) - if err != nil { - return - } - - return -} - -// CopyDir recursively copies a directory tree, attempting to preserve permissions. -// Source directory must exist, destination directory must *not* exist. -// Symlinks are ignored and skipped. -func CopyDir(src string, dst string) (err error) { - src = filepath.Clean(src) - dst = filepath.Clean(dst) - - si, err := os.Stat(src) - if err != nil { - return err - } - if !si.IsDir() { - return fmt.Errorf("source is not a directory") - } - - _, err = os.Stat(dst) - if err != nil && !os.IsNotExist(err) { - return - } - if err == nil { - return fmt.Errorf("destination already exists") - } - - err = os.MkdirAll(dst, si.Mode()) - if err != nil { - return - } - - entries, err := ioutil.ReadDir(src) - if err != nil { - return - } - - for _, entry := range entries { - srcPath := filepath.Join(src, entry.Name()) - dstPath := filepath.Join(dst, entry.Name()) - - if entry.IsDir() { - err = CopyDir(srcPath, dstPath) - if err != nil { - return - } - } else { - // Skip symlinks. - if entry.Mode()&os.ModeSymlink != 0 { - continue - } - - err = CopyFile(srcPath, dstPath) - if err != nil { - return - } - } - } - - return -} diff --git a/go.mod b/go.mod index 1c19fd6..ef5fafe 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,12 @@ module backup go 1.23.0 -require github.com/pelletier/go-toml v1.9.5 +require ( + github.com/otiai10/copy v1.14.0 + github.com/pelletier/go-toml v1.9.5 +) + +require ( + golang.org/x/sync v0.3.0 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +) diff --git a/go.sum b/go.sum index 02b71df..fef8d80 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,8 @@ +github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU= +github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=