部署rsync+inotify同步/chengli同步至目标服务器的/kongbai下
环境说明
服务器类型 | IP地址 |
---|---|
源服务器 | 192.168.88.129 |
目标服务器 | 192.168.88.128 |
1.关闭源服务器和目标服务器的防火墙和SELINUX
1.1源服务器
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# getenforce
Disabled
1.2目标服务器
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# getenforce
Disabled
2.源服务器和目标服务器安装rsync
[root@localhost ~]# yum install rsync -y
3.目标服务器配置
3.1创建用户认证文件
[root@localhost run]# echo 'kongbai:cl1996.'>/etc/rsync.pass
[root@localhost run]# cat /etc/rsync.pass
root:wscl1996.
3.2修改rsyncd.conf配置文件
[root@localhost ~]# cat >> /etc/rsyncd.conf <<EOF
> log file = /var/log/rsyncd.log
> pidfile = /var/run/rsyncd.pid
> lock file = /var/run/rsync.lock
> secrets file = /etc/rsync.pass
> [kongbai]
> path = /kongbai/
> comment = sync etc from client
> uid = root
> gid = root
> port = 873
> ignore errors
> use chroot = no
> read only = no
> list = no
> max connections = 200
> timeout = 600
> auth users = root
> EOF
3.3设置文件权限
[root@localhost ~]# chmod 600 /etc/rsync*
[root@localhost ~]# ll /etc/rsync*
-rw------- 1 root root 793 9月 18 17:23 /etc/rsyncd.conf
-rw------- 1 root root 16 9月 18 17:16 /etc/rsync.pass
3.4启动rsync服务并设置为开机自启
[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
4.源服务器配置
4.1安装rsync服务端软件
[root@localhost ~]# yum install epel-release -y
4.2创建认证密码文件,并设置权限为600
[root@localhost ~]# echo 'wscl1996.' > /etc/rsync.pass
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw------- 1 root root 8 9月 18 17:32 /etc/rsync.pass
4.3创建测试目录,运行命令
[root@localhost ~]# mkdir -pv /root/chengli
mkdir: 已创建目录 "/root/chengli"
[root@localhost chengli]# rsync -avH --port 873 --progress --delete /root/chengli/ [email protected]::kongbai --password-file=/etc/rsync.pass
sending incremental file list
./
.inotify.sh.swp
4,096 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=4/6)
abc
20 100% 19.53kB/s 0:00:00 (xfr#2, to-chk=3/6)
nohup.out
151 100% 147.46kB/s 0:00:00 (xfr#3, to-chk=2/6)
123/
aaaa/
sent 4,586 bytes received 92 bytes 9,356.00 bytes/sec
total size is 4,267 speedup is 0.91
4.4安装inotify-tools
[root@localhost ~]# yum install inotify-tools
4.5配置同步脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch inotify.sh
[root@localhost scripts]# chmod 755 inotify.sh
[root@localhost scripts]# vim inotify.sh
host=192.168.88.129
src=/root/chengli
des=kongbai
password=/etc/rsync.pass
user=kongbai
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $u
ser@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
4.6启动脚本
[root@localhost scripts]# nohup bash /scripts/inotify.sh &
[1] 59758
[root@localhost scripts]# ps -ef | grep inotify
root 59858 2266 0 18:28 pts/0 00:00:00 vim inotify.sh
root 59978 2266 0 18:34 pts/0 00:00:00 bash /scripts/inotify.sh
root 59979 59978 0 18:34 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/chengli
root 59980 59978 0 18:34 pts/0 00:00:00 bash /scripts/inotify.sh
root 59982 2266 0 18:34 pts/0 00:00:00 grep --color=auto inotify
5.查看效果
5.1源服务器
[root@localhost ~]# cd chengli/
[root@localhost chengli]# ls
123 aaaa abc nohup.out
5.2目标服务器
[root@localhost tmp]# cd /kongbai/
[root@localhost kongbai]# ls
123 aaaa abc nohup.out
5.3源服务器
[root@localhost chengli]# mkdir chengl
[root@localhost chengli]# ls
111 1111 aaaa chengl nohup.out
[root@localhost chengli]# touch xixi
5.4目标服务器
[root@localhost chengli]# ls
111 1111 aaaa chengl nohup.out xixi
6.设置脚本开机自动启动
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 473 Aug 10 23:23 /etc/rc.d/rc.local
[root@localhost ~]# vim /etc/rc.d/rc.local
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh 添加这一行