backup服务器
1)服务端配置

[root@backup ~]#yum -y install rsync
[root@backup ~]#cp /etc/rsyncd.conf{,.ori}
[root@backup ~]#ll /etc/rsyncd.conf{,.ori}

cat > /etc/rsyncd.conf<<EOF
#rsync_config----start
#created by oldboy
#site:https://www.shnne.com
uid = rsync
gid = rsync
use chroot = no
fake super = yes
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = welcome to oldboyedu backup!
path = /backup/

[data]
path = /data/
EOF

2)创建用户和目录

[root@backup ~]$mkdir -p /backup
[root@backup ~]$ls -d /backup
[root@backup ~]useradd -s /sbin/nologin -M rsync
[root@backup ~]$id rsync
[root@backup ~]$chown -R rsync.rsync /backup/
[root@backup ~]$ll -d /backup/

[root@backup ~]$mkdir -p /data
[root@backup ~]$chown rsync.rsync data/ -R

3)添加配置文件

[root@backup ~]$echo "rsync_backup:oldboy123" >/etc/rsync.password
[root@backup ~]$cat /etc/rsync.password
rsync_backup:oldboy123
[root@backup ~]$chmod 600 /etc/rsync.password
[root@backup ~]$ll /etc/rsync.password

4)启动服务

C6启动
rsync --daemon

C7启动
[root@backup ~]$systemctl start rsyncd
[root@backup ~]$systemctl enable rsyncd
[root@backup ~]$systemctl status rsyncd
[root@backup ~]$netstat -lntup|grep 873
[root@backup ~]$lsof -i :873

客户端配置【NFS服务器上配置】

第一种方法(推荐吧)

[root@nfs01 ~]$echo "oldboy123" > /etc/rsync.password
[root@nfs01 ~]$cat /etc/rsync.password
oldboy123
[root@nfs01 ~]$chmod 600 /etc/rsync.password
[root@nfs01 ~]$ll /etc/rsync.password

第二种方法

[root@nfs01 ~]$echo 'export RSYNC_PASSWORD=oldboy123' >> /etc/bashrc
[root@nfs01 ~]$tail -1 /etc/bashrc
export RSYNC_PASSWORD=oldboy123
[root@nfs01 ~]$. /etc/bashrc
[root@nfs01 ~]$echo $RSYNC_PASSWORD
oldboy123

测试

拉取实践
[root@nfs01 ~]$rsync -avz rsync_backup@172.16.1.41::backup /data --password-file=/etc/rsync.password
[root@nfs01 ~]$rsync -avz rsync://rsync_backup@172.16.1.41/backup /data --password-file=/etc/rsync.password
推送实践
[root@nfs01 ~]$rsync -avz /data rsync_backup@172.16.1.41::backup  --password-file=/etc/rsync.password
[root@nfs01 ~]$rsync -avz /data rsync://rsync_backup@172.16.1.41/backup  --password-file=/etc/rsync.password

inotify-tools工具安装过程【NFS服务器上配置】

[root@nfs01 ~]# yum install epel-release -y #如已装,请忽略

[root@nfs01 ~]# yum install inotify-tools -y
[root@nfs01 ~]# rpm -ql inotify-tools|head -2
/usr/bin/inotifywait
/usr/bin/inotifywatch

inotifywait命令参数
-m 始终保持事件监听状态*****
-d 类似于-m *****
-r 递归监控根目录数据信息变化*****
-e 监听的事件

实践

#测试create事件
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
#再开一个窗口测试
touch {1..5}.txt

#测试delete
inotifywait -mrq -e delete /data

#多个参数监控
inotifywait -mrq -e delete,create,close_write /data
inotifywait -mrq -e delete,close,move /data

inotify-tools软件部署项目实战

inotifywait -mrq --format '%w%f' -e close_write,delete /data
inotifywait -mrq -e close_write,delete /data

生产脚本nfs01创建执行执行【建议用sersync的方式】

[root@nfs01 scripts]$cat monitor.sh 
#!/bin/bash
cmd="/usr/bin/inotifywait"
$cmd -mrq -e close_write,create,delete --format '%w%f' /data | \
while read line
do
 cd /data &&\
 rsync -az --delete ./ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
done
#测试
sh -x monitor.sh 

#bacup服务器检查
ls /backup
#放到/etc/rc.local
/bin/sh /server/scripts/monitor.sh &>/dev/null &

inotify-tools优化

#放到/etc/rc.local下
echo "50000000" > /proc/sys/fs/inotify/max_user_watches #单进程可以监控的文件数量
echo "50000000" > /proc/sys/fs/inotify/max_queued_events #队列容纳的事件数量

sersync+rsync项目实践

在nfs01上安装配置sersync

mkdir /server/tools
cd /server/tools/
#wget https://github.com/wsgzao/sersync/blob/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
wget https://www.shnne.com/zb_users/upload/2022/11/sersync_oldboy_64bit_20160928.tar.gz

[root@nfs01 tools]# tar xf sersync_oldboy_64bit_20160928.tar.gz
[root@nfs01 tools]# mv application/sersync /application/

启动sersync

#帮助
/application/sersync/bin/sersync -h
#确定好配置文件里的内容,把backup改为data
    <sersync>
        <localpath watch="/data">
            <remote ip="172.16.1.41" name="data"/>
            <remote ip="172.16.1.41" name="oldboy"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>

#启动
[root@nfs01 ~]# /application/sersync/bin/sersync -r -d -o /application/sersync/conf/confxml.xml

结束进程

[root@nfs01 ~]# pkill sersync

配置systemd启动

配置:systemctl start sersync启动方案
https://blog.51cto.com/oldboy/2155931

[root@nfs01 /data]# tail -1 /etc/rc.local
/application/sersync/bin/sersync -d

配置:systemctl start sersync启动方案
https://blog.51cto.com/oldboy/2155931

[root@nfs01 /data]# cat /etc/rc.d/init.d/sersync
#!/bin/bash
# chkconfig: 2345 21 81
# description: rsync service start and stop scripts
# Author: oldboy
# Organization: www.oldboyedu.com

start(){
    /application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml &>/dev/null
}
stop(){
    killall sersync 2>/dev/null
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 2
        start
        ;;
    *)
        echo $"Usage:$0 {start|stop|restart}"
        exit 1
esac

chmod +x /etc/rc.d/init.d/sersync

[root@nfs01 /data]# cat /usr/lib/systemd/system/sersync.service
[Unit]
Description=sersyncd service
After=network.target
 
[Service]
Type=forking           
ExecStart=/etc/rc.d/init.d/sersync start   
ExecReload=/etc/rc.d/init.d/sersync restart
ExecStop=/etc/rc.d/init.d/sersync stop     
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

chmod +x /usr/lib/systemd/system/sersync.service

[root@nfs01 /data]# systemctl enable sersync.service
Created symlink from /etc/systemd/system/multi-user.target.wants/sersync.service to /usr/lib/systemd/system/sersync.service.

[root@nfs01 /data]# systemctl status sersync.service
   sersync.service - sersyncd service
   Loaded: loaded (/usr/lib/systemd/system/sersync.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
   
[root@nfs01 /data]# systemctl stop sersync
[root@nfs01 /data]# ps -ef|grep sersync|grep -v grep
[root@nfs01 /data]# systemctl start sersync