歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 輕松建立 Ubuntu/Debian 源鏡像

輕松建立 Ubuntu/Debian 源鏡像

日期:2017/2/28 16:47:02   编辑:Linux教程

服務器上的源是 Debian 的,考慮到學校裡面已經沒有多少人使用 Debian,而使用 Ubuntu 來做推廣顯然更加合適,同學們遇到什麼問題,也可以得到更好的支持,於是決定把 Debian 源換成 Ubuntu 的。
建立源,當然要用 rsync ,不過這裡並非直接使用,而是用的 Debian 官網提供的一段腳本,叫做 anonftpsync,加上 cron 作為定時任務做成的。

cron 是 Linux 下的計劃任務工具,可以按每小時、每天、每星期、每月執行任務,支持多用戶多設置,很是方便。往下看之前,確認你安裝了 rsync,並且開啟了 cron 守護程序。

下面說一下步驟:
首先,建立源目錄。如果想放在 FTP 服務器上,應該建立在 FTP 目錄裡,比如:

mkdir /home/ftp/ubuntu

由於腳本的需要,最好建立一個文件夾來記錄最後同步的時間:
mkdir -p /home/ftp/ubuntu/project/trace/

然後建立腳本 anonftpsync。拷貝下面的腳本存放在穩妥位置。
#! /bin/sh -p
set -e

# EXCLUDE 是你要排除的文件和目錄。
EXCLUDE="--exclude *alpha.deb \
--exclude *alpha.udeb \
--exclude binary-alpha/ \
--exclude disks-alpha/ \
--exclude *-alpha.gz \
--exclude installer-alpha/ \
--exclude binary-arm/ \
--exclude *arm.deb \
--exclude *arm.udeb \
--exclude disks-arm/ \
--exclude *-arm.gz \
--exclude installer-arm/ \
--exclude binary-m68k/ \
--exclude *m68k.deb \
--exclude *m68k.udeb \
--exclude disks-m68k/ \
--exclude *-m68k.gz \
--exclude installer-m68k/ \
--exclude binary-hppa/ \
--exclude *hppa.deb \
--exclude *hppa.udeb \
--exclude disks-hppa/ \
--exclude *-hppa.gz \
--exclude installer-hppa/ \
--exclude binary-ia64/ \
--exclude *ia64.deb \
--exclude *ia64.udeb \
--exclude disks-ia64/ \
--exclude *-ia64.gz \
--exclude installer-ia64/ \
--exclude binary-mips/ \
--exclude *mips.deb \
--exclude *mips.udeb \
--exclude disks-mips/ \
--exclude *-mips.gz \
--exclude installer-mips/ \
--exclude binary-mipsel/ \
--exclude *mipsel.deb \
--exclude *mipsel.udeb \
--exclude disks-mipsel/ \
--exclude *-mipsel.gz \
--exclude installer-mipsel/ \
--exclude binary-s360/ \
--exclude *s360.deb \
--exclude *s360.udeb \
--exclude disks-s360/ \
--exclude *-s360.gz \
--exclude installer-s360/ \
--exclude binary-s390/ \
--exclude *s390.deb \
--exclude *s390.udeb \
--exclude disks-s390/ \
--exclude *-s390.gz \
--exclude installer-s390/ \
--exclude binary-sh/ \
--exclude *sh.deb \
--exclude *sh.udeb \
--exclude disks-sh/ \
--exclude *-sh.gz \
--exclude installer-sh/ \
--exclude binary-sparc/ \
--exclude *sparc.deb \
--exclude *sparc.udeb \
--exclude disks-sparc/ \
--exclude *-sparc.gz \
--exclude installer-sparc/ \
--exclude /Debian-1.3* \
--exclude /Debian3.1* \
--exclude local/ \
--exclude stable/ \
--exclude slink-proposed-updates/ \
--exclude slink/ \
--exclude bo/ \
--exclude bo-unstable/ \
--exclude bo-updates/ \
--exclude binary-hurd-i386/ \
--exclude *hurd-i386.deb \
--exclude *hurd-i386.udeb \
--exclude disks-hurd-i386/ \
--exclude *-hurd-i386.gz \
--exclude installer-hurd-i386/ \
--exclude binary-powerpc/ \
--exclude *powerpc.deb \
--exclude *powerpc.udeb \
--exclude disks-powerpc/ \
--exclude *-powerpc.gz \
--exclude installer-powerpc/ "
#--exclude /contrib/ --exclude /non-free/ --exclude source/\
# --exclude Incoming/ \
#######################################

# TO 是目標目錄
TO=/home/ftp/ubuntu
# 以下兩個合起來就是源地址: debian.ustc.edu.cn/ubuntu/
# 這裡設置同步的服務器域名
RSYNC_HOST=debian.ustc.edu.cn
# 這裡是同步服務器上源所在的目錄
RSYNC_DIR=ubuntu/

LOCK="${TO}/Archive-Update-in-Progress-`hostname -f`"

# Get in the right directory and set the umask to be group writable
#
cd $HOME
umask 002

# Check to see if another sync is in progress
if lockfile -! -l 43200 -r 0 "$LOCK"; then
echo `hostname` is unable to start rsync, lock file exists
exit 1
fi
trap "rm -f $LOCK > /dev/null 2>&1" exit

set +e

#result=1
#while (( $result != 0 )) ; do
rsync -rltv --progress --delete \
--exclude "Archive-Update-in-Progress-`hostname -f`" \
--exclude "project/trace/`hostname -f`" \
$EXCLUDE \
$RSYNC_HOST::$RSYNC_DIR $TO > ${HOME}/log/rsync.log 2>&1
#result=$?
#done

date -u > "${TO}/project/trace/`hostname -f`"
savelog ${HOME}/log/rsync.log > /dev/null 2>&1

設置權限。確定腳本可以被執行,目標目錄有寫權限。
chmod +x anonftpsync
chmod 755 /home/ftp/ubuntu

添加計劃任務。這裡使用 crontab 設置,-e 選項表示編輯設置:
crontab -e

輸入下面的一行,表示每天凌晨 2:00 運行 /usr/local/bin/anonftpsync,添加完畢保存:
00 2 * * *   /usr/local/bin/anonftpsync

使用下面的命令查看是否添加成功:
crontab -l

重啟 cron。更改完設置需要重啟 cron,通常是:
/etc/init.d/cron restart

Arch Linux 下是:
/etc/rc.d/crond restart

接下來就是等待咯,如果你迫不及待,可以直接運行腳本同步。
可以看到,建立一個 Ubuntu/Debian 源並沒有如何麻煩,這不得不歸功於 linux 下工具和腳本的強大,呵呵~
如果有教育網的同學,可以加這個源,現在還在同步中,呵呵~每天從中國科技大學同步一次。從這裡下載源列表:
ftp://tofree.org/sources.list/


如果對 cron 不夠明白,可以參考:計劃任務工具 cron 的配置和說明

本文來自http://ivenvd.blogspot.com/2009/05/ubuntudebian.html

Copyright © Linux教程網 All Rights Reserved