linux使用rsync daemon 定时同步数据

@努力的小王  December 21, 2017

QQ截图20171221160427.png
服务端

this is rsyncdaemon script

author xw 2017.12.21

!/bin/bash

mkdir /backup/
filedir=/backup
if rpm -qa |grep rsync |wc -l > /dev/null;then
yum install -y rsync
fi
echo $?

检查rsync安装包是否存在

sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
service iptables stop
setenforce 0
chkconfig iptables off

安装准备

useradd rsync -s /sbin/nologin -M
chown rsync.rsync $filedir
cat > /etc/rsyncd.conf<<EOF

rsync.conf start

uid = rsync
gid = rsync

用户

use chroot = no

以chroot 方式运行

max connections = 200

最大连接数

timeout = 300

超时时间

pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log

进程文件锁文件日志文件位置

[backup]
path = $filedir
ignore errors
read only = false
list = false
hosts allow = 192.168.187.0/32
hosts deny = 0.0.0.0/32

允许和拒绝主机参数

auth user = rsync_backup
secrets file = /etc/rsync.password

认证

EOF

此处可以使用多模块

echo "rsync_backup:123456" >/etc/rsync.password
chmod 600 /etc/rsync.password

权限必须为600,否则日志会出现报错

rsync --daemon

此处使用daemon方式启动,当然也可以用脚本实现,脚本附下文

grep rsync /etc/rc.local|| echo '/usr/bin/rsync --daemon' >> /etc/rc.local
echo $?

客户端

this is rsyncdaemon clinet script

author xw 2017.12.21

!/bin/bash

mkdir /backup/
filedir=/backup
if rpm -qa |grep rsync |wc -l > /dev/null;then
yum install -y rsync
fi
echo $?

检查rsync安装包是否存在

sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
service iptables stop
setenforce 0
chkconfig iptables off

安装准备

echo “123456” >etc/rsync.password
chmod 600 /etc/rsync.password
cd /backup && touch zzzz{1..500}

进入backup目录创建文件

echo "/5 * root ./rsyncclient.sh /dev/null 2>&1">/etc/crontab
/etc/rc.d/init.d/crond restart

定时任务建好

echo "rsync -avz /$filedir/ rsync_backup@192.168.187.129::backup --password-file=/etc/rsync.password">rsyncclient.sh
chmod +x rsyncclient.sh
./rsyncclient.sh
echo $?

服务端 rsync 启动脚本

author xw 2017.12.21

!/bin/bash

chkconfig: 2345 20 80

description: create by vincen

. /etc/init.d/functions

function usage(){

    echo $"usage:$0 {start|stop|restart}"
    exit 1

}

function start(){

    rsync --daemon
    sleep 1
    if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ];then
            action "rsyncd is started." /bin/true
    else
            action "rsyncd is started." /bin/false
    fi

}

function stop(){

    killall rsync
    sleep 1
    if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ];then
            action "rsyncd is stopped." /bin/true
    else
            action "rsync is started." /bin/false
    fi

}

function main(){

    if [ $# -ne 1 ];then
            usage
    fi
    if [ "$1" == "start" ];then
            start
    elif [ "$1" == "stop" ];then
            stop
    elif [ "$1" == "restart" ];then
            stop
            sleep 1
            start
    else
            usage
    fi

}
main $*


添加新评论