在Ubuntu中触发数据同步可根据需求选择工具,以下是常见方法及操作:
实时同步(基于事件触发)
sudo apt install inotify-tools rsync。sync_data.sh),使用inotifywait监控源目录变化,触发rsync同步:#!/bin/bash
SOURCE_DIR="/path/to/source"
TARGET_DIR="/path/to/target"
inotifywait -m -r -e create,delete,modify "$SOURCE_DIR" | while read FILE; do
rsync -avz --delete "$SOURCE_DIR/" "$TARGET_DIR/"
done
chmod +x sync_data.sh,./sync_data.sh(可搭配nohup后台运行)。定时同步(基于时间触发)
crontab -e。0 * * * * rsync -avz /source/ /target/。/etc/systemd/system/sync.service)和定时器文件(如/etc/systemd/system/sync.timer),配置同步命令和触发时间。sudo systemctl enable --now sync.timer。工具自带触发机制
根据具体场景选择工具,实时同步适合高频数据变更,定时同步适合周期性任务。