Difference between revisions of "Local Mirror (简体中文)"

From ArchWiki
Jump to: navigation, search
({{out of date}})
m (替代方法)
(40 intermediate revisions by 14 users not shown)
Line 1: Line 1:
[[Category:Package Management]]
+
[[Category:Package management (简体中文)]]
{{i18n_links_start}}
+
[[Category:简体中文]]
{{i18n_entry|English|Local Mirror}}
+
[[en:Local Mirror]]
{{i18n_entry|中文(简体)|本地镜像}}
+
[[ru:Local Mirror]]
{{i18n_links_end}}
+
{{translateme (简体中文)|Article has been updated. To be translated.}}
{{out of date}}
+
  
===如何设置一个本地镜像===
+
{{警告|如果你想创建一个官方镜像,参考 [https://wiki.archlinux.org/index.php/DeveloperWiki:NewMirrors 这个页面]。}}
  
这篇文章将讲述如何在你的本地机器上面创建一个包含 "current" 和 "extra" 所有文件的Arch镜像,以及如何使其定时更新和如何设置使得pacman使用这个镜像。
+
==STOP==
  
* 当然,首先确定你的Arch是最新的:
+
{{警告|通常不赞成创建一个本地镜像,因为需要很大的带宽。其中一个替代的方法也许会满足你的要求。请查看下面的替代方法。}}
  <pre>
+
  pacman -Syu
+
  </pre>
+
  
* 安装rsync:
+
====替代方法====
  <pre>
+
*[[Pacman_Tips_(简体中文) #.E5.9C.A8.E7.BD.91.E7.BB.9C.E4.B8.8A.E5.85.B1.E4.BA.ABpacman.E7.BC.93.E5.AD.98|在网络上共享pacman缓存]]
  pacman -S rsync
+
  </pre>
+
  
* 创建一个用来保存镜像文件的文件夹。到2005年3月,"current" 需要 500 MB 左右的空间,"extra" 大概需要 2.2 GB。如果你希望的话,你也可以镜像"testing" (800 MB) 和 "unstable" (350 MB). 同时修改权限,让一个普通用户(本例中为 "chris")作为属主。
+
==本地镜像==
  <pre>
+
  mkdir -p /mirror/{current,extra}
+
  # mkdir /mirror/{testing,unstable}
+
  chown -R chris /mirror
+
  </pre>
+
  
* 使用你最喜欢的编辑器,创建一个包含下面内容的名为 /mirror/sync.sh 的文件。 如果你希望镜像 "testing" 和 "unstable" ,去掉相应行的注释符号(即#)。或许你还需要调整其中的镜像的地址为和你地理位置比较近的。
+
===需要牢记的事实===
  <pre>
+
* 镜像的带宽不是免费的,镜像必须为提供给你的数据付费
  #!/bin/sh
+
** 尽管你已经为你的ISP付费,这也是成立的。
  rsync -avz --delete rsync.archlinux.org::current /mirror/current/
+
* 有很多你可能下载了却永远用不到的包
  rsync -avz --delete rsync.archlinux.org::extra /mirror/extra/
+
* 镜像的管理员会更喜欢让你仅下载需要的包
  # --delete to delete old files remove it if you want to keep them
+
* 再次请求仔细查看上面的替代方案
  # rsync -avz --delete rsync.archlinux.org::ftp/testing /mirror/
+
  # rsync -avz --delete rsync.archlinux.org::ftp/unstable /mirror/
+
  </pre>
+
  
* 使得新创建的文件可执行:
+
'''如果你完全确定本地镜像是唯一合适的解决方案,那么按照下面的指引操作。'''
  <pre>
+
  chmod a+x /mirror/sync.sh
+
  </pre>
+
  
* 确定已经安装了 dcron (应该已经安装好了):
+
===服务器设置===
  <pre>
+
====Building Rsync Command====
  pacman -Q dcron
+
* Use the rsync arguments from [https://wiki.archlinux.org/index.php/DeveloperWiki:NewMirrors DeveloperWiki:NewMirrors]
  </pre>
+
* Select a server from the above article
 +
* Exclude folder/files you do not want by including {{Ic|1=--exclude-from="/path/to/exclude.txt"}} in the rsync arguments. Example contents might include:
 +
<pre>
 +
iso
  
* 创建一个定时执行的脚本,内容包含下面所示,文件名为 /etc/cron.daily/sync ,并且替换 "chris" 为你系统中的一个普通用户:
+
#Exclude i686 Packages
  <pre>
+
*/os/i686
  #!/bin/sh
+
pool/*/*-i686.pkg.tar.xz
  SYNCLOGFILE="/var/log/sync.log"
+
pool/*/*-i686.pkg.tar.gz
  SYNCLOCKFILE="/var/lock/sync.lock"
+
  
  if [[ -f $SYNCLOCKFILE ]]; then
+
#Exclude x86_64 Packages
    # lock file already present, bail
+
*/os/x86_64
    exit 1
+
pool/*/*-x86_64.pkg.tar.xz
  fi
+
pool/*/*-x86_64.pkg.tar.gz
 +
</pre>
  
  echo -n ">>> Sync log for " > $SYNCLOGFILE
+
* All packages reside in the pool directory. Symlinks are then created from pool to core/extra/testing/etc..
  date >> $SYNCLOGFILE
+
** As of 9/21/2010 this migration is not yet complete.
 +
*** There may be actual packages, instead of symlinks, in ${repo}/os/${arch}
 +
* Exclude any top-level directories that you do not need
  
  cd /mirror
+
Example: {{Ic|1=rsync ''$rsync_arguments'' --exclude="/path/to/exclude.txt" ''rsync://example.com/'' /path/to/destination}}
  touch $SYNCLOCKFILE
+
  su - chris -c "/mirror/sync.sh" >> $SYNCLOGFILE
+
  rm -f $SYNCLOCKFILE
+
  
  </pre>
+
====Example Script====
  
* 设置权限:
+
{{Warning|DO NOT USE THIS SCRIPT UNLESS YOU HAVE READ WARNINGS AT THE START OF THIS ARTICLE}}
  <pre>
+
{{Warning|Only use this script to sync Core/Extra/Community! If you need Testing, gnome-unstable or any other repo, use rsync --exclude instead!}}
  chmod 744 /etc/cron.daily/sync
+
  </pre>
+
  
此后镜像将在每天的 00:02 AM 进行同步。剩下的内容将讲述如何使本机使用这个镜像。
+
Yes, this script is partially broken '''ON PURPOSE''' to avoid people doing copy-and-paste to create their own mirror. It should be easy to fix if you REALLY want a mirror.
  
* 修改 /etc/pacman.d/current 文件使其内容类似下面:
+
#!/bin/bash
   <pre>
+
  #
+
#################################################################################################
   # CURRENT: Arch Linux core repository
+
### It is generally frowned upon to create a local mirror due the bandwidth that is required.
  #
+
### One of the alternatives will likely fulfill your needs.
  [[current]]
+
### REMEMBER:
  Server = file:///mirror/current/os/i686
+
###   * Bandwidth is not free for the mirrors. They must pay for all the data they serve you
  Server = ftp://ftp.archlinux.org/current/os/i686
+
###      => This still applies although you pay your ISP
  # Also include the rest of the mirrors if you want
+
###      => There are many packages that will be downloaded that you will likely never use
  </pre>
+
###      => Mirror operators will much prefer you to download only the packages you need
 +
###   * Really please look at the alternatives on this page:
 +
###      https://wiki.archlinux.org/index.php?title=Local_Mirror
 +
### If you are ABSOLUTELY CERTAIN that a local mirror is the only sensible solution, then this
 +
### script will get you on your way to creating it.
 +
#################################################################################################
 +
 +
# Configuration
 +
SOURCE='rsync://mirror.example.com/archlinux'
 +
DEST='/srv/mirrors/archlinux'
 +
BW_LIMIT='500'
 +
REPOS='core extra'
 +
RSYNC_OPTS="-rtlHq --delete-after --delay-updates --copy-links --safe-links --max-delete=1000 --bwlimit=${BW_LIMIT} --delete-excluded --exclude=.*"
 +
LCK_FLE='/var/run/repo-sync.lck'
 +
 +
# Make sure only 1 instance runs
 +
if [ -e "$LCK_FLE" ] ; then
 +
OTHER_PID=`/bin/cat $LCK_FLE`
 +
echo "Another instance already running: $OTHER_PID"
 +
exit 1
 +
fi
 +
echo $$ > "$LCK_FLE"
 +
 +
for REPO in $REPOS ; do
 +
echo "Syncing $REPO"
 +
echo /usr/bin/rsync $RSYNC_OPTS ${SOURCE}/${REPO} ${DEST}
 +
done
 +
 +
# Cleanup
 +
/bin/rm -f "$LCK_FLE"
 +
 +
exit 0
  
* 修改 /etc/pacman.d/extra 文件使其内容类似下面:
 
  <pre>
 
  #
 
  # The Extra Repository
 
  #
 
  [[extra]]
 
  Server = file:///mirror/extra/os/i686
 
  Server = ftp://ftp.ibiblio.org/pub/linux/distributions/archlinux/extra/os/i686
 
  # Also include the rest of the mirrors if you want
 
  </pre>
 
  
* 对 "testing" 和 "unstable" 做相同的操作如果你对它们也进行了镜像。
+
====Another mirror script using lftp====
 +
lftp can mirror via several different protocols: ftp, http, etc. It also restarts on error, and can run in the background. Put this into your $PATH for an easy way to mirror that continues if you log out.
  
* 等镜像第一次同步结束之后 (检查 /var/log/sync.log 文件),试试看下面的命令是否能正常使用:
+
#!/usr/bin/lftp -f
  <pre>
+
lcd /local/path/to/your/mirror
  pacman -Sy
+
open ftp.archlinux.org (or whatever your favorite mirror is)
  </pre>
+
# Use 'cd' to change into the proper directory on the mirror, if necessary.
 +
mirror -cve -X *i686* core &
 +
mirror -cve -X *i686* extra &
 +
mirror -cve -X *i686* community &
 +
mirror -cve -X *i686* multilib &
 +
lcd pool
 +
cd pool
 +
mirror -cve -X *i686* community &
 +
mirror -cve -X *i686* packages &
  
* 输出应该类似下面:
+
if you want to see the current status of the mirror. open lftp on terminal and type 'attach <PID>'
  <pre>
+
  :: Synchronizing package databases...
+
  current                 [[/mirror/current/os/i686/]] 100%    LOCAL
+
  extra                    [[/mirror/extra/os/i686/]] 100%    LOCAL
+
  </pre>
+
  
就是这样了。
+
====Partial mirroring====
  
我非常感谢在IRC中提任何评论和建议,我的昵称是 busfahrer ,我在 irc.freenode.net 的 [[ArchChannel|#archlinux]] 频道。
+
Mirroring only some repositories is definitely not easy, due to the centralization of most packages in `pool/`.
 +
See [http://blog.invokk.net/2012/01/mirroring-only-some-repositories-of-archlinux/ this blog post] for an attempt at writing a script for this task.
  
 +
====Serving====
 +
*HTTP (LAN)
 +
**[[LAMP]]
 +
**[[Lighttpd]]
 +
*FTP (LAN)
 +
**[[vsftpd]]
 +
*Physical Media
 +
**Flash Drive
 +
**External HD
  
更多的内容,看看 [[Howto_Upgrade_via_Home_Network | 这里]]。
+
===Client Configuration===
 +
*Add the proper Server= variable in /etc/pacman.d/mirrorlist
 +
*For physical media (such as flash drive) the following can be used: Server = file:///mnt/media/repo/$repo/os/$arch (''where /mnt/media/repo is directory where local mirror located'')

Revision as of 14:10, 14 March 2013

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: Article has been updated. To be translated.
警告: 如果你想创建一个官方镜像,参考 这个页面

STOP

警告: 通常不赞成创建一个本地镜像,因为需要很大的带宽。其中一个替代的方法也许会满足你的要求。请查看下面的替代方法。

替代方法

本地镜像

需要牢记的事实

  • 镜像的带宽不是免费的,镜像必须为提供给你的数据付费
    • 尽管你已经为你的ISP付费,这也是成立的。
  • 有很多你可能下载了却永远用不到的包
  • 镜像的管理员会更喜欢让你仅下载需要的包
  • 再次请求仔细查看上面的替代方案

如果你完全确定本地镜像是唯一合适的解决方案,那么按照下面的指引操作。

服务器设置

Building Rsync Command

  • Use the rsync arguments from DeveloperWiki:NewMirrors
  • Select a server from the above article
  • Exclude folder/files you do not want by including --exclude-from="/path/to/exclude.txt" in the rsync arguments. Example contents might include:
iso

#Exclude i686 Packages
*/os/i686
pool/*/*-i686.pkg.tar.xz
pool/*/*-i686.pkg.tar.gz

#Exclude x86_64 Packages
*/os/x86_64
pool/*/*-x86_64.pkg.tar.xz
pool/*/*-x86_64.pkg.tar.gz
  • All packages reside in the pool directory. Symlinks are then created from pool to core/extra/testing/etc..
    • As of 9/21/2010 this migration is not yet complete.
      • There may be actual packages, instead of symlinks, in ${repo}/os/${arch}
  • Exclude any top-level directories that you do not need

Example: rsync $rsync_arguments --exclude="/path/to/exclude.txt" rsync://example.com/ /path/to/destination

Example Script

Warning: DO NOT USE THIS SCRIPT UNLESS YOU HAVE READ WARNINGS AT THE START OF THIS ARTICLE
Warning: Only use this script to sync Core/Extra/Community! If you need Testing, gnome-unstable or any other repo, use rsync --exclude instead!

Yes, this script is partially broken ON PURPOSE to avoid people doing copy-and-paste to create their own mirror. It should be easy to fix if you REALLY want a mirror.

#!/bin/bash

#################################################################################################
### It is generally frowned upon to create a local mirror due the bandwidth that is required.
### One of the alternatives will likely fulfill your needs.
### REMEMBER:
###   * Bandwidth is not free for the mirrors. They must pay for all the data they serve you
###       => This still applies although you pay your ISP 
###       => There are many packages that will be downloaded that you will likely never use
###       => Mirror operators will much prefer you to download only the packages you need
###   * Really please look at the alternatives on this page:
###       https://wiki.archlinux.org/index.php?title=Local_Mirror
### If you are ABSOLUTELY CERTAIN that a local mirror is the only sensible solution, then this
### script will get you on your way to creating it. 
#################################################################################################

# Configuration
SOURCE='rsync://mirror.example.com/archlinux'
DEST='/srv/mirrors/archlinux'
BW_LIMIT='500'
REPOS='core extra'
RSYNC_OPTS="-rtlHq --delete-after --delay-updates --copy-links --safe-links --max-delete=1000 --bwlimit=${BW_LIMIT} --delete-excluded --exclude=.*"
LCK_FLE='/var/run/repo-sync.lck'

# Make sure only 1 instance runs
if [ -e "$LCK_FLE" ] ; then
	OTHER_PID=`/bin/cat $LCK_FLE`
	echo "Another instance already running: $OTHER_PID"
	exit 1
fi
echo $$ > "$LCK_FLE"

for REPO in $REPOS ; do
	echo "Syncing $REPO"
	echo /usr/bin/rsync $RSYNC_OPTS ${SOURCE}/${REPO} ${DEST}
done

# Cleanup
/bin/rm -f "$LCK_FLE"

exit 0


Another mirror script using lftp

lftp can mirror via several different protocols: ftp, http, etc. It also restarts on error, and can run in the background. Put this into your $PATH for an easy way to mirror that continues if you log out.

#!/usr/bin/lftp -f
lcd /local/path/to/your/mirror
open ftp.archlinux.org (or whatever your favorite mirror is)
# Use 'cd' to change into the proper directory on the mirror, if necessary.
mirror -cve -X *i686* core &
mirror -cve -X *i686* extra &
mirror -cve -X *i686* community &
mirror -cve -X *i686* multilib &
lcd pool
cd pool
mirror -cve -X *i686* community &
mirror -cve -X *i686* packages &

if you want to see the current status of the mirror. open lftp on terminal and type 'attach <PID>'

Partial mirroring

Mirroring only some repositories is definitely not easy, due to the centralization of most packages in `pool/`. See this blog post for an attempt at writing a script for this task.

Serving

Client Configuration

  • Add the proper Server= variable in /etc/pacman.d/mirrorlist
  • For physical media (such as flash drive) the following can be used: Server = file:///mnt/media/repo/$repo/os/$arch (where /mnt/media/repo is directory where local mirror located)