使 mirror://mirrors.ubuntu.com 高度可用

Til*_*ill 26 apt mirrors

我们是一个分布式团队,这就是我们的 VM 使用 Ubuntumirror://设置的原因。我们的/etc/apt/sources.list样子如下:

deb mirror://mirrors.ubuntu.com/mirrors.txt lucid main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt lucid-updates main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt lucid-backports main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt lucid-security main restricted universe multiverse
Run Code Online (Sandbox Code Playgroud)

这本身就非常了不起,对在不同地点工作的人非常有帮助——不需要本地定制等。理论上的故障转移。

在日常工作中,此设置经常失败。这周我想说2-3次。

现在作为我的壁橱镜子mirrors.ubuntu.com回来了ftp.uni-bayreuth.de。不幸的是,它似乎下降了。

这已经持续了几个小时,镜子由一所大学的志愿者主持,今天是星期五,我希望这会很快得到纠正。

所有的谈话,我的问题是:

  • 有人用这个吗?
  • 您如何解决停机问题?(我的快速修复是一个 shell 脚本)
  • 我怎样才能帮助改善这种情况?

Rad*_*anu 10

我个人认为选择最佳 Ubuntu 存储库镜像的最佳方法是使用 GUI 方法:

下载服务器

现在,为了改善问题中描述的情况,您需要以某种方式设置一些规则。这些规则必须付诸行动mirrors.ubuntu.com。我可以建议一些规则如下:

  • 列出最佳/首选镜子;你可以在这里这里这里看到很多镜子
  • 如果你找到了一个好的镜子,把它添加到列表中
  • 如果镜子有时掉下来或坏了,这意味着它不是一个好镜子,你应该把它从列表中删除
  • 你可以使用netselectapt-spy或者apt-fast
  • 和其他,取决于您的要求。

接下来,为了了解您可以如何解决,我可以通过三个 bash 脚本示例为您提供一个逐步描述的方法。第一个脚本使用您当前所在国家/地区的镜像而不是mirrors.ubuntu.com/mirrors.txt(对于每个国家/地区都有一个带有镜像关联的文本文件;请参阅http://mirrors.ubuntu.com/):

  • 终端运行中mkdir -p bin-如果您还没有,此命令将bin在您的home文件夹中创建一个目录。
  • 运行后gedit ~/bin/change_sources.sh- 这将change_sources.sh在 gedit 中创建新文件。
  • 将以下脚本之一复制并粘贴到新创建的文件中:
#!/bin/bash

export DISPLAY=:0

if ! [ "`ping -c 1 google.com`" ]; then
    notify-send "No internet connection"
    exit 0  
fi

ip=$(curl -s 'http://ipecho.net/plain')
country=$(curl -s 'http://geoiplookup.net/geoapi.php?output=countrycode' \
    | awk '{ print toupper($2) }')
release=$(lsb_release -sc)

file="/etc/apt/sources.list"
old_file="/etc/apt/sources.list.old"

line=$(head -n 1 $file)
new_line="## Ubuntu Repos for $ip"

if [ "$line" == "$new_line" ] ; then
    exit 0
fi

cp -f $file $old_file

printf "$new_line
deb mirror://mirrors.ubuntu.com/$country.txt $release main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/$country.txt $release-updates main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/$country.txt $release-backports main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/$country.txt $release-security main restricted universe multiverse
" > $file

notify-send "$file has been changed" "The old file has been put in $old_file"

exit 0
Run Code Online (Sandbox Code Playgroud)

或者,类似于可以在http://repogen.simplylinux.ch/上找到的内容:

#!/bin/bash

export DISPLAY=:0

if ! [ "`ping -c 1 google.com`" ]; then
    notify-send "No internet connection"
    exit 0  
fi

ip=$(curl -s 'http://ipecho.net/plain')
country=$(curl -s 'http://geoiplookup.net/geoapi.php?output=countrycode' \
    | awk '{ print tolower($2) }')
release=$(lsb_release -sc)

file="/etc/apt/sources.list"
old_file="/etc/apt/sources.list.old"

line=$(head -n 1 $file)
new_line="## Ubuntu Main Repos for $ip"

if [ "$line" == "$new_line" ] ; then
    exit 0
fi

cp -f $file $old_file

printf "$new_line
deb http://$country.archive.ubuntu.com/ubuntu/ $release main restricted universe  multiverse
deb-src http://$country.archive.ubuntu.com/ubuntu/ $release main restricted universe multiverse

## Ubuntu Update Repos for $ip
deb http://$country.archive.ubuntu.com/ubuntu/ $release-security main restricted universe multiverse
deb http://$country.archive.ubuntu.com/ubuntu/ $release-updates main restricted universe multiverse
deb-src http://$country.archive.ubuntu.com/ubuntu/ $release-security main restricted universe multiverse
deb-src http://$country.archive.ubuntu.com/ubuntu/ $release-updates main restricted universe multiverse
" > $file

notify-send "$file has been changed" "The old file has been put in $old_file"

exit 0
Run Code Online (Sandbox Code Playgroud)

或者,使用脚本netselect(下载从这里,安装说明这里)作为izx柠尼斯解释这个答案

#!/bin/bash

export DISPLAY=:0

if ! [ "`ping -c 1 google.com`" ]; then
    notify-send "No internet connection"
    exit 0  
fi

url=$(netselect \
    `wget -q -O- https://launchpad.net/ubuntu/+archivemirrors \
        | grep -P -B8 "statusUP|statusSIX" \
        | grep -o -P "(f|ht)tp.*\"" \
        | tr '"\n' '  '` \
    | awk '{print $2}')
release=$(lsb_release -sc)

if [ "$url" == "" ] ; then
    exit 0
fi

file="/etc/apt/sources.list"
old_file="/etc/apt/sources.list.old"

cp -f $file $old_file

printf "## Ubuntu Best Repos
deb http://extras.ubuntu.com/ubuntu $release main
deb-src http://extras.ubuntu.com/ubuntu $release main
deb $url $release main universe restricted multiverse
deb http://security.ubuntu.com/ubuntu/ $release-security restricted universe main multiverse
deb $url $release-updates restricted universe main multiverse
" > $file

notify-send "$file has been changed" "The old file has been put in $old_file"

exit 0
Run Code Online (Sandbox Code Playgroud)
  • 保存文件并关闭它。
  • 返回终端并运行:chmod +x ~/bin/change_sources.sh- 授予脚本的执行访问权限。
  • 仅用于测试,要运行新脚本,请输入 terminal ~/bin/change_sources.sh。它会给你一个错误,因为你无权编辑/etc/apt/sources.list. 所以,使用sudo ~/bin/change_sources.sh
  • 使用sudo crontab -e命令编辑 root 用户的 crontab 文件并添加以下行:
@hourly /home/$USER/bin/change_sources.sh  
#change $USER with your user name
Run Code Online (Sandbox Code Playgroud)
  • 我已经设置了每个小时的 cron 作业,但是您可以根据自己的意愿或认为更好的方式进行更改。从这个意义上说,请参阅http://en.wikipedia.org/wiki/Cron
  • 保存文件并检查新的 crontab 条目sudo crontab -l

注意:要恢复此脚本所做的更改,请删除 cron 作业并按照上图中的指示操作或在终端中使用下一个命令:

cp -f /etc/apt/sources.list.bak /etc/apt/sources.list
Run Code Online (Sandbox Code Playgroud)

从现在开始,该文件将在发现 IP 地址更改后动态更改。

这可能不是最好的解决方案,但在我看来,可以像上面的脚本那样以这种方式给出一个好的解决方案

  • 我想我会争论是否可以将 Linux 上的 gui 视为传统方式。:) 但无论如何:你的第一个脚本解决了一个问题,我没有。镜像语法已经得到了我的德语镜像,或美国或任何人们所在的地方。第二个脚本使用国家/地区镜像——它们似乎具有最少的可用带宽(在启动板上的存档镜像列表中看到),第三个脚本假设快速(或最近的)镜像确实是最好的。回到原点。我现在正在编写一个小的 `apt-spy` 脚本。以后会在这里更新。 (2认同)
  • 我不认为这是题外话。各种 Canonical/Ubuntu 人告诉我在这里添加我的问题。这就是我首先来到这里的原因,我猜也是为什么分配赏金的原因。 (2认同)

Til*_*ill 4

我感谢有关这个问题的所有意见,但由于没有人提出适合我们情况的简单解决方案,因此我决定自己解决这个问题。

\n\n

我创建了一个工具(专门用于 Ubuntu),我称之为apt-spy2.

\n\n

该工具的主要目标是快速找到工作镜。工作的定义是镜像服务器可用并且(希望是:)是最新的。

\n\n

我不假设所选服务器是否一定是最近且最快的。我没有执行任何 ping 或 GEO DNS 技巧 \xe2\x80\x94 但到目前为止,当出现问题时,这会起作用。

\n\n

简而言之,它是如何工作的 \xe2\x80\x94:

\n\n
    \n
  1. 我使用http://mirrors.ubuntu.com启动板的镜像列表来检索服务器。
  2. \n
  3. 我对每个(HTTP 响应状态代码)进行了简单的检查。
  4. \n
  5. LBNL,我更新了/etc/apt/sources.list
  6. \n
\n\n

请注意:这假设人们玩得很好并放置了额外的镜像(例如将第 3 方存储库放入/etc/apt/sources.list.d. 但我想这意味着还有改进的空间。

\n\n

您可以像这样获取这个工具:

\n\n

\n$ [sudo] gem install apt-spy2\n

\n\n

cli 附带listcheck和(带有有关如何使用它的扩展信息)fixhelp

\n\n

我尝试在项目的README中尽可能多地记录。

\n\n

目前的版本是非常保守的0.5.0

\n\n

该代码是开源的,并且许可证是自由的。我接受所有的贡献。

\n


归档时间:

查看次数:

18630 次

最近记录:

5 年,11 月 前