架设Ubuntu源的脚本

84 篇文章 0 订阅
69 篇文章 0 订阅

由于工作需要我们在需要在服务器中架设Ubuntu源,那么你知道架设Ubuntu源的脚本是什么吗?
接下来我们就和小编一起去看看具体的相关介绍吧,希望对你有所帮助。
最近在折腾新的Ubuntu的源,向公司申请了一台配置一般的服务器,另外买了一块硬盘,硬件就有了。
然后装了个Ubuntu 9.04 Server,装上ssh,再装上Nginx,稍微配置一下,服务就好了,这个过程还是蛮简单的,也很顺利,就不多说了。
接下来就可以开始同步数据了。由于我的目标是一个完整的源,所以就没有用apt-mirror之类的工具,而是直接拿rsync抓取上游的数据了。这个数据量是很大的,所以想尽量选择一个速度最快的带rsync的源,但是在茫茫的Ubuntu官方源列表里,这么多源哪个最快呢?当然没有人会告诉你答案,因为每个人的网络环境都不一样啊。所以,最好的办法还是实地测试,所谓磨刀不误砍柴功啊,后面有250G的数据要下载呢,这个测试绝对有价值。
上脚本(不是很完美,够用就行):
#!/bin/bash
urls=curl https://launchpad.net/ubuntu/+archivemirrors | \ grep -B 2 '>rsync</a>' | grep http | awk -F '"' '{print $2}'
rm res
echo “ u r l s " ∣ w h i l e r e a d u r l ; d o r m T w g e t − q − − n o − c a c h e − O T " urls" | while read url;do rm T wget -q --no-cache -O T " urls"whilereadurl;dormTwgetqnocacheOT"url/ls-lR.gz” &
sleep 3
kill %%
echo "testing… u r l " e c h o − n " url" echo -n " url"echon"url " >> res
ls -l T >>res
done

sort -k 6 -n res > fast_mirror
rm res T
最后你可以 tail fast_mirror 看到3秒内下载的字节数最多的一个源。我这还真有一个源,能在3秒把7.4M大的ls-lR.gz给下载完的。
然后,你可以先用这个最快的源把大部分数据先更新下来,完了之后,为了保证数据最新,再向官方源更新一次,我用的是这个脚本:
#!/bin/bash
[[ $UID == 0 ]] || { echo “Must be root to run this script.”; exit 0; }
LOCK="/data/sync_sh/lock"
LOG="/data/sync_sh/log"

while true; do
echo -e "\nstart sync @ date" | tee -a $LOG

if [ -f $LOCK ]; then
echo “another sync is running, I exiting…” | tee -a $LOG
exit 1
fi
touch $LOCK

st=date +%s
rsync --timeout=120 --exclude=".tmp" -avP --delete-excluded --progress rsync://archive.ubuntu.com/ubuntu/pool/ /data/mirrors/ubuntu/pool/
res=$?
if [ $res -eq 0 ]; then
echo “rsync pool succ” | tee -a $LOG
et=date +%s
echo “pool sync use $(( e t − et- etst )) sec = ( ( ( (( ( (((et- s t ) / 60 ) ) ( ( ( st)/60 ))(( ( st)/60))(((et-$st)%60 ))” | tee -a $LOG
else
echo “rsync pool failed” $res | tee -a $LOG
fi

st=date +%s
rsync --timeout=120 --exclude=".tmp" -avP --delete-excluded --progress rsync://archive.ubuntu.com/ubuntu/ /data/mirrors/ubuntu/
res=$?
if [ $res -eq 0 ]; then
echo “rsync all succ” | tee -a $LOG
et=date +%s
echo “all sync use $(( e t − et- etst )) sec = ( ( ( (( ( (((et- s t ) / 60 ) ) ( ( ( st)/60 ))(( ( st)/60))(((et-$st)%60 ))” | tee -a $LOG
else
echo “rsync all failed” $res | tee -a $LOG
fi

df | grep “/data” | tee -a $LOG
echo -e "end sync @ date" | tee -a $LOG

rm $LOCK

sleep 7200
done
这个脚本先更新pool目录,再整个目录更新一遍,这样是为了减少出现软件列表里已经有某软件,但是却下载不到的情况。
脚本常驻运行,在上次更新完以后的2小时,启动下一次更新,这样应该算是国内最新的源了吧?
上文介绍的就是架设Ubuntu源的脚本,按照以上步骤应该不会出问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值