Rsync

記一次線上數據同步 不用模塊和祕鑰文件,通過兩臺服務器之間做互信的方式在同步文件。 首先拷貝發送端的公鑰到接收端上面

# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
eb:1d:9a:b9:d8:42:ab:2e:7c:31:eb:7d:24:69:81:a4 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|    .            |
|   o .           |
|  E . .          |
|       oS        |
|    o = ..       |
| .   * +. .      |
|  o o.o+.= .     |
|   =+.ooB..      |
+-----------------+
# vim .ssh/id_rsa.pub

這裏我直接複製到接收端的.ssh/authorized_keys文件中。因爲是複製的,很可能會自動換行。需要手動調整。 將其調整問一行,當然你把發送端的公鑰文件scp到接收端上,然後重命名文件爲authorized_keys也是可以的。 測試互信是否成功 以/tmp文件夾下的文件做同步測試 

# ls /tmp/
test   test1.txt  test2.txt  test3.txt  test4.txt  test5.txt  test6.txt
test1  test2      test3      test4      test5      test6      yum.log
# /usr/bin/rsync -vzrtopg --progress /tmp/ [email protected]:/tmp
sending incremental file list
./
test1
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=34/36)
test1.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=33/36)
test2
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=32/36)
test2.txt
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=31/36)
test3
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=30/36)
test3.txt
           0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=29/36)
test4
           0 100%    0.00kB/s    0:00:00 (xfer#7, to-check=28/36)
test4.txt
           0 100%    0.00kB/s    0:00:00 (xfer#8, to-check=27/36)
test5
           0 100%    0.00kB/s    0:00:00 (xfer#9, to-check=26/36)
test5.txt
           0 100%    0.00kB/s    0:00:00 (xfer#10, to-check=25/36)
test6
           0 100%    0.00kB/s    0:00:00 (xfer#11, to-check=24/36)
test6.txt
           0 100%    0.00kB/s    0:00:00 (xfer#12, to-check=23/36)
yum.log
           0 100%    0.00kB/s    0:00:00 (xfer#13, to-check=22/36)
.ICE-unix/
test/
test/a1
          12 100%    0.00kB/s    0:00:00 (xfer#14, to-check=19/36)
test/a2
           0 100%    0.00kB/s    0:00:00 (xfer#15, to-check=18/36)
test/a3
           0 100%    0.00kB/s    0:00:00 (xfer#16, to-check=17/36)
test/a4
           0 100%    0.00kB/s    0:00:00 (xfer#17, to-check=16/36)
test/a5
           0 100%    0.00kB/s    0:00:00 (xfer#18, to-check=15/36)
test/a6
           0 100%    0.00kB/s    0:00:00 (xfer#19, to-check=14/36)
test/a7
           0 100%    0.00kB/s    0:00:00 (xfer#20, to-check=13/36)
test/aaa111.txt
           0 100%    0.00kB/s    0:00:00 (xfer#21, to-check=12/36)
test/tes11.txt
           0 100%    0.00kB/s    0:00:00 (xfer#22, to-check=11/36)
test/tes112.txt
           0 100%    0.00kB/s    0:00:00 (xfer#23, to-check=10/36)
test/tes113.txt
           0 100%    0.00kB/s    0:00:00 (xfer#24, to-check=9/36)
test/tes114.txt
           0 100%    0.00kB/s    0:00:00 (xfer#25, to-check=8/36)
test/tes115.txt
           0 100%    0.00kB/s    0:00:00 (xfer#26, to-check=7/36)
test/tes116.txt
           0 100%    0.00kB/s    0:00:00 (xfer#27, to-check=6/36)
test/test1123.txt
           0 100%    0.00kB/s    0:00:00 (xfer#28, to-check=5/36)
test/zz1111.txt
           0 100%    0.00kB/s    0:00:00 (xfer#29, to-check=4/36)
test/zz2222.txt
           0 100%    0.00kB/s    0:00:00 (xfer#30, to-check=3/36)
test/zzz.txt
           0 100%    0.00kB/s    0:00:00 (xfer#31, to-check=2/36)
test/test1/
test/zzz/
sent 1745 bytes  received 620 bytes  1576.67 bytes/sec
total size is 12  speedup is 0.01

加入inotify實現增量同步,腳本如下

# auth_rsync.sh
#!/bin/bash
user1="root"
host1="119.23.211.200"
dst1="/data"
src="/data/"
/soft/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${src} | while read files
        do
        nohup /usr/bin/rsync -vzrtopg --progress ${src} ${user1}@${host1}:${dst1}
        echo "${files} was rsynced" >> /var/log/rsync.log 2>&1
        done

修改權限

# chmod 755 auth_rsync.sh

添加開機啓動

# cp auth_rsync.sh /etc/init.d/
# chkconfig --add /etc/init.d/auth_rsync.sh

以守護進程啓動腳本並放在後臺運行

# nohup /etc/init.d/auth_rsync.sh > /tmp/rsync.log 2>&1 &


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章