shell实现部署ftp提供共享yum仓库

脚本实现自动化配置vsftp,并共享opt目录给其它节点机器实现离线源配置

使用脚本需修改以下内容:
ftp_ip为ftp服务器ip
reponame为repo文件名字
dir为仓库目录,默认opt下
houzui为opt目录下的离线仓库,写入到houzui中为数组
使用前还需保持服务器可以下载vsftp服务

脚本实现

#!/bin/bash

ftp_ip="192.168.200.80"
reponame="local.repo"
houzui=("centos" "iaas")
opt="/opt"

if [ "$(hostname -I | awk '{print $1}')" = "$ftp_ip" ]; then
    systemctl stop firewalld
    setenforce 0
    yum install -y vsftpd
    echo "anon_root=$opt" >> /etc/vsftpd/vsftpd.conf
    systemctl enable --now vsftpd
    systemctl restart vsftpd
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=file://$opt/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
else
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=ftp://$ftp_ip/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
fi

脚本测试

节点IP
节点1192.168.200.80
节点2192.168.200.81
节点3192.168.200.82

手动配置好离线仓库

[root@localhost ~]# mkdir /opt/{centos,iaas}
[root@localhost ~]# mount /dev/sr0 /opt/centos/
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# cp -rf /opt/centos/* /opt/iaas/

脚本部署

[root@localhost ~]# cat repo.sh
#!/bin/bash

ftp_ip="192.168.200.80"
reponame="local.repo"
houzui=("centos" "iaas")
opt="/opt"

if [ "$(hostname -I | awk '{print $1}')" = "$ftp_ip" ]; then
    systemctl stop firewalld
    setenforce 0
    yum install -y vsftpd
    echo "anon_root=$opt" >> /etc/vsftpd/vsftpd.conf
    systemctl enable --now vsftpd
    systemctl restart vsftpd
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=file://$opt/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
else
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=ftp://$ftp_ip/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
fi
[root@localhost ~]# hostname -I
192.168.200.80
[root@localhost ~]# bash repo.sh
[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=file:///opt/iaas
gpgcheck=0
enabled=1
[root@localhost ~]# yum clean all;yum repolist
Loaded plugins: fastestmirror
Cleaning repos: centos iaas
Cleaning up list of fastest mirrors
Other repos take up 195 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
centos                                                                                           | 3.6 kB  00:00:00
iaas                                                                                             | 3.6 kB  00:00:00
(1/4): centos/group_gz                                                                           | 153 kB  00:00:00
(2/4): centos/primary_db                                                                         | 3.3 MB  00:00:00
(3/4): iaas/group_gz                                                                             | 153 kB  00:00:00
(4/4): iaas/primary_db                                                                           | 3.3 MB  00:00:00
repo id                                                  repo name                                                status
centos                                                   centos                                                   4,070
iaas                                                     iaas                                                     4,070
repolist: 8,140
[root@localhost ~]#

从节点验证

[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=ftp://192.168.200.80/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=ftp://192.168.200.80/iaas
gpgcheck=0
enabled=1
[root@localhost ~]# yum clnen all;yum repolist
Loaded plugins: fastestmirror
No such command: clnen. Please use /usr/bin/yum --help
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                                  repo name                                                status
centos                                                   centos                                                   4,070
iaas                                                     iaas                                                     4,070
repolist: 8,140
[root@localhost ~]# hostname -I
192.168.200.81
[root@localhost ~]#
[root@localhost ~]# bash repo.sh
Loaded plugins: fastestmirror
Cleaning repos: centos iaas
Cleaning up list of fastest mirrors
Loaded plugins: fastestmirror
Determining fastest mirrors
centos                                                                                           | 3.6 kB  00:00:00
iaas                                                                                             | 3.6 kB  00:00:00
(1/4): iaas/group_gz                                                                             | 153 kB  00:00:00
(2/4): centos/group_gz                                                                           | 153 kB  00:00:00
(3/4): centos/primary_db                                                                         | 3.3 MB  00:00:00
(4/4): iaas/primary_db                                                                           | 3.3 MB  00:00:00
repo id                                                  repo name                                                status
centos                                                   centos                                                   4,070
iaas                                                     iaas                                                     4,070
repolist: 8,140
[root@localhost ~]# hostname -I
192.168.200.82
[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=ftp://192.168.200.80/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=ftp://192.168.200.80/iaas
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huhy~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值