Skip to content

mxm145/lsync

Repository files navigation

lsync

高性能目录同步工具,支持双向同步和实时监测。 命令行和MAC APP两个版本

ScreenShot.png

功能特性

  • 双向同步:支持本地和远程目录的双向同步
  • 实时监测:使用 notify 库实现本地文件系统事件监测
  • 增量同步:使用文件哈希和大小进行快速比较
  • 忽略模式:支持配置文件或命令行参数指定需要忽略的文件/文件夹模式
  • 调试模式:支持调试模式,只测试本地文件系统监测功能
  • 多种连接方式:支持 SSH 密钥和密码认证

mac使用

直接从release下载或使用xcode自行编译

命令行安装

从源代码编译

cd /path/to/lsync
cargo build --release
cp target/release/lsync /usr/local/bin/

命令行使用方法

配置文件

创建 config.toml 文件,配置同步选项:

[sync]
# 本地目录
local_dir = "/path/to/local/directory"
# 远程服务器地址
remote_host = "192.168.1.100"
# 远程服务器端口(默认 22)
remote_port = 22
# 远程用户名
remote_user = "username"
# 远程目录
remote_dir = "/path/to/remote/directory"
# SSH 密钥路径(可选,默认使用系统配置)
ssh_key_path = "/path/to/ssh/key"
# SSH 密码(可选,不建议在配置文件中直接写入)
ssh_password = "password"
# 轮询间隔(毫秒,默认 1000)
poll_interval = 1000
# 忽略的文件/文件夹模式
ignore = [
    "*.tmp",
    "*.log",
    "**/node_modules/**",
    "**/.git/**"
]

命令行参数

lsync [OPTIONS]

可选参数:
  -c, --config-file <PATH>    指定配置文件路径(默认:./config.toml)
  -d, --debug                 调试模式,只测试本地文件系统监测功能
  --local-dir <PATH>          本地目录
  --remote-host <HOST>        远程服务器地址
  --remote-port <PORT>        远程服务器端口
  --remote-user <USERNAME>    远程用户名
  --remote-dir <PATH>         远程目录
  --ssh-key <PATH>            SSH 密钥文件路径
  --ssh-password <PASSWORD>   SSH 密码
  --poll-interval <MILLIS>    轮询间隔(毫秒)
  -i, --ignore <PATTERN>      忽略的文件/文件夹模式(可多次使用)
  -h, --help                  显示帮助信息
  -V, --version               显示版本信息

忽略模式语法

忽略模式使用 glob 模式匹配:

  • *.tmp:匹配所有扩展名为 tmp 的文件
  • *.log:匹配所有扩展名为 log 的文件
  • **/node_modules/**:匹配所有 node_modules 文件夹中的文件
  • **/.git/**:匹配所有 .git 文件夹中的文件
  • *.{tmp,log}:匹配扩展名为 tmp 或 log 的文件
  • !important.txt:不忽略 important.txt 文件(取反模式)

示例

使用配置文件

lsync --config-file /path/to/config.toml

使用命令行参数

lsync \
  --local-dir /home/user/docs \
  --remote-host 192.168.1.100 \
  --remote-user user \
  --remote-dir /home/user/docs \
  --ssh-key /home/user/.ssh/id_rsa \
  --ignore "*.tmp" \
  --ignore "*.log" \
  --ignore "**/node_modules/**"

调试模式

lsync --config-file /path/to/config.toml --debug

架构设计

项目结构:

lsync/
├──lsync-mac
├── src/
│   ├── config.rs         # 配置管理模块
│   ├── error.rs          # 错误处理模块
│   ├── hash.rs           # 文件哈希计算模块
│   ├── lib.rs            # 库入口文件
│   ├── main.rs           # 程序入口文件
│   ├── monitor.rs        # 文件系统监测模块
│   ├── sync.rs           # 同步逻辑模块
│   └── utils.rs          # 工具函数模块(包含忽略模式匹配)
├── Cargo.toml            # 项目配置文件
└── Cargo.lock            # 依赖锁定文件

性能优化

  • 增量同步:只同步内容或大小变化的文件
  • 异步监测:使用 notify 库实现高效的文件系统事件监测
  • 并行处理:使用 rayon 库实现并行处理(待实现)
  • 缓存机制:缓存文件哈希值以减少重复计算(待实现)

安全注意事项

  • 不要在配置文件中明文存储密码,建议使用 SSH 密钥认证
  • 在生产环境中使用前,先在测试环境中进行充分测试
  • 确保程序有适当的权限来访问同步的目录和文件

支持的平台

  • macOS (Darwin)

开发

依赖

  • Rust 1.70+
  • Cargo(Rust 包管理器)

构建

cargo build

运行测试

cargo test

代码格式化

cargo fmt

代码检查

cargo clippy

About

高性能目录同步工具,支持双向同步和实时监测。

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors