搭建rsync服務

首先環境說明
| 服務器說明 | ip地址 | 應用 | 系統 |

服務器說明 ip地址 應用 系統
源服務器 192.168.100.100 rsync inotify tools腳本 red hat7
目標服務器 192.168.100.129 rsync centos7

1.部署rsync+inotify同步/dxk目錄至目標服務器/tmp/dxk下

部署rsync服務同步

首先環境說明

服務器說明 ip地址 應用 系統
源服務器 192.168.100.100 rsync inotify tools腳本 red hat7
目標服務器 192.168.100.129 rsync centos7

部署rsync+inotify同步/qinyong目錄至目標服務器/tmp/qinyong下
源服務器

一、關閉源服務器和目標服務器的防火牆和SELINUX

1.源服務器和目標服務器

[root@localhost ~]# systemctl stop firewalld(源服務器)
[root@localhost ~]# getenforce 
Disabled
[root@localhost ~]# systemctl stop firewalld(目標服務器)
[root@localhost ~]# getenforce
Disabled

二、安裝rsync

[root@localhost ~]# yum install rsync -y

三、目標服務器配置

1.創建用戶認證文件

[root@localhost run]# echo 'qinyong.'>/etc/rsync.pass
[root@localhost run]# cat /etc/rsync.pass 
root:qy5247479-

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
> [qinyong] 
> path = /nihao/
> 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.設置文件權限

[root@localhost ~]# chmod 600 /etc/rsync*
[root@localhost ~]# ll /etc/rsync*
-rw------- 1 root root 793 9月  19 10:03 /etc/rsyncd.conf
-rw------- 1 root root  16 9月  19 10:06 /etc/rsync.pass

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.

四、源服務器配置

1.安裝rsync服務端軟件

[root@localhost ~]# yum install epel-release -y

2.創建認證密碼文件,並設置權限爲600

[root@localhost ~]# echo 'qy5247479-' > /etc/rsync.pass
[root@localhost ~]# chmod 600 /etc/rsync.pass 
[root@localhost ~]# ll /etc/rsync.pass
-rw------- 1 root root 8 9月  19 10:32 /etc/rsync.pass

3.創建測試目錄,運行命令

[root@localhost ~]# mkdir -pv /root/nihao
mkdir: 已創建目錄 "/root/nihao"
[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/nihao/ [email protected]::nihao --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%   26.53kB/s    0:00:00 (xfr#2, to-chk=3/6)
nohup.out
            151 100%  230.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.安裝inotify-tools

[root@localhost ~]# yum install inotify-tools

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.100.129
src=/root/nihao
des=qinyong
password=/etc/rsync.pass
user=root
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

6.啓動腳本

[root@localhost scripts]# nohup bash /scripts/inotify.sh &
[1] 59758

五、驗證結果

1.源服務器

[root@localhost ~]# cd nihao/
[root@localhost nihao]# ls
55  da  hehe  nohup.out

2.目標服務器

[root@localhost tmp]# cd /qinyong/
[root@localhost qinyong]# ls
55  da  hehe  nohup.out

3.源服務器

[root@localhost nihao]# mkdir qinyong
[root@localhost nihao]# ls
dad  555  aaa nihao  nohup.out
[root@localhost nihaoi]# touch lala

4.目標服務器

[root@localhost qinyong]# ls
dad  555  aaa nihao  nohup.out lala
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章