mirror of
https://github.com/vbatts/utils.git
synced 2024-12-04 14:35:41 +00:00
adding my repo sync tool
This commit is contained in:
parent
37f00446a3
commit
dcfb41dece
3 changed files with 122 additions and 0 deletions
15
cmd/slackware-sync/README.md
Normal file
15
cmd/slackware-sync/README.md
Normal file
|
@ -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`.
|
||||
|
87
cmd/slackware-sync/main.go
Normal file
87
cmd/slackware-sync/main.go
Normal file
|
@ -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")
|
||||
)
|
20
cmd/slackware-sync/slackware-sync.toml
Normal file
20
cmd/slackware-sync/slackware-sync.toml
Normal file
|
@ -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
|
||||
|
Loading…
Reference in a new issue