【三】ubuntu仓库本地同步

 

目录

1 引言

2 ubuntu官方仓库

3 仓库参数意义

4 本地仓库创建工具

4.1 rsync

4.2 apt-mirror

5 设置定时同步

5.1 规则

参考


1 引言

        想象一下,1万名Linux开发者同时尝试利用互联网来获取源更新,但是源仓库的带宽只有100M,可以想象整个更新过程会有多么缓慢和拥挤。随着人群的增长,这一问题会愈发显著。
本文将寻求解决这一问题的方法,实际上所有的开发者都在进行同样的更新,这一切都是重复工作。所以如果能够将ubuntu的官方源同步到本地,那么更新速度将大幅提升,而损失的仅仅是第一次同步耗时与硬盘空间。

2 ubuntu官方仓库

        X86架构的ubuntu源:http://archive.ubuntu.com/ubuntu/

        ARM架构的ubuntu源:http://ports.ubuntu.com/ubuntu-ports/

3 仓库参数意义

       ubuntu中source.list的内容之一:

       http://ports.ubuntu.com/ubuntu-ports/ bionic main restricted universe multiverse

       下面将对格式进行解析:

        source.list中的每行都可以按照下图进行解析

       第三部分解释位:“软件安全性分” 类更加恰当。

       第四部分内容的解释:

  • main(主要的)--“main”组件包括了自由软件、可以被自由发布的软件和被Ubuntu团队完全支持的软件。其中包括了大多数流行的和稳定的开源软件,安装Ubuntu时默认安装的就是这些软件。
  • restricted(限制的)--“restricted”组件是专供普遍用途,而且没有自由软件版权,但依然被Ubuntu团队支持的软件。
  • universe(宇宙,世界,全人类)--“universe”组件是整个自由、开源Linux世界的缩影。在“universe”组件中,你可以找到大多数开源软件,以及在开源版权下的软件,所 有这些都是在公共源的基础上建立的。这些软件都是使用“main”中的组件编写的,它们能与“main”组件相安无事地共同运行,但它们没有安全升级的保 障。“universe”组件包含了数以千计的软件。虽然是公共的,用户必须明白它们与稳定的Ubuntu核心的软件的差异和不稳定。
  • multiverse(多元的)--multiverse”组件包含了“不自由”的软件,这意味着这些软件不满足 Ubuntu相对于“main”组件的各种版权政策。当您使用这些软件时,如何调整各种权力以及遵重版权所有者的问题,就完全依靠自己把握了。 这些软件不被ubuntu团队支持,而且通常不能被修改和更新,如果使用,自己将承担任何风险。
     

4 本地仓库创建工具

 

4.1 rsync

        该工具会将指定的仓库所有内容同步下来,无法指定架构,所以需要硬盘空间至少1T。

        创建一个sync.sh脚本,内容如下:

#!/bin/bash

STORAGE_PATH="/mirror-directory" #存储路径

rsync \
  --recursive \
  --links \
  --perms \
  --times \
  --compress \
  --progress \
  --delete \
  rsync://archive.ubuntu.com/ubuntu \ #源地址
  $STORAGE_PATH

      参数解释:

      -r, --recursive 对子目录以递归模式处理
      -l, --links 保留软链结
      -p, --perms 保持文件权限
      -t, --times 保持文件时间信息
      -z, --compress 对备份的文件在传输时进行压缩处理
      --progress 显示备份过程
      --delete 删除那些DST中SRC没有的文件
      --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息

4.2 apt-mirror

      “apt-mirror” 是一个 Perl 开发的工具,用来从一个公开仓库中下载并镜像全部内容,当然该工具是支持同步指定架构的源。

      配置 apt-mirror 工具,主要关注以下内容:

  •       存储位置(base_path)
  •      下载的线程数(nthreads)
  •      需要下载的版本和架构

      上述配置在/etc/apt/mirror.list文件中完成,当然也能够指定其他位置的mirror.list。
在安装工具的过程中,会生成一个缺省版本,我们需要在这基础上对上述参数进行调整。

      下面列出一个完整的配置文件,它将会镜像 Ubuntu 8.04 LTS 的 32 和 64 位版本,arm64和armhf架构进行同步。这需要接近 500G 的存储空间,
设置中指定把这些内容同步到 /media/STORAGE/ 中。

############# config ##################
#
set base_path    /media/STORAGE
#
# set mirror_path  $base_path/mirror
# set skel_path    $base_path/skel
# set var_path     $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch  arm64 armhf
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############

# for bionic
deb-arm64 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main restricted universe multiverse
deb-armhf https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main restricted universe multiverse
deb-arm64 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb-armhf https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb-arm64 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb-armhf https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb-arm64 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse
deb-armhf https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse

#for focal
deb-arm64 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb-armhf https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb-arm64 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb-armhf https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb-arm64 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb-armhf https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb-arm64 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
deb-armhf https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse

clean https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/

  配置完成之后,就可以开始用下面的命令来同步镜像了:
  apt-mirror  (可以指定其他位置的配置文件)

5 设置定时同步

  crontab是用来定期执行程序的命令

  参数说明:
  -e : 执行文字编辑器来设定时程表,内定的文字编辑器是 VI,如果你想用别的文字编辑器,则请先设定 VISUAL 环境变数来指定使用那个文字编辑器(比如说 setenv VISUAL joe)
  -r : 删除目前的时程表
  -l : 列出目前的时程表

  输入命令 crontab -e

  填写内容:

# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
*/1 * * * * /where/directory/test.sh

如:6小时同步一次,则为:0 */6 * * * /where/directory/test.sh 

test.sh脚本中的内容就是同步命令。

5.1 规则

时间格式如下:
f1 f2 f3 f4 f5 program

其中 f1 是表示分钟,f2 表示小时,f3 表示一个月份中的第几日,f4 表示月份,f5 表示一个星期中的第几天。program 表示要执行的程序。
当 f1 为 * 时表示每分钟都要执行 program,f2 为 * 时表示每小时都要执行程序,其馀类推
当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要执行,f2 为 a-b 时表示从第 a 到第 b 小时都要执行,其此类推
当 f1 为 */n 时表示每 n 分钟个时间间隔执行一次,f2 为 */n 表示每 n 小时个时间间隔执行一次,其此类推
当 f1 为 a, b, c,... 时表示第 a, b, c,... 分钟要执行,f2 为 a, b, c,... 时表示第 a, b, c...个小时要执行,其此类推
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- 星期中星期几 (0 - 6) (星期天 为0)
|    |    |    +---------- 月份 (1 - 12) 
|    |    +--------------- 一个月中的第几天 (1 - 31)
|    +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)
使用者也可以将所有的设定先存放在文件中,用 crontab file 的方式来设定执行时间,执行命令为:crontab [ -u user ] file

参考

https://www.jianshu.com/p/84d07a78cd0f

https://blog.programster.org/deploy-a-local-ubuntu-mirror-using-rsync

https://www.runoob.com/linux/linux-comm-crontab.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值