1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| package main
import ( "fmt" "io" "log" "sort" "os" "os/exec" "path/filepath" "strings" "time" "github.com/fsnotify/fsnotify" )
var lastEventTime = make(map[string]time.Time) var debounceDuration = 10 * time.Second
var rootDir = "/www/wwwroot/ftp"
var servers = []string{ "同步服务器1登录账号@同步服务器1IP:/", "同步服务器2登录账号@同步服务器2IP:/", "同步服务器3登录账号@同步服务器3IP:/", "root@47.98.152.221:/", }
var pathMap = map[string]string{ "本地服务器路径标识": "同步服务器路径(从跟路径起算)", "本地服务器路径标识": "同步服务器路径(从跟路径起算)", "Anxin": "www/wwwroot/Anxin", }
func main() { logFile, err := os.OpenFile("script.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) if err != nil { log.Fatalf("无法打开日志文件: %v", err) } defer logFile.Close() log.SetOutput(logFile)
watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal(err) } defer watcher.Close() done := make(chan bool) go func() { for { select { case event, ok := <-watcher.Events: if !ok { return } if (event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create || event.Op&fsnotify.Rename == fsnotify.Rename || event.Op&fsnotify.Chmod == fsnotify.Chmod) && strings.HasSuffix(event.Name, ".apk") { if lastTime, exists := lastEventTime[event.Name]; exists { if time.Since(lastTime) < debounceDuration { continue } } lastEventTime[event.Name] = time.Now() log.Printf("监测到文件变更: %s, 事件类型: %v\n", event.Name, event.Op) err := syncFile(event.Name) if err != nil { log.Printf("文件同步失败: %v", err) } } case err, ok := <-watcher.Errors: if !ok { return } log.Println("错误:", err) } } }() err = filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { err = watcher.Add(path) if err != nil { log.Fatal(err) } } return nil }) if err != nil { log.Fatal(err) } <-done }
func syncFile(filePath string) error { subDir := "" for _, dir := range sortedKeys(pathMap) { if strings.Contains(filePath, dir) { subDir = dir break } } if subDir == "" { return fmt.Errorf("文件没有匹配的目标目录: %s", filePath) } targetPath := pathMap[subDir] fmt.Println("匹配目录:", targetPath)
for _, server := range servers { destination := fmt.Sprintf("%s%s", server, targetPath) cmd := exec.Command("scp", filePath, destination) output, err := cmd.CombinedOutput() if err != nil { log.Printf("执行 scp 命令失败: %v, 输出: %s", err, output) return err } log.Printf("文件 %s 同步到 %s\n", filePath, destination) }
localDestination := filepath.Join(rootDir, targetPath) err := copyFileToLocal(filePath, localDestination) if err != nil { log.Printf("文件复制到本地失败: %v", err) return err } log.Printf("文件 %s 复制到本地目录 %s\n", filePath, localDestination)
return nil }
func copyFileToLocal(srcPath, dstDir string) error { if err := os.MkdirAll(dstDir, os.ModePerm); err != nil { return fmt.Errorf("创建本地目录失败: %v", err) } fileName := filepath.Base(srcPath) dstPath := filepath.Join(dstDir, fileName) srcFile, err := os.Open(srcPath) if err != nil { return fmt.Errorf("打开源文件失败: %v", err) } defer srcFile.Close() dstFile, err := os.Create(dstPath) if err != nil { return fmt.Errorf("创建目标文件失败: %v", err) } defer dstFile.Close() _, err = io.Copy(dstFile, srcFile) if err != nil { return fmt.Errorf("复制文件内容失败: %v", err) } return nil }
func sortedKeys(m map[string]string) []string { keys := make([]string, 0, len(m)) for k := range m { keys = append(keys, k) } sort.Slice(keys, func(i, j int) bool { return len(keys[i]) > len(keys[j]) }) return keys }
|