关于linux上文件同步操作 inotify-tools
  IqJftG8zUrLM 2023年11月02日 90 0


  1. 安装 inotify-tools 插件(易可源码安装)
sudo apt-get update
sudo apt-get install inotify-tools
#或者
sudo yum install inotify-tools
  1. 编写测试脚本(如果脚本执行不成功,可能需要ssh-keygen 证书授权)
#!/bin/bash

# Get the current user
CURRENT_USER="$USER"

# Source and destination folders
SOURCE_FOLDER="/path/to/source/folder"
DESTINATION_HOST="username@destination_host"
DESTINATION_FOLDER="/path/to/destination/folder/$CURRENT_USER"

# Log file
LOG_FILE="/path/to/log/file.log"

# Function to sync files using rsync
sync_files() {
    rsync -avz --delete "$SOURCE_FOLDER/" "$DESTINATION_HOST:$DESTINATION_FOLDER/"
}

# Function to log changes
log_change() {
    echo "$(date): Change detected by $CURRENT_USER - $1 $2" >> "$LOG_FILE"
}

# Monitor changes using inotifywait
while true; do
    inotifywait -r -e modify,create,delete,move "$SOURCE_FOLDER" |
    while read path action file; do
        log_change "$action" "$file"
        sync_files
    done
done
  1. 启动脚本
#脚本后台运行01
nohup ./your_script.sh > script_output.log 2>&1 &
#后台运行脚本02
npm install -g pm2
pm2 start your_script.sh --name your_script
pm2 logs your_script
  1. 查看运行时日志
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
sending incremental file list
./
text.txt

sent 110 bytes  received 38 bytes  98.67 bytes/sec
total size is 0  speedup is 0.00
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
  1. 查看文件变更日志
2023年 08月 17日 星期四 10:06:01 CST: Change detected by root - CREATE text.txt


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
IqJftG8zUrLM