-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.sh
executable file
·109 lines (90 loc) · 3.49 KB
/
sync.sh
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
REMOTE_HOST=192.168.1.101
# no backslashes, can be a relative path
DEST="$1"
if [ ! -d "$DEST" -o ! "$DEST" ]; then
echo "usage: sh sync.sh <sync_dir>"
echo "you must specify a existed directory"
exit 0
fi
DEST=`cd "$1"; pwd`
echo "sync with server $REMOTE_HOST: [ $DEST ]"
echo "note: please make sure [ $DEST ] has existed on remote server"
[ ! -d /root/sync ] && mkdir -p /root/sync
PREFIX=/root/sync/prefix-$(date +%y%m%d%H%M%S)
LOCAL_LST_FILE=$PREFIX-local.lst
LOCAL_DIR_LST_FILE=$PREFIX-local-dir.lst
REMOTE_LST_FILE=$PREFIX-remote.lst
SYNC_LST_FILE=$PREFIX-sync.lst
SYNC_DIR_LST_FILE=$PREFIX-sync-dir.lst
TMP_FILE=$PREFIX-tmp.lst
SCP_DEST=${DEST// /\\ }
echo
echo "# 1. preparing sync ..."
pushd "$DEST" 2>&1 1>/dev/null
echo "# 1.1 getting local file list ..."
# `.wdmc` directory is generated by wdmycloud
find . -type f -not -regex ".*/\.wdmc/.*" > $LOCAL_LST_FILE
echo "# 1.1.1 local file count: `wc -l $LOCAL_LST_FILE | awk '{print $1}'`"
echo
echo "# 1.2 getting local directory list ..."
find . -type d -not -regex '.*/\.wdmc/.*' > $LOCAL_DIR_LST_FILE
echo "# 1.2.1 local directory count: `wc -l $LOCAL_DIR_LST_FILE | awk '{print $1}'`"
echo
echo "# 1.3 getting remote file list ..."
ssh $REMOTE_HOST "[ ! -d '$DEST' ] && mkdir '$DEST'; cd '$DEST' && find . -type f -not -regex '.*/\.wdmc/.*'" > $REMOTE_LST_FILE
echo "# 1.3.1 remote file count: `wc -l $REMOTE_LST_FILE | awk '{print $1}'`"
echo
echo "# 1.4 getting sync file list ..."
awk "{if(system(sprintf(\"grep -m 1 \\\"%s\\\" $REMOTE_LST_FILE 1>/dev/null\", \$0))) print \$0;}" \
$LOCAL_LST_FILE > $SYNC_LST_FILE
echo "# 1.4.1 sync file count: `wc -l $SYNC_LST_FILE | awk '{print $1}'`"
echo
echo "# 1.5 getting sync directory list ..."
awk "{if(system(sprintf(\"grep -m 1 \\\"%s\\\" $REMOTE_LST_FILE 1>/dev/null\", \$0))) print \$0;}" \
$LOCAL_DIR_LST_FILE > $SYNC_DIR_LST_FILE
awk "{if(system(sprintf(\"c=\`grep \\\"%s\\\" $SYNC_DIR_LST_FILE | wc -l\`; exit \$c\", \$0)) == 1) print \$0;}" \
$SYNC_DIR_LST_FILE > $TMP_FILE
mv $TMP_FILE $SYNC_DIR_LST_FILE
count_sync_dir=`wc -l $SYNC_DIR_LST_FILE | awk '{print $1}'`
echo "# 1.5.1 sync directory count: $count_sync_dir"
echo
echo "# 2. sync ..."
echo "# 2.1 creating remote sync directories ..."
count=0
for i in `sed 's/ /@__@/g' $SYNC_DIR_LST_FILE`; do
(( count = count + 1 ))
file_name=${i//@__@/ }
echo "# creating remote directory [ $count / $count_sync_dir ] '$DEST/$file_name'"
ssh $REMOTE_HOST "cd '$DEST' && mkdir -p '$file_name' 2>/dev/null"
done
MAX_COUNT=50000
echo
echo "# 2.2 set max send count: $MAX_COUNT"
count_sync_files=`wc -l $SYNC_LST_FILE | awk '{print $1}'`
echo
echo "# 2.3 start coping $count_sync_files files to remote server ..."
count=0
for i in `sed 's/ /@__@/g' $SYNC_LST_FILE`; do
(( count = count + 1 ))
if [ $count -lt $MAX_COUNT ]; then
# local file needs no backslashes
file_name_local=${i//@__@/ }
# remote file needs backslashes
file_name_remote=${file_name_local// /\\ }
file_name_remote=${file_name_remote//(/\\(}
file_name_remote=${file_name_remote//)/\\)}
file_name_remote=${file_name_remote//&/\\&}
echo
echo "# sending $count / $count_sync_files: $file_name_local => $file_name_remote"
scp -rp "$file_name_local" $REMOTE_HOST:"$SCP_DEST/$file_name_remote"
if [ $? -ne 0 ]; then
echo "error occured"
exit 1
fi
fi
done
popd
echo
echo "# 2.4 sync successfully: $count files in all"
echo "bye!!"