RHEL 6.5----rsync+inotify數據同步服務

Rsync特性:html

能夠鏡像保存整個目錄樹和文件系統;web

能夠保持原文件的權限、時間、軟硬連接等;vim

安裝簡單。安全

傳輸特色:bash

速度快:rsync首次同步會複製同步所有內容,之後只傳輸修改過的文件;cookie

壓縮傳輸:rsync傳輸數據時,採用壓縮解壓縮的的方式,所以能夠大大節省帶寬。ssh

安全:可使用scp、ssh等方式傳輸文件,固然也能夠經過直接的socket鏈接。socket

支持匿名傳輸,便於進行網站鏡像;tcp

選擇性保持:符號鏈接、硬連接、文件屬性、權限、時間等。工具

主機名 IP  安裝的軟件 
master 192.168.30.130  xinetd、rsync
slave 192.168.30.131  

 

 

 

[root@master ~]# yum install -y xinetd rsync
[root@master ~]# vim /etc/xinetd.d/rsync 
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
[root@master ~]# /etc/init.d/xinetd restart 
Stopping xinetd:                                           [FAILED]
Starting xinetd:                                           [  OK  ]
[root@master ~]# netstat -antup | grep 873   //rsync的工做端口是873
tcp        0      0 :::873                      :::*                        LISTEN      17551/xinetd 

 以root用戶同步測試

[root@slave ~]# mkdir /data-back
[root@slave ~]# rsync -azP root@192.168.30.130:/var/www/html/ /data-back/
The authenticity of host '192.168.30.130 (192.168.30.130)' can't be established.
RSA key fingerprint is 11:d7:10:45:df:f3:49:15:b4:b5:8e:6d:bc:4f:75:63.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.30.130' (RSA) to the list of known hosts.
root@192.168.30.130's password: 
receiving incremental file list
./
group
         965 100%  942.38kB/s    0:00:00 (xfer#1, to-check=3/5)
hosts
         372 100%  363.28kB/s    0:00:00 (xfer#2, to-check=2/5)
passwd
        2049 100%    1.95MB/s    0:00:00 (xfer#3, to-check=1/5)
shadow
        1156 100%    1.10MB/s    0:00:00 (xfer#4, to-check=0/5)

sent 90 bytes  received 2072 bytes  393.09 bytes/sec
total size is 4542  speedup is 2.10

對比文件權限

[root@master html]# getfacl group 
# file: group
# owner: root
# group: root
user::rw-
group::r--
other::r--

能夠看出master和slave上的兩個文件權限相同
[root@slave ~]# getfacl /data-back/group 
getfacl: Removing leading '/' from absolute path names
# file: data-back/group
# owner: root
# group: root
user::rw-
group::r--
other::r--

基於系統用戶的備份

建立上傳和下載用戶
[root@master ~]# useradd rget01
[root@master ~]# echo "rget01:123456" | chpasswd
[root@master ~]# useradd rput01
[root@master ~]# echo "rput01:123456" | chpasswd
[root@master ~]# setfacl -R -m user:rput01:rwx /var/www/html/
[root@master ~]# setfacl -R -m default:user:rput01:rwx /var/www/html/
[root@master ~]# getfacl /var/www/html/
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/
# owner: root
# group: root
user::rwx
user:rput01:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:rput01:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

[root@master ~]# setfacl -R -m user:rget01:rwx /var/www/html/
[root@master ~]# setfacl -R -m default:user:rget01:rwx /var/www/html/
[root@master ~]# getfacl /var/www/html/
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/
# owner: root
# group: root
user::rwx
user:rget01:rwx
user:rput01:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:rget01:rwx
default:user:rput01:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

[root@master ~]# getfacl /var/www/html/passwd   //以前存在的文件權限
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/passwd
# owner: root
# group: root
user::rw-
user:rget01:rwx
user:rput01:rwx
group::r--
mask::rwx
other::r--

[root@master ~]# cp -r /root/install.log /var/www/html/
[root@master ~]# getfacl /var/www/html/install.log //新建文件的權限
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/install.log
# owner: root
# group: root
user::rw-
user:rget01:rwx            #effective:r--   //若是在後面的試驗中同步不到客戶端,注意看這裏是否有生效的讀取的權限
user:rput01:rwx            #effective:r--
group::r-x            #effective:r--
mask::r--
other::r--

[root@slave ~]# rsync -azP --delete rget01@192.168.30.130:/var/www/html /data-back  //注意這裏加 / 和不加 /的區別;不加 / 會把目錄同步過去
rget01@192.168.30.130's password: 
receiving incremental file list
html/
html/group
         965 100%  942.38kB/s    0:00:00 (xfer#1, to-check=4/6)
html/hosts
         372 100%   90.82kB/s    0:00:00 (xfer#2, to-check=3/6)
html/install.log
       61802 100%    9.82MB/s    0:00:00 (xfer#3, to-check=2/6)
html/passwd
        2049 100%  333.50kB/s    0:00:00 (xfer#4, to-check=1/6)
html/shadow
        1156 100%  188.15kB/s    0:00:00 (xfer#5, to-check=0/6)

sent 110 bytes  received 16047 bytes  4616.29 bytes/sec
total size is 66344  speedup is 4.11

[root@slave ~]# cd /data-back/
[root@slave data-back]# ll 
total 20
-rw-r--r-- 1 root root  965 May 24 20:36 group
-rw-r--r-- 1 root root  372 May 24 20:36 hosts
drwxrwxr-x 2 root root 4096 May 24 20:48 html
-rw-r--r-- 1 root root 2049 May 24 20:36 passwd
---------- 1 root root 1156 May 24 20:36 shadow
[root@slave data-back]# cd /data-back/html/
[root@slave html]# getfacl install.log 
# file: install.log
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@master ~]# rm -rf /var/www/html/install.log 
[root@slave ~]# ssh-copy-id rget01@192.168.30.130
rget01@192.168.30.130's password: 
Now try logging into the machine, with "ssh 'rget01@192.168.30.130'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@slave ~]# rsync -azP --delete rget01@192.168.30.130:/var/www/html /tmp
receiving incremental file list

sent 15 bytes  received 1319 bytes  2668.00 bytes/sec
total size is 95038  speedup is 71.24
[root@slave ~]# rsync -azP --delete rget01@192.168.30.130:/var/www/html /tmp
receiving incremental file list
deleting html/install.log
html/

sent 18 bytes  received 1291 bytes  2618.00 bytes/sec
total size is 33236  speedup is 25.39

定時任務的時候,設置免密碼登錄,寫個簡單的腳本便可。

[root@slave ~]# rsync -azP root@192.168.30.130:/var/www/html /tmp 
root@192.168.30.130's password: 
receiving incremental file list
html/
html/install.log
       61802 100%   58.94MB/s    0:00:00 (xfer#1, to-check=0/2)

sent 34 bytes  received 13989 bytes  5609.20 bytes/sec
total size is 61802  speedup is 4.41
[root@slave ~]# getfacl /tmp/html/install.log 
getfacl: Removing leading '/' from absolute path names
# file: tmp/html/install.log
# owner: root
# group: root
user::rw-
group::r--
other::r--

[root@slave ~]# rsync -azp --delete rget01@192.168.30.130:/var/www/html /tmp  //小寫的p不打印傳輸過程,--delete會刪除以前備份的文件,能夠看出現當前只有html目錄,沒有以前的install.log文件了
rget01@192.168.30.130's password: 
[root@slave ~]# ll /tmp/
total 4
drwxrwxr-x  4 root root 4096 May 25 10:38 html
-rw-------. 1 root root    0 May  7 23:58 yum.log

注意有些文件,rget01和rput01並無有效權限
[root@master ~]# cp -r /boot/grub /var/www/html/
[root@master ~]# ll /var/www/html/
total 96
drwxr-xr-x+ 2 root root  4096 May 25 10:46 grub
-rw-rwxr--+ 1 root root 61802 May 25 10:30 install.log
drwxr-xr-x+ 2 root root  4096 May 25 10:38 pam.d
drwxr-xr-x+ 3 root root  4096 May 25 10:38 pango
-rw-r--r--+ 1 root root  1861 May 25 10:38 passwd
-rw-r--r--+ 1 root root  1820 May 25 10:38 passwd-
[root@master ~]# getfacl /var/www/html/grub/
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/grub/
# owner: root
# group: root
user::rwx
user:rget01:rwx            #effective:r-x
user:rput01:rwx            #effective:r-x
group::r-x
mask::r-x
other::r-x
default:user::rwx
default:user:rget01:rwx
default:user:rput01:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

[root@master ~]# getfacl /var/www/html/grub/
device.map         grub.conf          minix_stage1_5     stage2
e2fs_stage1_5      iso9660_stage1_5   reiserfs_stage1_5  ufs2_stage1_5
fat_stage1_5       jfs_stage1_5       splash.xpm.gz      vstafs_stage1_5
ffs_stage1_5       menu.lst           stage1             xfs_stage1_5
[root@master ~]# getfacl /var/www/html/grub/grub.conf 
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/grub/grub.conf
# owner: root
# group: root
user::rw-
user:rget01:rwx            #effective:---
user:rput01:rwx            #effective:---
group::r-x            #effective:---
mask::---
other::---

[root@slave ~]# rsync -azP --delete rget01@192.168.30.130:/var/www/html /tmp
rget01@192.168.30.130's password: 
receiving incremental file list
html/
html/grub/
html/grub/device.map
          63 100%   61.52kB/s    0:00:00 (xfer#1, to-check=78/86)
html/grub/e2fs_stage1_5
       13380 100%    3.19MB/s    0:00:00 (xfer#2, to-check=77/86)
html/grub/fat_stage1_5
       12620 100%    2.01MB/s    0:00:00 (xfer#3, to-check=76/86)
html/grub/ffs_stage1_5
       11748 100%    1.87MB/s    0:00:00 (xfer#4, to-check=75/86)
rsync: send_files failed to open "/var/www/html/grub/grub.conf": Permission denied (13)   //這種錯誤是rget01沒有有效的讀取權限
html/grub/iso9660_stage1_5
       11756 100%    1.02MB/s    0:00:00 (xfer#5, to-check=73/86)
html/grub/jfs_stage1_5
       13268 100%  996.69kB/s    0:00:00 (xfer#6, to-check=72/86)
html/grub/menu.lst -> ./grub.conf
html/grub/minix_stage1_5
       11956 100%  898.14kB/s    0:00:00 (xfer#7, to-check=70/86)
html/grub/reiserfs_stage1_5
       14412 100% 1005.30kB/s    0:00:00 (xfer#8, to-check=69/86)
html/grub/splash.xpm.gz
        1341 100%   93.54kB/s    0:00:00 (xfer#9, to-check=68/86)
html/grub/stage1
         512 100%   35.71kB/s    0:00:00 (xfer#10, to-check=67/86)
html/grub/stage2
      126100 100%    5.01MB/s    0:00:00 (xfer#11, to-check=66/86)
html/grub/ufs2_stage1_5
       12024 100%  469.69kB/s    0:00:00 (xfer#12, to-check=65/86)
html/grub/vstafs_stage1_5
       11364 100%  426.83kB/s    0:00:00 (xfer#13, to-check=64/86)
html/grub/xfs_stage1_5
       13964 100%  524.49kB/s    0:00:00 (xfer#14, to-check=63/86)

sent 310 bytes  received 149583 bytes  59957.20 bytes/sec
total size is 350356  speedup is 2.34
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1505) [generator=3.0.6]
在master上執行
[root@master ~]# chmod 744 /var/www/html/grub/grub.conf
而後在slave上再次執行
[root@slave ~]# rsync -azP --delete rget01@192.168.30.130:/var/www/html /tmp
rget01@192.168.30.130's password: 
receiving incremental file list
html/grub/grub.conf
         799 100%  780.27kB/s    0:00:00 (xfer#1, to-check=74/86)

sent 35 bytes  received 2169 bytes  629.71 bytes/sec
total size is 350356  speedup is 158.96
在master上刪除一些文件測試同步
[root@master ~]# rm -rf /var/www/html/grub/
在slave上同步測試
[root@slave ~]# rsync -azP --delete rget01@192.168.30.130:/var/www/html /tmp
rget01@192.168.30.130's password: 
receiving incremental file list
deleting html/grub/xfs_stage1_5
deleting html/grub/vstafs_stage1_5
deleting html/grub/ufs2_stage1_5
deleting html/grub/stage2
deleting html/grub/stage1
deleting html/grub/splash.xpm.gz
deleting html/grub/reiserfs_stage1_5
deleting html/grub/minix_stage1_5
deleting html/grub/menu.lst
deleting html/grub/jfs_stage1_5
deleting html/grub/iso9660_stage1_5
deleting html/grub/grub.conf
deleting html/grub/ffs_stage1_5
deleting html/grub/fat_stage1_5
deleting html/grub/e2fs_stage1_5
deleting html/grub/device.map
deleting html/grub/
html/

sent 18 bytes  received 1322 bytes  536.00 bytes/sec
total size is 95038  speedup is 70.92

自動定時備份簡單腳本

[root@slave ~]# vim /root/rsync_auto.sh
#!/bin/bash
rsync -az --delete rget01@192.168.30.130:/var/www/html /web-back
tar -czvf data-back-`date +%Y-%m-%d`.tar.gz /data-back/*
[root@slave ~]# chmod +x /root/rsync_auto.sh 
[root@slave ~]# echo "0 3 * * * sh /root/rsync-auto.sh &" > /var/spool/cron/root

基於非系統用戶的數據備份實例

在master上創建rsyncd.conf文件
[root@master ~]# vim /etc/rsyncd.conf
uid = nobody                            
gid = nobody                            
address = 192.168.30.130                        
port = 873                              
hosts allow = 192.168.30.131 
use chroot = yes             
max connections = 5                                     
pid file = /var/run/rsyncd.pid          
lock file = /var/run/rsync.lock                 
log file = /var/log/rsyncd.log          
motd file = /etc/rsyncd.motd          

[rsync-data]                            
path = /var/www/html                            
comment = used for web-data root    
read only = yes                                 
list = yes                                              
auth users = rsyncuser                          
secrets file = /etc/rsync.passwd
創建歡迎信息文件
[root@master ~]# echo ""Welcome to Rsync data back" > /etc/rsyncd.motd
[root@master ~]# cat !$
cat /etc/rsyncd.motd
<h1>Welcome to Rsync data back</h1>
創建rsync用戶密碼文件
[root@master ~]# vim /etc/rsync.passwd
rsyncuser:admin123
修改密碼文件權限
[root@master ~]# chmod 600 /etc/rsync.passwd 
重啓rsync服務(兩種方式)
[root@master ~]# service xinetd restart 
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
[root@master ~]# netstat -antup | grep 873
tcp        0      0 :::873                      :::*                        LISTEN      49021/xinetd        
[root@master ~]# service xinetd stop 
Stopping xinetd:                                           [  OK  ]
[root@master ~]# mv /etc/rsync.conf /etc/rsyncd.conf
[root@master ~]# rsync --daemon --config=/etc/rsyncd.conf 
[root@master ~]# netstat -antup | grep 873
tcp        0      0 192.168.30.130:873          0.0.0.0:*                   LISTEN      49042/rsync  

在slave端同步測試

[root@slave ~]# rsync -azP rsyncuser@192.168.30.130::backdata /data-back/
Welcome to Rsync data back  //這是咱們設置的歡迎信息

Password:   #輸入admin123,回車
receiving incremental file list
./
passwd
        1861 100%    1.77MB/s    0:00:00 (xfer#1, to-check=1/3)
rsync: send_files failed to open "/shadow" (in backdata): Permission denied (13) 

sent 104 bytes  received 1037 bytes  175.54 bytes/sec
total size is 3088  speedup is 2.71
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1505) [generator=3.0.6]
在master上更改權限重試便可
[root@master ~]# chmod 744 /var/www/html/shadow 
[root@slave ~]# rsync -azP rsyncuser@192.168.30.130::backdata /data-back/
Welcome to Rsync data back

Password: 
receiving incremental file list
shadow
        1227 100%    1.17MB/s    0:00:00 (xfer#1, to-check=0/3)

sent 82 bytes  received 706 bytes  175.11 bytes/sec
total size is 3088  speedup is 3.92

實現自動備份的腳本

[root@slave ~]# vim /root/auto-rsync-passwd.sh
#!/bin/bash
export RSYNC_PASSWORD=admin123
rsync -avz rsyncuser@191.168.30.130::backdata /data-back

生產環境下,開啓iptables,而後放行873端口

[root@master ~]# iptables -A INPUT -p tcp --dport 873 -j ACCEPT

 

Rsync命令
    rsync命令和scp命令很類似
-a, --archive archive mode 權限保存模式,至關於 -rlptgoD 參數,存檔,遞歸,保持屬性等
-r, --recursive 複製全部下面的資料,遞歸處理
-p, --perms 保留檔案權限 ,文件原有屬性
-t, --times 保留時間點,文件原有時間
-g, --group 保留原有屬組
-o, --owner 保留檔案全部者(root only)
-D, --devices 保留device資訊(root only) 
-l, --links 複製全部的鏈接 ,拷貝鏈接文件
-z, --compress 壓縮模式, 當資料在傳送到目的端進行檔案壓縮.  –azP
-H, --hard-links 保留硬連接文件
-A, --acls 保留ACL屬性文件,須要配合--perms
-P,-P參數和 --partial --progress 相同.只是爲了把參數簡單化,表示傳進度
--version, 輸出rsync版本 
-v , --verbose 複雜的輸出信息 
-u, --update 僅僅進行更新,也就是跳過已經存在的目標位置,而且文件時間要晚於要備份的文件,不覆蓋新的文件
--port=PORT, 定義rsyncd(daemon)要運行的port(預設爲tcp 873) 
--delete, 刪除那些目標位置有的文件而備份源沒有的文件 
--password-file=FILE ,從 FILE 中獲得密碼 
--bwlimit=KBPS, 限制 I/O 帶寬 
--filter 「-filename」,須要過濾的文件 
--exclude=filname,須要過濾的文件
--progress,顯示備份過程

 inotify實時同步測試

inotify下載地址:

https://sourceforge.net/projects/inotify-tools/files/latest/download

  Linux 內核從 2.6.13 版本開始提供了 inotify 通知接口,用來監控文件系統的各類變化狀況,如文件存取、刪除、移動等。利用這一機制,能夠很是方便地實現文件異動告警、增量備份,並針對目錄或文件的變化及時做出響應。使用 rsync 工具與 inotify 機制相結合,能夠實現觸發式備份(實時同步),只要原始位置的文檔發生變化,則當即啓動增量備份操做,不然處於靜態等侍狀態,這樣一來,就避免了按固定週期備份進存在的延遲性、週期過密等問題。

[root@slave ~]# uname -r
2.6.32-431.el6.x86_64
[root@slave ~]# ls /proc/sys/fs/inotify/
max_queued_events  max_user_instances  max_user_watches

max_queued_events  #監控時間隊列
max_user_instances  #最多監控實例數
max_user_watches   #每一個實例最多監控的文件數
[root@slave ~]# cat /proc/sys/fs/inotify//max_queued_events
16384
[root@slave ~]# cat /proc/sys/fs/inotify//max_user_
max_user_instances  max_user_watches    
[root@slave ~]# cat /proc/sys/fs/inotify//max_user_instances 
128
[root@slave ~]# cat /proc/sys/fs/inotify//max_user_watches 
8192

[root@slave ~]# vim /etc/sysctl.conf 
#末尾添加三行內容
fs.inotify.max_queued_events = 32768
fs.inotify.user_instances = 1024
fs.inotify.max_user_watches = 900000000
使當即生效並查看
[root@slave ~]# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
fs.inotify.max_queued_events = 32768
error: "fs.inotify.user_instances" is an unknown key
fs.inotify.max_user_watches = 900000000
[root@slave ~]# cat /proc/sys/fs/inotify/max_user_watches 
900000000

安裝inotify-tools

[root@slave ~]# tar -xf inotify-tools-3.13.tar.gz -C /usr/local/src/
[root@slave ~]# cd /usr/local/src/inotify-tools-3.13/
[root@slave inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify-tools;make;make install
[root@slave ~]# ln -s /usr/local/inotify-tools/bin/* /usr/bin/
使用一樣方法在master上也安裝inotify-tools
inotifywait經常使用參數:
     -e  用來指定要監控哪些事件。這些事件包括: create 建立,move 移    
         動,delete 刪除,modify 修改文件內容,attrib 屬性更改。
    -m 表示持續監控
    -r  表示遞歸整個目錄
    -q 表示簡化輸出信息。

測試

首先打開兩個終端
在第一個終端內輸入
[root@master ~]# ln -s /usr/local/inotify-tools/bin/* /usr/bin/
[root@master ~]# inotifywait -mrq -e create.move,delete,modify 
在另一個終端內輸入
[root@master ~]# cp install.log.syslog /var/www/html/
[root@master ~]# mkdir /var/www/html/inotify
而後在第一個中能夠看到
[root@master ~]# ln -s /usr/local/inotify-tools/bin/* /usr/bin/
[root@master ~]# inotifywait -mrq -e create.move,delete,modify /var/www/html/
'create.move,delete,modify' is not a valid event!  Run with the '--help' option to see a list of events.
[root@master ~]# inotifywait -mrq -e create,move,delete,modify /var/www/html/
/var/www/html/ CREATE install.log.syslog
/var/www/html/ MODIFY install.log.syslog
/var/www/html/ CREATE,ISDIR inotify

若是配置自動備份腳本

[root@master ~]# vim /root/inotify.sh
#!/bin/bash
SRC=/var/www/html
DST=root@192.168.30.130:/data-back
inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F
        do
                /usr/bin/rsync -ahqzt --delete $SRC $DST
        done
#D E F 分別對應前面的三個變量,由於前面輸出三段內容
~ 
相關文章
相關標籤/搜索