From dcfb41dece8e38c450af06b773f7a5718baeca66 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Wed, 16 Jul 2014 09:50:38 -0400 Subject: [PATCH] adding my repo sync tool --- cmd/slackware-sync/README.md | 15 +++++ cmd/slackware-sync/main.go | 87 ++++++++++++++++++++++++++ cmd/slackware-sync/slackware-sync.toml | 20 ++++++ 3 files changed, 122 insertions(+) create mode 100644 cmd/slackware-sync/README.md create mode 100644 cmd/slackware-sync/main.go create mode 100644 cmd/slackware-sync/slackware-sync.toml diff --git a/cmd/slackware-sync/README.md b/cmd/slackware-sync/README.md new file mode 100644 index 0000000..8538e0e --- /dev/null +++ b/cmd/slackware-sync/README.md @@ -0,0 +1,15 @@ +# slackware-sync + +Really it is not limited to slackware, but that is what I was thinking when I +wrote the first version. + +# install + +Install this command + + go get github.com/vbatts/freezing-octo-hipster/cmd/slackware-sync + +Copy the `slackware-sync.toml` to `~/.slackware-sync.toml`, and edit as needed. + +Then either run it periodically, or add a crontab to run `slackware-sync -q`. + diff --git a/cmd/slackware-sync/main.go b/cmd/slackware-sync/main.go new file mode 100644 index 0000000..2f108ca --- /dev/null +++ b/cmd/slackware-sync/main.go @@ -0,0 +1,87 @@ +package main + +import ( + "flag" + "fmt" + "github.com/BurntSushi/toml" + "net/url" + "os" + "os/exec" + "path" +) + +func main() { + flag.Parse() + var config GeneralConfig + _, err := toml.DecodeFile(*flConfigFile, &config) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + _, err = EnsureDirExists(config.SyncDir) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + for _, mirror := range config.Mirrors { + if !mirror.Enabled { + continue + } + uri, err := url.Parse(mirror.URL) + if err != nil { + fmt.Fprintln(os.Stderr, err) + continue + } + dest := path.Join(config.SyncDir, uri.Host, uri.Path) + _, err = EnsureDirExists(dest) + if err != nil { + fmt.Fprintln(os.Stderr, err) + continue + } + + cmd := exec.Command("rsync", "-avPHS", uri.String(), dest+"/") + cmd.Stderr = os.Stderr // we'll want to see errors, regardless + if !*flQuiet { + cmd.Stdout = os.Stdout + } + + err = cmd.Run() + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + } +} + +func EnsureDirExists(path string) (os.FileInfo, error) { + stat, err := os.Stat(path) + if err != nil { + if !os.IsNotExist(err) { + return stat, err + } + if err = os.MkdirAll(path, 0755); err != nil { + return stat, err + } + if stat, err = os.Stat(path); err != nil { + return stat, err + } + } + return stat, nil +} + +type GeneralConfig struct { + SyncDir string `toml:"sync_dir"` + Mirrors map[string]Mirror +} + +type Mirror struct { + Title string `toml:"title"` + URL string `toml:"url"` + Enabled bool `toml:"enabled"` +} + +var ( + flConfigFile = flag.String("c", path.Join(os.Getenv("HOME"), ".slackware-sync.toml"), "config file for the sync") + flQuiet = flag.Bool("q", false, "less output") +) diff --git a/cmd/slackware-sync/slackware-sync.toml b/cmd/slackware-sync/slackware-sync.toml new file mode 100644 index 0000000..e009317 --- /dev/null +++ b/cmd/slackware-sync/slackware-sync.toml @@ -0,0 +1,20 @@ +sync_dir = "/mnt/storage/slackware" + +[mirrors] + + [mirrors.alien_multilib_current] + url = "rsync://taper.alienbase.nl/mirrors/people/alien/multilib/current/" + enabled = false + + [mirrors.slackware64] + url = "rsync://slackware.osuosl.org/slackware/slackware64-current/" + enabled = true + + [mirrors.slackware64_14.1] + url = "rsync://slackware.osuosl.org/slackware/slackware64-14.1/" + enabled = true + + [mirrors.slackware32] + url = "rsync://slackware.osuosl.org/slackware/slackware-current/" + enabled = false +