-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.sh
executable file
·86 lines (71 loc) · 1.7 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
#!/bin/bash
cd $(dirname $0)
root=/data/lib/mirrors
retry=1
repos=(*)
rsync_args="-rtlHp
--safe-links
--delete-after
--timeout=600
--contimeout=60
--delay-updates
--no-motd
--max-size=10m
--bwlimit=1m
--exclude-from=$PWD/exclude.txt
"
log_rotate() {
size=100m
f="$1"
[[ -z $(du -t $size "$f") ]] && return
for (( i=0;;i++ )); do
[[ ! -f "$f.$i" ]] && break
done
for ((;i>0;i--)); do
mv -n "$f.$((i-1))" "$f.$i" || return
done
mv -n "$f" "$f.0"
}
do_sync_one() {
set -e
export DISTRO="$1"
export TARGET_DIR="$root/$DISTRO"
export TMP_DIR="$root/.tmp/$DISTRO"
export LOG_DIR="$root/.log/$DISTRO"
export LOG_FILE="$LOG_DIR/sync.log"
export RSYNC_ARGS=( ${rsync_args} --temp-dir="$TMP_DIR" --log-file="$LOG_FILE" )
(
flock 9 || exit 5
mkdir -p "$TMP_DIR" "$TARGET_DIR" "$root/.lock" "$LOG_DIR"
log_rotate "$LOG_FILE"
touch "$LOG_FILE"
source ./sync.sh
) 9> "$root/.lock/$DISTRO.lock"
}
do_sync_all() {
local repo
fail=()
for repo; do
[[ ! -x "$repo/sync.sh" ]] && continue
if ! pushd "$repo" >/dev/null; then
echo "Can not enter dir $repo"
continue
fi
echo "Syncing $repo"
if ! su http -s /bin/bash -c "do_sync_one '$repo'" ; then
echo "$repo failed"
fail+=("$repo")
fi
popd >/dev/null
done
return ${#fail[@]}
}
export root rsync_args
export -f do_sync_one log_rotate
echo "Start syncing..."
fail=()
while ! do_sync_all "${repos[@]}" && [[ $retry -gt 0 ]]; do
let retry--
repos=( "${fail[@]}" )
echo "Retry sync ${repos[@]}"
done