shell脚本实现--rsync守护进程模式部署

准备A,B两台主机
1,在主机A上部署服务端

#!/bin/bash
rpm -qa|grep rsync &> /dev/null #查看是否存在rsync服务
if [ $? -eq 0 ];then
        echo "rsync服务已存在,自动修改配置文件"
else
        echo "正在安装rsync服务......"
        yum -y install rsync &> /dev/null
        echo "rsync服务安装完成,自动修改配置文件"
fi
cat>/etc/rsyncd.conf<<\EOF #编写配置文件
#全局配置
uid = root
gid = root
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
##模块配置
[backup]
path = /data
ignore errors
read only = false
list = false
hosts allow = 10.0.0.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
EOF
echo "" #相当于回车
mkdir -p /data  #创建rsync备份目录
useradd rsync -s /sbin/nologin -M  #授权rsync用户管理备份目录
chown -R rsync.rsync /data/ #修改备份目录权限
echo "rsync_backup:123456" >/etc/rsync.password #创建认证用户密码文件
chmod 600 /etc/rsync.password #修改文件权限
systemctl enable rsyncd.service &> /dev/null #添加开机自启
systemctl restart rsyncd.service
ps -ef |grep rsync|grep 'daemon' &> /dev/null #判断进程是否启动
if [ $? -eq 0 ];then
        echo "rsync服务已启动!"
else
        echo "rsync启动失败,请检查配置!"
fi
echo "rsync服务部署完毕!"

2,在主机B上部署客户端

#!/bin/bash
rpm -qa|grep rsync &> /dev/null #查看是否存在rsync服务
if [ $? -eq 0 ];then
        echo "rsync服务已存在,自动创建密码文件"
else
        echo "正在安装rsync服务......"
        yum -y install rsync &> /dev/null
        echo "rsync服务安装完成,自动创建密码文件"
fi
echo "123456">/etc/rsync.password #创建密码文件,客户端密码文件中,只需要密码即可
chmod 600 /etc/rsync.password #密码文件的权限是600
while :
do
        read -p "请输入向服务端推送的文件:" file #定义输入变量
        rsync -avz $file rsync_backup@10.0.0.41::backup --password-file=/etc/rsync.password &> /dev/null #在客户端向服务端推送文件
        read -p "是否继续推送文件(y/n)?" yes
        if [ $yes -eq y ];then
                continue
        else
                break
        fi
done

客户端在服务端拉取文件
#注意:rsync默认使用873端口,防火墙开启时,需放行端口

发布了68 篇原创文章 · 获赞 107 · 访问量 4359

猜你喜欢

转载自blog.csdn.net/xiaohuai0444167/article/details/105745379
今日推荐