3.6 rsync

1. rsync簡介

rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地複製,或者與其他SSH、rsync主機同步。

  • 適用於小文件備份(大文件不適用)

2. rsync特性

rsync支持很多特性:

  • 可以鏡像保存整個目錄樹和文件系統
  • 可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等
  • 無須特殊權限即可安裝
  • 快速:第一次同步時rsync會複製全部內容,但在下一次只傳輸修改過的文件。rsync在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬
  • 安全:可以使用scp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接
  • 支持匿名傳輸,以方便進行網站鏡像

3. rsync的ssh認證協議

rsync命令來同步系統文件之前要先登錄remote主機認證,認證過程中用到的協議有2種:

  • ssh協議
  • rsync協議

rsync server端不用啓動rsync的daemon進程,只要獲取remote host的用戶名和密碼就可以直接rsync同步文件
rsync server端因爲不用啓動daemon進程,所以也不用配置文件/etc/rsyncd.conf

ssh認證協議跟scp的原理是一樣的,如果在同步過程中不想輸入密碼就用ssh-keygen -t rsa打通通道

//配置免密登錄
[root@yusyang ~]# ssh-keygen -t rsa
[root@yusyang ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

//這種方式默認是省略了 -e ssh 的,與下面等價:
rsync -avz /SRC -e ssh [email protected]:/DEST 
    -a  //文件宿主變化,時間戳不變
    -z  //壓縮數據傳輸
 
//當遇到要修改端口的時候,我們可以:
rsync -avz /SRC -e "ssh -p2222" [email protected]:/DEST  
//修改了ssh 協議的端口,默認是22

4. rsync命令

//Rsync的命令格式常用的有以下三種:
    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]HOST:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST
  
//對應於以上三種命令格式,rsync有三種不同的工作模式:
1)拷貝本地文件。當SRC和DES路徑信息都不包含有單個冒號":"分隔符時就啓動這種工作模式。如:
[root@localhost ~]# ls
anaconda-ks.cfg  nfs.sh
[root@localhost ~]# rsync -a nfs.sh a.sh
[root@localhost ~]# ll
total 12
-rw-------. 1 root root 1453 Jun 13 19:27 anaconda-ks.cfg
-rwxr-xr-x  1 root root 1041 Aug  8 18:14 a.sh
-rwxr-xr-x  1 root root 1041 Aug  8 18:14 nfs.sh
[root@localhost ~]# ll -i
total 12
33574978 -rw-------. 1 root root 1453 Jun 13 19:27 anaconda-ks.cfg
33574979 -rwxr-xr-x  1 root root 1041 Aug  8 18:14 a.sh
33574990 -rwxr-xr-x  1 root root 1041 Aug  8 18:14 nfs.sh

2)使用一個遠程shell程序(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器。當DST路徑地址包 \
含單個冒號":"分隔符時啓動該模式。如:
[root@localhost ~]# rsync -avz nfs.sh [email protected]:/root/b.sh
sending incremental file list
nfs.sh

sent 643 bytes  received 35 bytes  1,356.00 bytes/sec
total size is 1,041  speedup is 1.54
[root@localhost ~]# ssh [email protected] 'ls -l /root'
total 8
-rw-------. 1 root root 1454 Aug  6 04:39 anaconda-ks.cfg
-rwxr-xr-x  1 root root 1041 Aug  8  2018 b.sh

3)使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑 \
包含單個冒號":"分隔符時啓動該模式。如:
[root@localhost ~]# ls
anaconda-ks.cfg  a.sh  nfs.sh
[root@localhost ~]# rsync -avz [email protected]:/etc/yum.repos.d /root/
receiving incremental file list
yum.repos.d/
yum.repos.d/163.repo
yum.repos.d/redhat.repo

sent 66 bytes  received 918 bytes  1,968.00 bytes/sec
total size is 1,820  speedup is 1.85
[root@localhost ~]# ls
anaconda-ks.cfg  a.sh  nfs.sh  yum.repos.d
[root@localhost ~]# ls yum.repos.d/
163.repo  redhat.repo



//rsync常用選項:
    -a, --archive       //歸檔
    -v, --verbose       //囉嗦模式
    -q, --quiet         //靜默模式
    -r, --recursive     //遞歸
    -p, --perms         //保持原有的權限屬性
    -z, --compress      //在傳輸時壓縮,節省帶寬,加快傳輸速度
    --delete            //在源服務器上做的刪除操作也會在目標服務器上同步

5. rsync+inotify

Inotify是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了Inotify支持,通過Inotify可以監控文件系統中添加、刪除,修改、移動等各種細微事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools就是這樣的一個第三方軟件。
在前面有講到,rsync可以實現觸發式的文件同步,但是通過crontab守護進程方式進行觸發,同步的數據和實際數據會有差異,而inotify可以監控文件系統的各種變化,當文件有任何變動時,就觸發rsync同步,這樣剛好解決了同步數據的實時性問題。

環境說明:

服務器類型 IP地址 應用 操作系統
源服務器 172.16.12.128 rsync
inotify-tools
腳本
centos7/redhat7
目標服務器 172.16.12.129 rsync centos7/redhat7

需求:

  • 把源服務器上/etc目錄實時同步到目標服務器的/tmp/下

在目標服務器上做以下操作:

//關閉防火牆與SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux


//安裝rsync服務端軟件
[root@localhost ~]# yum -y install rsync
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
myrepo                                          | 4.1 kB     00:00
(1/2): myrepo/group_gz                            | 137 kB   00:00
(2/2): myrepo/primary_db                          | 4.0 MB   00:00
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
......
myrepo/productid                                | 1.6 kB     00:00
  Verifying  : rsync-3.0.9-18.el7.x86_64                           1/1

Installed:
  rsync.x86_64 0:3.0.9-18.el7

Complete!


//設置rsyncd.conf配置文件
[root@localhost ~]# cat >> /etc/rsyncd.conf <<EOF
log file = /var/log/rsyncd.log    # 日誌文件位置,啓動rsync後自動產生這個文件,無需提前創建
pidfile = /var/run/rsyncd.pid     # pid文件的存放位置
lock file = /var/run/rsync.lock   # 支持max connections參數的鎖文件
secrets file = /etc/rsync.pass    # 用戶認證配置文件,裏面保存用戶名稱和密碼,必須手動創建這個文件

[etc_from_client]     # 自定義同步名稱
path = /tmp/          # rsync服務端數據存放路徑,客戶端的數據將同步至此目錄
comment = sync etc from client
uid = root        # 設置rsync運行權限爲root
gid = root        # 設置rsync運行權限爲root
port = 873        # 默認端口
ignore errors     # 表示出現錯誤忽略錯誤
use chroot = no       # 默認爲true,修改爲no,增加對目錄文件軟連接的備份
read only = no    # 設置rsync服務端爲讀寫權限
list = no     # 不顯示rsync服務端資源列表
max connections = 200     # 最大連接數
timeout = 600     # 設置超時時間
auth users = admin        # 執行數據同步的用戶名,可以設置多個,用英文狀態下逗號隔開
hosts allow = 172.16.12.128   # 允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
hosts deny = 192.168.1.1      # 禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
EOF


//創建用戶認證文件
[root@localhost ~]# echo 'admin:123456' > /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
admin:123456

//設置文件權限
[root@localhost ~]# chmod 600 /etc/rsync*
[root@localhost ~]# ll /etc/rsync*
-rw-------. 1 root root 802 Aug  9 22:45 /etc/rsyncd.conf
-rw-------. 1 root root  13 Aug  9 22:47 /etc/rsync.pass

//啓動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.
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128     *:22                  *:*
LISTEN      0      100    127.0.0.1:25                  *:*
LISTEN      0      5       *:873                 *:*
LISTEN      0      128    :::22                 :::*
LISTEN      0      100       ::1:25                 :::*
LISTEN      0      5      :::873                :::*

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

/關閉防火牆與SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

//配置yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2018-08-10 12:07:17--  http://mirrors.163.com/.help/CentOS7-Base-163.repo
Resolving mirrors.163.com (mirrors.163.com)... 59.111.0.251
Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1572 (1.5K) [application/octet-stream]
Saving to: ‘CentOS7-Base-163.repo’

100%[=================================>] 1,572       --.-K/s   in 0s

2018-08-10 12:07:17 (191 MB/s) - ‘CentOS7-Base-163.repo’ saved [1572/1572]
[root@localhost ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost ~]# yum -y install epel-release
安裝過程略。。。。


//安裝rsync服務端軟件,只需要安裝,不要啓動,不需要配置
[root@localhost ~]# yum -y install rsync
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
myrepo                                          | 4.1 kB     00:00
(1/2): myrepo/group_gz                            | 137 kB   00:00
(2/2): myrepo/primary_db                          | 4.0 MB   00:00
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
......
myrepo/productid                                | 1.6 kB     00:00
  Verifying  : rsync-3.0.9-18.el7.x86_64                           1/1

Installed:
  rsync.x86_64 0:3.0.9-18.el7

Complete!


//創建認證密碼文件
[root@localhost ~]# echo '123456' > /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
123456

//設置文件權限,只設置文件所有者具有讀取、寫入權限即可
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw------- 1 root root 7 Aug 10 11:40 /etc/rsync.pass

//在源服務器上創建測試目錄,然後在源服務器運行以下命令
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# mkdir -pv /root/etc/test
mkdir: created directory ‘/root/etc’
mkdir: created directory ‘/root/etc/test’
[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
./
deleting systemd-private-899c38a51df749efb3104e68d77739ff-vmtoolsd.service-28tQpq/tmp/vmware-root/
deleting systemd-private-899c38a51df749efb3104e68d77739ff-vmtoolsd.service-28tQpq/tmp/
deleting systemd-private-899c38a51df749efb3104e68d77739ff-vmtoolsd.service-28tQpq/
deleting systemd-private-899c38a51df749efb3104e68d77739ff-vgauthd.service-0QH6Vs/tmp/
deleting systemd-private-899c38a51df749efb3104e68d77739ff-vgauthd.service-0QH6Vs/
deleting systemd-private-899c38a51df749efb3104e68d77739ff-chronyd.service-4Z4q8n/tmp/
deleting systemd-private-899c38a51df749efb3104e68d77739ff-chronyd.service-4Z4q8n/
deleting .font-unix/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
deleting yum.log
deleting ks-script-G8Z8sn
test/

sent 49 bytes  received 15 bytes  128.00 bytes/sec
total size is 0  speedup is 0.00
//運行完成後,在目標服務器上查看,在/tmp目錄下有test目錄,說明數據同步成功






//安裝inotify-tools工具,實時觸發rsync進行同步
//查看服務器內核是否支持inotify
[root@localhost ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_queued_events
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_user_instances
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_user_watches
//如果有這三個max開頭的文件則表示服務器內核支持inotify

//安裝inotify-tools
[root@localhost ~]# yum -y install make gcc gcc-c++
安裝過程略....
[root@localhost ~]# yum -y install inotify-tools
安裝過程略....


//寫同步腳本,此步乃最最重要的一步,請慎之又慎。讓腳本自動去檢測我們制定的目錄下 \
//文件發生的變化,然後再執行rsync的命令把它同步到我們的服務器端去
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# touch /scripts/inotify.sh
[root@localhost ~]# chmod 755 /scripts/inotify.sh
[root@localhost ~]# ll /scripts/inotify.sh
-rwxr-xr-x 1 root root 0 Aug 10 13:02 /scripts/inotify.sh
[root@localhost ~]# vim /scripts/inotify.sh
host=172.16.12.129      # 目標服務器的ip(備份服務器)
src=/etc        # 在源服務器上所要監控的備份目錄(此處可以自定義,但是要保證存在)
des=etc_from_client     # 自定義的模塊名,需要與目標服務器上定義的同步名稱一致
password=/etc/rsync.pass        # 執行數據同步的密碼文件
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@localhost ~]# nohup bash /scripts/inotify.sh &
[1] 86871
[root@localhost ~]# nohup: ignoring input and appending output to ‘nohup.out’

[root@localhost ~]# ps -ef|grep inotify
root      86871   2143  0 14:52 pts/0    00:00:00 bash /scripts/inotify.sh
root      86872  86871  0 14:52 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 /etc
root      86873  86871  0 14:52 pts/0    00:00:00 bash /scripts/inotify.sh
root      86875   2143  0 14:52 pts/0    00:00:00 grep --color=auto inotify

//在源服務器上生成一個新文件
[root@localhost ~]# ls /etc/httpd24/
extra  httpd.conf  magic  mime.types  original
[root@localhost ~]# echo 'hello world' > /etc/httpd24/test

//查看inotify生成的日誌
[root@localhost ~]# tail /tmp/rsync.log
20180810 14:59 /etc/httpd24/testCREATE was rsynced
20180810 14:59 /etc/httpd24/testMODIFY was rsynced
//從日誌上可以看到,我們生成了一個test文件,並且添加了內容到其裏面

設置腳本開機自動啓動:

[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 ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@localhost ~]# tail /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

到目標服務器上去查看是否把新生成的文件自動傳上去了:

[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# ls
etc  test
[root@localhost tmp]# ls etc/httpd24/
extra  httpd.conf  magic  mime.types  original  test

//由此可見,已將源服務器的/etc目錄整個同步到了目標服務器,且新增的test文件也自動同步了

部署rsync+inotify同步/runtime目錄至目標服務器的/NAME/下。這裏的NAME是指你的名字。

在服務端配置rsync

1.安裝rsync軟件

yum -y install rsync
已加載插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
正在嘗試其它鏡像。

2.設置配置文件

[root@localhost ~]# cat /etc/rsyncd.conf 
[common] 
path = /zhangruilin/   
comment = sync /runtime 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  
hosts allow = 172.16.12.128
hosts deny = 192.168.1.1 

3.創建用戶認證,創建路徑

[root@localhost ~]# cat /etc/rsync.pass 
admin:123456
[root@localhost ~]# mkdir /zhangruilin

4.設置文件權限

[root@localhost ~]# chmod 600 /etc/rsync.pass 
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 13 12月 12 10:16 /etc/rsync.pass

5.設置開啓自啓動,看端口是否開啓

[root@localhost ~]# systemctl restart rsyncd
[root@localhost ~]# systemctl enable rsyncd
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port              
LISTEN      0      128                           *:111                                       *:*                  
LISTEN      0      128                           *:22                                        *:*                  
LISTEN      0      100                   127.0.0.1:25                                        *:*                  
LISTEN      0      25                            *:514                                       *:*                  
LISTEN      0      5                             *:873                                       *:*                  
LISTEN      0      128                          :::111                                      :::*                  
LISTEN      0      128                          :::22                                       :::*                  
LISTEN      0      100                         ::1:25                                       :::*                  
LISTEN      0      25                           :::514                                      :::*                  
LISTEN      0      5                            :::873                                      :::*            

在源服務器做以下操作

1.下載rsync

[root@yusyang ~]# yum -y install rsync
已加載插件:fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                        | 3.6 kB  00:00:00     
epel                                                                                        | 5.3 kB  00:00:00     
extras                                                                                      | 2.9 kB  00:00:00     
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
正在嘗試其它鏡像。
updates                                                                                     | 2.9 kB  00:00:00     
(1/3): epel/x86_64/updateinfo                                                               | 1.0 MB  00:00:00     
(2/3): epel/x86_64/primary_db                               

2.編輯文件,設置和目標服務器密碼相同的文件

[root@yusyang ~]# vim /etc/rsync.pass
[root@yusyang ~]# cat /etc/rsync.pass
123456

3.設置權限,創建目錄

[root@yusyang ~]# chmod 600 /etc/rsync.pass
[root@yusyang ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 12月 12 10:35 /etc/rsync.pass
[root@yusyang ~]# mkdir /runtime
[root@yusyang ~]# 
[root@yusyang ~]# 
[root@yusyang ~]# cd /runtime
[root@yusyang runtime]# touch {a..z}
[root@yusyang runtime]# ls
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

4.用rsync協議鏈接873和192.168.100.129的套接字,將源服務器的/runtime的 修改,刪除等記錄都會同步到目標服務器的目錄下面。common是一個虛擬目錄,目標服務器的真正路徑是/yusyang

[root@yusyang runtime]# rsync -avH --port 873 --progress --delete /runtime [email protected]::common --password-file=/etc/rsync.pass
sending incremental file list
runtime/
runtime/a
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=25/27)
runtime/b
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=24/27)
runtime/c
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=23/27)
runtime/d
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=22/27)
runtime/e
              0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=21/27)
runtime/f
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=20/27)
runtime/g
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=19/27)
runtime/h
              0 100%    0.00kB/s    0:00:00 (xfr#8, to-chk=18/27)
runtime/i
              0 100%    0.00kB/s    0:00:00 (xfr#9, to-chk=17/27)
runtime/j
              0 100%    0.00kB/s    0:00:00 (xfr#10, to-chk=16/27)
runtime/k
              0 100%    0.00kB/s    0:00:00 (xfr#11, to-chk=15/27)

5.下載make gcc gcc-c++和inotify-tools

[root@yusyang ~]# yum -y install make gcc gcc-c++
已加載插件:fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
正在嘗試其它鏡像。
軟件包 gcc-4.8.5-39.el7.x86_64 已安裝並且是最新版本
軟件包 gcc-c++-4.8.5-39.el7.x86_64 已安裝並且是最新版本
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 make.x86_64.1.3.82-23.el7 將被 升級
[root@yusyang ~]# yum -y install inotify-tools
已加載插件:fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 -

寫同步腳本運用到intoify

1.創建腳本文件並給他權限

[root@yusyang ~]# mkdir /scripts
[root@yusyang ~]# touch /scripts/inotify.sh
[root@yusyang ~]# chmod 755 /scripts/inotify.sh 

2.寫腳本,src是源服務器的需要同步的路徑,des是目標服務器寫的虛擬目錄

[root@yusyang ~]# vim /scripts/inotify.sh
[root@yusyang ~]# cat /scripts/inotify.sh
#!/bin/bash

host=192.168.100.129
src=/runtime  
des=common     
password=/etc/rsync.pass
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

3.把腳本放在後臺運行

[root@yusyang ~]# nohup bash /scripts/inotify.sh &
[1] 2225
[root@yusyang ~]# nohup: 忽略輸入並把輸出追加到"nohup.out"

[root@yusyang ~]# ps -ef|grep inotify
root       2225   2083  0 11:00 pts/1    00:00:00 bash /scripts/inotify.sh
root       2226   2225  0 11:00 pts/1    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /runtime
root       2227   2225  0 11:00 pts/1    00:00:00 bash /scripts/inotify.sh
root       2229   2083  0 11:00 pts/1    00:00:00 grep --color=auto inotify

4.在源服務器創建文件,在目標服務器查看路徑是否同步
原服務器創建目錄

[root@yusyang ~]# cd /runtime
[root@yusyang runtime]# touch haha

目標服務器查看

[root@localhost ~]# ls /yusyang/runtime/
a  b  c  d  e  f  g  h  haha  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

將腳本寫在/etc/rc.local裏就可以開機自啓動腳本後臺運行

1.查看/etc/rc.local裏軟連接到了/etc/rc.d/rc.local裏面。將/etc/rc.d/rc.loca設置給予執行權限

[root@yusyang ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 9月  14 00:17 /etc/rc.local -> rc.d/rc.local
[root@yusyang ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 6月  27 2017 /etc/rc.d/rc.local
[root@yusyang ~]# 
[root@yusyang ~]# 
[root@yusyang ~]# chmod +x /etc/rc.d/rc.local

2.編輯/etc/rc.local,加上nohup /bin/bash /sctipts/inotify.sh然後開機之後就可以後臺運行腳本

[root@yusyang ~]# vim /etc/rc.local 
[root@yusyang ~]# cat /etc/rc.local
nohup /bin/bash /sctipts/inotify.sh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章