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
|
#!/bin/bash
#set -eux
## 源目录
src_path="/app/user1/webroot/img_static/"
## 目标目录
dest_path="/app/user2/webroot/img_static/"
dest_user="user2"
dest_ip="XX.XXX.XXX.XX"
log_path="/app/user1/scripts/sync.log"
if [ ! -e "$(dirname $log_path)" ]; then
mkdir -p "$(dirname $log_path)"
fi
/app/user1/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M:%S' --format '%T|%w%f|%e' -e close_write,delete,create,attrib $src_path | while read file; do
f_name="$(echo $file | awk -F '|' '{print $2}')"
if [[ $(basename "$f_name") =~ ^\..* ]]; then
continue
fi
if [ -f "$f_name" ]; then
file_dest_path="$(dirname $f_name)/"
relative_path="${f_name#$src_path}"
rsync -vzrtopgR -e "ssh -i /app/user2/.ssh/sync_rsa" "${src_path}./${relative_path}" $dest_user@$dest_ip:$dest_path >>"$log_path" 2>&1
echo "file ${file} was rsynced" >>"$log_path"
else
rsync -vzrtopg -e "ssh -i /app/user2/.ssh/sync_rsa" "$src_path" $dest_user@$dest_ip:$dest_path >>"$log_path" 2>&1
echo "dir ${file} was rsynced" >>"$log_path"
fi
done
|