引子:
git地址:https://github.com/qingfeng0101/sync
1 |
最新版本2.11 |

一、编译(go版本1.19)
1 2 |
#安装go环境 yum install golang |
Linux
1 2 3 4 5 6 7 8 9 |
#客户端 env GOOS=linux GOARCH=amd64 go build -o sync-client . #服务端 env GOOS=linux GOARCH=amd64 go build -o sync-server . env CGO_ENABLED=1 go build -ldflags '-linkmode "external" -extldflags "-static"' |
Windows
1 2 3 4 5 6 |
#客户端 env GOOS=windows GOARCH=amd64 go build -o sync-clien.exe . #服务端 env GOOS=windows GOARCH=amd64 go build -o sync-server.exe . |
server端配置文件
1 |
server.yaml |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 需要同步的目录 datadir: "/home/docker" # 客户端连接地址 clientaddr: "192.168.0.2:8010" # 是否同步删除操作,true为同步,false为不同步,默认不设置为false delete: false # 排除的目录,注意逗号分隔 exclude: "/tmp-test/a,/tmp-test/c/" # 记录已同步文件,默认不设置不记录 savefile: "/root/sync/data.json" # 是否删除已同步文件,默认不设置为false #sourcedelete: false # 同步线程数,默认不设置为1 savethread: 10 # 服务端连接客户端重试连接间隔,默认不设置3秒 timeout: 3 |
客户端配置文件
1 |
client.yaml |
1 2 3 4 5 6 |
# 监听IP地址 ipaddr: "0.0.0.0" # 客户端端口 port: "8010" # 客户端存储数据目录 datadir: "/home/sync" |

Rsync 两种同步方式:
- 上行同步: 将 Rsync 配置文件中的
path
目录同步到当前主机指定目录。(类似于 Pull,进行下载) - 下行同步: 将当前主机指定目录同步到 Rsync 配置文件中的
path
目录。(类似于 Push,进行上传) (本文为下行)
二、启动
Linux
1 2 3 4 |
#客户端启动命令 nohup ./sync-client -f client.yaml > sync-client.log 2>&1 & #服务端启动命令 nohup ./sync-server -f client.yaml > sync-server.log 2>&1 & |
Win-cmd
1 2 3 4 |
#客户端 sync-client -f client.yaml #服务端 sync-server -f server.yaml |
也可以配置随系统启动(推荐)
1 |
vim /usr/lib/systemd/system/sync-client.service |
1 2 3 4 5 6 7 8 |
[Service] Type=notify PrivateTmp=true WorkingDirectory=/opt/sync/client/main ExecStart=/opt/sync/client/main/sync-client -f client.yaml > sync-client.log 2>&1 & Restart=on-failure [Install] WantedBy=multi-user.target |
1 2 |
systemctl start sync-client.service systemctl enable sync-client.service |
三、定时任务参考
1 |
参考此大佬文章即可:https://blog.csdn.net/qq_38766930/article/details/123641213 |
- 本文固定链接: https://www.yoyoask.com/?p=10128
- 转载请注明: shooter 于 SHOOTER 发表