rsync

rsync

环境说明:

服务器类型应用主机名
源服务器rsync
inotify-tools
rsync-daemon
脚本
root@source
目标服务器rsync
rsync-daemon
root@dest
[root@localhost ~]# hostnamectl set-hostname source
[root@localhost ~]# bash
[root@source ~]# 

[root@localhost ~]# hostnamectl set-hostname dest
[root@localhost ~]# bash
[root@dest ~]# 

需求:

  • 把源服务器上/etc目录实时同步到目标服务器的/tmp/下

在目标服务器上做以下操作:

//关闭防火墙与SELINUX
[root@dest ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@dest ~]# setenforce 0
[root@dest ~]# vim /etc/selinux/config
[root@dest ~]# dnf -y install rsync
[root@dest ~]# dnf list all|grep rsync
rsync.x86_64                                           3.1.3-12.el8                                           @base     
libguestfs-rsync.x86_64                                1:1.40.2-28.module_el8.5.0+821+97472045                AppStream 
rsync-daemon.noarch                                    3.1.3-12.el8                                           base      
[root@dest ~]# dnf -y install rsync-daemon

[root@dest ~]# vim /etc/rsyncd.conf

log file = /var/log/rsyncd.log 
pidfile = /var/run/rsyncd.pid     
lock file = /var/run/rsync.lock
secrets file = /etc/.runtime

[20220321]         
path = /tmp/  
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 = admin  
~                 

[root@dest ~]# vim /etc/.runtime

admin:123456

[root@dest ~]# chmod 600 /etc/.runtime
[root@dest ~]# ll /etc/.runtime
-rw-------. 1 root root 13 Aug  9 22:46 /etc/.runtime

[root@dest ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@dest ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.servi>
   Active: active (running) since Tue 2022-08-09 23:41:>
 Main PID: 11266 (rsync)
    Tasks: 1 (limit: 11216)
   Memory: 308.0K
   CGroup: /system.slice/rsyncd.service
           └─11266 /usr/bin/rsync --daemon --no-detach

Aug 09 23:41:49 dest systemd[1]: Started fast remote fi>

在源服务器上做以下操作:

//关闭防火墙与SELINUX
[root@source ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@source ~]# setenforce 0
[root@source ~]# vim /etc/selinux/config
[root@source ~]# dnf -y install epel-release

//安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@source ~]# dnf -y install rsync

//创建认证密码文件
[root@source ~]# echo '123456' > /etc/.hehe

[root@source ~]# chmod 600 /etc/.hehe
[root@source ~]# mkdir -pv /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'
[root@source ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.163.138::20220321 --password-file=/etc/.hehe
sending incremental file list
deleting vmware-root_981-4290756372/
deleting vmware-root_1015-4281777838/
deleting .font-unix/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
deleting ks-script-tb3fadow
deleting ks-script-1nz9nvhy
./
test/

sent 77 bytes  received 206 bytes  566.00 bytes/sec
total size is 0  speedup is 0.00

对面效果:

[root@dest ~]# cd /tmp/
[root@dest tmp]# ls
ks-script-1nz9nvhy  vmware-root_1015-4281777838
ks-script-tb3fadow  vmware-root_981-4290756372
[root@dest tmp]# ls
test
//安装inotify-tools
[root@source ~]# dnf -y install inotify-tools

//写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下 文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@source ~]# mkdir /scripts
[root@source ~]# touch /scripts/inotify.sh
[root@source ~]# chmod +x /scripts/inotify.sh
[root@source ~]# ll /scripts/inotify.sh
-rwxr-xr-x 1 root root 0 Aug 10 00:19 /scripts/inotify.sh
[root@source ~]# vim /scripts/inotify.sh

host=192.168.163.138      
src=/etc 
des=20220321
password=/etc/.hehe
user=admin          
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 $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

[root@source ~]# nohup bash /scripts/inotify.sh &
[1] 2949
[root@source ~]# nohup: ignoring input and appending output to 'nohup.out'

[root@source ~]# echo 'hello world' > /etc/abc

[root@dest tmp]# cat etc/abc
hello world

//查看inotify生成的日志
[root@source ~]# ls /tmp/
ks-script-ginfxusd
ks-script-u89l_pw4
rsync.log
vmware-root_1003-4290756405
vmware-root_1008-2965579127
vmware-root_1010-2957124853
vmware-root_980-2957518026
[root@source ~]# tail /tmp/rsync.log 
20220810 00:42 /etc/.heheATTRIB was rsynced
20220810 00:44 /etc/abcCREATE was rsynced
20220810 00:44 /etc/abcMODIFY was rsynced

设置脚本开机自动启动:

[root@source ~]# vim /etc/rc.local

#!/bin/bash
nohup /bin/bash /scripts/inotify.sh 
......

[root@source ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Sep 23  2021 /etc/rc.local -> rc.d/rc.local
[root@source ~]# chmod +x /etc/rc.d/rc.local 
[root@source ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Sep 23  2021 /etc/rc.local -> rc.d/rc.local
[root@source ~]# reboot
......

[root@source ~]# ps -ef |grep inotify
root        1034    1033  0 00:57 ?        00:00:00 /bin/bash /scripts/inotify.sh
root        1035    1034  0 00:57 ?        00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root        1036    1034  0 00:57 ?        00:00:00 /bin/bash /scripts/inotify.sh
root        1537    1505  0 00:58 pts/1    00:00:00 grep --color=auto inotify
[root@source ~]# echo 'xixi' > /etc/abc

[root@dest tmp]# cat etc/abc
xixi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值