Rsync同步文件

Rsync常用的一款同步文件工具。配置也很方便。

 

yum install sync -y或者apt-get install sync

* 服務安裝好之後,/etc下沒有配置文件,一般情況可以copy示例文件到/etc 

#cp /usr/share/doc/rsync/examples/rsyncd.conf /etc

 

vim /etc/rsyncd.conf

[root@test150 rsyncd]# grep -v "^#" /etc/rsyncd.conf

 

 

 

 uid = nobody

 gid = nobody

 use chroot = yes

 max connections = 3600

log file = /var/log/rsync/rsyncd.log

motd file = /var/log/rsync/rsyncd.motd  

pid file = /var/run/rsyncd.pid

secrets file = /etc/rsyncd/rsyncd.secrets  

log format = %t %a %m %f %b

syslog facility = local3

strict modes=yes

timeout=300

 

 

 lock file=/var/run/rsyncd.lock

 hosts allow=192.168.2.151

 hosts deny=*

 

[homeools]

path = /home/tools

list=yes

ignore errors

ayth users=rsync

comment = homeools

exclude = important/

 

* 建立/etc/rsyncd/rsyncd.secrets文件

#cat /etc/rsyncd/rsyncd.secrets

    rsync:123456

* 爲了密碼的安全性,我們必須把權限設爲600

# chown root:root /etc/rsyncd/rsyncd.secrets

 

# chmod 600 /etc/rsyncd/rsyncd.secrets

# vim /etc/rsyncd/rsyncd.motd

 

啓動rsync

systemctl start rsyncd.service

 lsof -i:873

COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME

rsync   28949 root    4u  IPv4 3939762      0t0  TCP *:rsync (LISTEN)

rsync   28949 root    5u  IPv6 3939763      0t0  TCP *:rsync (LISTEN)

至此服務器端安裝完畢;

客戶端安裝配置:

 

yum -y install rsync

 

運行:rsync -zvaP [email protected]::hometools /tmp/test/

Password: 這裏要輸入rsync的密碼,是服務器端提供的,在前面的例子中,我們用的是 asdf,輸入的密碼並不顯示出來;輸好後就回車; 注: 這個命令的意思就是說,用rsync 用戶登錄到服務器上,把hometools 數據,同步到本地目錄/tmp/test/上。當然本地的目錄是可以你自己定義的,比如 dave也是可以的;當你在客戶端上,當前操作的目錄下沒有/tmp/test/這個目錄時,系統會自動爲你創建一個;當存在/tmp/test/這個目錄中,你要注意它的寫權限。

參數說明:

 -a 參數,相當於-rlptgoD

   -r 是遞歸 -l 是鏈接文件,意思是拷貝鏈接文件;

   -p 表示保持文件原有權限;

   -t 保持文件原有時間;
   -g 保持文件原有用戶組;

   -o 保持文件原有屬主;

   -D 相當於塊設備文件;

   -z 傳輸時壓縮;

   -P 傳輸進度;

   -v 傳輸時的進度等信息,和-P有點關係

 

*

rsync -avzP  --delete [email protected]::hometools /tmp/test/

 

–delete 選項,表示服務器上的數據要與客戶端完全一致,如果 /tmp/test/目錄中有服務器上不存在的文件,則刪除。最終目的是讓/tmp/test/目錄上的數據完全與服務器上保持一致;用的時候要小心點,最好不要把已經有重要數所據的目錄,當做本地更新目錄,否則會把你的數據全部刪除;

 

*

[root@151test test]# rsync -avzP -delete  [email protected]::hometools /tmp/test --password-file=/etc/rsyncd/rsyncd.secrets

 

 

計劃任務同步:

5/* * * * * rsync -zvaP [email protected]::hometools /tmp/test/ --password-file=/etc/rsyncd/rsyncd.secrets

 

 


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