用Rsync實現windows下同步linux服務器的數據

一:環境

  一、服務端:Red Hat Enterprise Linux Server release 6.4 (Santiago)
  二、客戶端:windows7旗艦版64位
  三、同步對象:測試數據
  四、Rsync介紹:rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync能夠遠程同步,支持本地複製,或者與其餘SSH、rsync主機同步。是一款很是理想的數據同步工具
  Rsync的好處:支持增量備份,對於不少大的數據文件來講用rsync是一個很理想的工具,既能節省磁盤空間還能提升備份相率linux

  五、注意事項:此種同步方式僅限rsync的daemon模式vim

二:無差別同步示意圖

三:服務端配置文件

  一、 rsync配置文件:/etc/rsyncd.conf

uid = rsync # rsync用戶,要是0的話表明全部用戶
gid = rsync # rsync用戶組
use chroot = no # 安全考慮,出現BUG能夠把危險的數據定位到一個chroot目錄下去,避免惡意攻擊
max connections = 200 # 最大鏈接數
timeout = 300 # 超時時常
pid file = /var/run/rsyncd.pid # 服務端進程號
lock file = /var/run/rsync.lock # 服務端鎖文件
log file = /var/log/rsyncd.log # 服務端後臺日誌
[test] # 模塊名
path = /tmp/filesystem/ # 須要同步的目錄內容
ignore errors # 忽略錯誤
read only = false # 取消只讀
list = false # 客戶端列出服務端的內容
hosts allow = 192.168.3.0/24  # 同步的主機地址
hosts deny = 0.0.0.0/32 # 須要阻止哪些地址登錄
auth users = test_rsync # rsync用戶名
secrets file = /etc/rsync.password 

  二、rsync密碼文件:/etc/rsync.password

[root@test ~]# cat /etc/rsync.password 
test_rsync:test1234  #前面是rsync的虛擬用戶,後者是rsync的密碼 
[root@test ~]# ll /etc/rsync.password
-rw------- 1 root root 17 Nov 26 14:46 /etc/rsync.password  # 修改密碼文件權限爲600

  三、修改服務端兩個配置文件權限(/etc/rsyncd.conf、rsync.password)

[root@test ~]# chmod 600 /etc/rsyncd.conf
[root@test ~]# chmod 600 /etc/rsync.password

  四、建立rsync用戶

[root@test ~]# useradd rsync -s /sbin/nologin

  五、啓動服務器端rsync進程

[root@test ~]# rsync --daemon

  六、檢查進程狀態以及進程端口(默認873)以下:說明服務端一切正常

[root@test ~]# netstat -lnutp | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 20509/rsync 
tcp 0 0 :::873 :::* LISTEN 20509/rsync 
[root@test ~]# ps -ef | grep rsync
root 20509 1 0 16:20 ? 00:00:00 rsync --daemon
root 22508 10881 0 17:12 pts/1 00:00:00 grep rsync

三:客戶端準備


  下載客戶端軟件:cwRsync_4.1.0_Installer.exewindows

  連接:https://pan.baidu.com/s/1rzDbW1O5mVoW3F-sKQodUg
  提取碼:lwg0 安全

四:安裝rsync客戶端

五:客戶端配置

  一、rsync家目錄內容

  二、配置客戶端rsync

    在bin目錄下加入密碼文件:rsync.password,注意此密碼要和服務端的/etc/rsync.password裏面的密碼一致,服務端密碼爲:test1234,因此客戶端密碼文件也寫成這個服務器

  三、修改客戶端密碼文件的屬主:(不修改屬主默認會提示錯誤)

  四、同步服務端的數據到客戶端指定位置

    在客戶端rsync目錄的bin目錄下執行:
    rsync.exe -avz --password-file=rsync.password test_rsync@192.168.3.14::test /cygdrive/f/testdir >> syslog.logtcp

  五、參數解讀

    -avz參數說明:
    -a:等於下圖全部內容:工具

    -v:可視化
    -z:傳輸時進行壓縮提升傳輸速率
    --password-file:指定密碼文件
    test_rsync:服務端/etc/rsyncd.conf裏面的rsync的虛擬用戶
    ::test:這個是服務端/etc/rsyncd.conf裏面的模塊名,客戶端會根據服務端的模塊名去找服務端須要共同步數據的目錄
    /cygdrive/f/testdir:告訴rsync個人數據是同步到客戶端f盤的testdir目錄下測試

  六、linux和linux系統之間同步客戶端配置更簡單★★★★

    只須要在客戶端上加入以下內容便可:ui

[root@rsync_client ~]# cat /etc/rsync.password 
test1234

六:無差別備份實驗

  一、服務端數據

[root@rsync_server zwy]# ls
1 10 2 3 4 5 6 7 8 9

  二、客戶端數據

[root@rsync_client data]# pwd
/root/data
[root@rsync_client data]# ls # 客戶端無數據

  三、第一次同步

[root@rsync_client data]# rsync -avz --password-file=/etc/rsync.password test_rsync@192.168.3.14::test ~/data/
receiving incremental file list
./
1
10
2
3
4
5
6
7
8
9
sent 248 bytes received 539 bytes 1574.00 bytes/sec
total size is 0 speedup is 0.00

  四、檢查服務端數據是否同步正常

[root@rsync_client data]# ls
1 10 2 3 4 5 6 7 8 9

  五、無差別同步,刪除服務器端的1-5的數據,而後客戶端拉取數據看看效果

[root@rsync_server zwy]# ls
1 10 2 3 4 5 6 7 8 9
[root@rsync_server zwy]# rm -f {1..5}
[root@rsync_server zwy]# ls
10 6 7 8 9

  六、客戶端用無差別備份的方式拉數據看看效果

[root@rsync_client data]# rsync -avz --delete --password-file=/etc/rsync.password test_rsync@192.168.3.14::test ~/data/
receiving incremental file list
deleting 5
deleting 4
deleting 3
deleting 2
deleting 1
./
10
6
7
8
9
sent 153 bytes received 333 bytes 972.00 bytes/sec
total size is 0 speedup is 0.00
[root@rsync_client data]# ls # 再看客戶端數據發現1-5的數據已經被刪除了
10 6 7 8 9

  七、無差別備份要了解拉數據和推數據的流程,不然風險很大!!!!!

七:附rsync自啓動腳本

 1 [root@test ~]# cat /etc/init.d/tfrsync 
 2 #!/bin/sh
 3 #
 4 #
 5 # rsync start/stop/status/restart
 6 #
 7 # chkconfig: 35 57 10
 8 # description: rsync backup oa data 
 9 
10 . /etc/init.d/functions
11 
12 PID="/var/run/rsyncd.pid"
13 LOCK="/var/run/rsync.lock"
14 COUNT=`ps -ef | grep rsync | grep -vE "grep|vim|cat|more|sh" | wc -l`
15 STATUS_TRUE="action "$0" /bin/true"
16 STATUS_FALSE="action "$0" /bin/false"
17 
18 case "$1" in
19 
20   start)
21 
22         if [ $COUNT -eq 1 ] 
23           then
24             echo -ne "rsync is started " && $STATUS_FALSE
25             exit 1
26         else
27             /usr/bin/rsync --daemon && $STATUS_TRUE || $STATUS_FALSE
28 
29         fi
30 
31         ;;
32 
33   stop)
34 
35         cat $PID | xargs kill -9 2>/dev/null && $STATUS_TRUE && rm -f $PID $LOCK || $STATUS_FALSE
36 
37         ;;
38 
39   status)
40 
41         [ $COUNT -eq 1 ] && echo "rsync is running." || echo "rsync is stoped."
42 
43         ;;
44 
45   restart)
46 
47         if [ -f $PID ]
48           then
49             cat $PID | xargs kill -9 && $STATUS_TRUE && rm -rf $PID $LOCK && \
50             /usr/bin/rsync --daemon && $STATUS_TRUE || $STATUS_FALSE
51         else
52             /usr/bin/rsync --daemon && $STATUS_TRUE || $STATUS_FALSE
53         fi
54 
55         ;;
56 
57   *)
58         echo "Usage:$0 {start|stop|status|restart}"
59         ;;
60 esac

腳本寫的很差,還請你們多多包涵,謝謝!spa

相關文章
相關標籤/搜索