This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchupdate
88 lines (71 loc) · 2.16 KB
/
fetchupdate
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
#!/bin/sh
. /etc/rc.subr
name="fetchpkg"
rcvar=fetchupdate_enable
start_cmd="start_fetch"
stop_cmd="stop_fetch"
start_precmd="init_env"
localdir="/other/update" #定义本地存储目录
remotedir="/" #定义远程目录
url="http://update1.chinafreebsd.cn" #定义远程克隆 URL
thread=120
fetch_bin=`pkg query %n lftp`
[ -z "fetchupdate_enable" ] && fetchupdate_enable="NO"
init_env(){
if [ "$fetch_bin" != "lftp" ];then
/usr/sbin/pkg update -f && /usr/sbin/pkg install -y lftp && \
ln -s /usr/local/bin/lftp /usr/local/bin/update_lftp
fi
if [ ! -d "$localdir" ];then
mkdir -p $localdir
fi
check_url $url
}
##################################################
# 执行时间写入日志
##################################################
log(){
echo `date +%Y-%m-%d_%H:%M:%S` >> /var/log/fetchupdate.log
}
##################################################
# 检查预定义 url
##################################################
check_url(){
flag='0'
du1="http://update1.chinafreebsd.cn"
for tmp in "$du1"; do
if [ "$tmp" == "$1" ]; then
flag='1'
break
fi
done
if [ "$flag" == '1' ];then
echo "check url success!"
else
echo "URL:$url is wrong!"
exit
fi
}
#################################################
# --parallel=30 表示同时开启 30 下载线程
# --exclude 表示需要排除的克隆目录
# --delete 表示差异备份
#################################################
start_fetch(){
echo "start fetching ..."
/usr/local/bin/update_lftp -c open -e "mirror --parallel=$thread \
--delete $remotedir $localdir" $url &
log
}
stop_fetch()
{
pid=`ps aux | grep "[u]pdate_lftp" | awk '{print $11}' | cut -d / -f 5`
if [ "$pid" == "update_lftp" ];then
echo "stoping fetchpkg ..."
kill `ps aux | grep "[u]pdate_lftp" | awk '{print $2}'`
else
echo "service dos not exsiting ...."
fi
}
load_rc_config $name
run_rc_command $1