rsync的配置與使用

rysnc的主頁地址爲:
http://rsync.samba.org/
目前最新版本爲3.0.6。可以選擇從原始網站下載:
http://rsync.samba.org/ftp/rsync/
也可以選擇從本站下載:rsync 3.0.6

一、下載、安裝rsync
 #tar zxvf rsync-3.0.6.tar.gz
 #cd rsync-3.0.6
 #./configure --prefix=/usr/local/rsync
 #make
 #make install
 **** 正常安裝RH的時候是帶RSYNC的,運行文件放在/usr/bin/rsync


二、配置rsync server
1.啓動rsync
[root@lecai ~]# vim /etc/xinetd.d/rsync
把 disable = no   把yes改爲NO
#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
}
隨系統啓動rsync
[root@lecai ~]# chkconfig rsync on

2、配置/etc/rsyncd.conf(需要手動生成)
rsyncd.conf的參數寫在上邊就是全局參數和寫在模塊裏的就是模塊參數
#vi /etc/rsyncd.conf
全局參數
uid = root                                  //運行RSYNC守護進程的用戶
gid = root                                  //運行RSYNC守護進程的組
use chroot = no                 //不使用chroot
max connections = 4             // 最大連接數爲4
strict modes =yes                //是否檢查口令文件的權限
port = 873                      //默認端口873
註釋:下面這些文件是安裝完RSYNC服務後自動生成的文件
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock

模塊參數
[caiwubeifen]               //這裏是認證的模塊名,在client端需要指定
comment= backup caiwu       //這個模塊的註釋信息。
path=/home/backup           //需要要備份鏡像的目錄路徑,必不可少
ignore errors               //忽視一些無關的IO錯誤
read only = no             //用yes,只能從service端啦數據,無法向service端推數據。
auth users = caiwu          //認證的用戶名,如果沒有這行則表明是匿名,此用戶與系統無關
secrets file= /etc/rsync.pas //密碼和用戶名對比表,密碼文件自己生成
#hosts allow=172.16.78.0/22  //允許主機
#hosts deny = 0.0.0.0/0       //禁止主機

3、配置rsync密碼(在上邊的配置文件中已經寫好路徑) /etc/rsync.pas(名字隨便寫,只要和上邊配置文件裏的一致即可),格式(一行一個用戶)
賬號:密碼
[root@lecai ~]# vim /etc/rsyncd.pas

caiwu:123456
權限:因爲rsync.pas存儲了rsync服務的用戶名和密碼,所以非常重要。要將rsync.pas設置爲root擁有, 且權限爲600。
[root@lecai ~]# cd /etc/
[root@lecai etc]# chown root.root rsync.pas
[root@lecai etc]# chmod 600 rsync.pas
[root@lecai etc]# ll rsync.pas
-rw-------. 1 root root 13 4月  13 02:51 rsync.pas
3.rsyncd.motd(配置歡迎信息,可有可無)信息輸入內容隨意。
[root@lecai etc]# vim /etc/rsync.motd

Welcome to use the lecaicaiwu rsync services!
4、讓配置生效
[root@lecai ~]# service xinetd restart
停止 xinetd:                                              [確定]
正在啓動 xinetd:                                          [確定]

三、啓動rsync server
   RSYNC服務端啓動的兩種方法
1、啓動rsync服務端(獨立啓動)
#/usr/bin/rsync –daemon

2、啓動rsync服務端 (有xinetd超級進程啓動)
# /etc/rc.d/init.d/xinetd reload
例子:
[root@lecai ~]# /etc/rc.d/init.d/xinetd reload
重新載入配置:                                             [確定]

四:加入rc.local
在各種操作系統中,rc文件存放位置不盡相同,可以修改使系統啓動時把rsync --daemon加載進去
[root@lecai ~]# vim /etc/rc.local
加入一行/usr/bin/rsync --daemon
例子:
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/usr/bin/rsync --daemon
五.檢查 rsync
[root@lecai ~]# netstat -a | grep rsync
tcp        0      0 *:rsync                     *:*                         LISTEN
[root@lecai ~]# lsof -i:873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
xinetd  4189 root    5u  IPv6 233004      0t0  TCP *:rsync (LISTEN)
服務端的文件:
[root@lecai home]# cd /home/backup/
[root@lecai backup]# ll
總用量 4
-rw-r--r--. 1 root root    0 4月  16 00:03 test
drwxr-xr-x. 2 root root 4096 4月  16 00:04 test1

六.關閉防火牆和通信限制selinux
[root@lecai home]# vi /etc/selinux/config
SELINUX=disabled   改爲disabled 是就不要改了
[root@lecai home]# setenforce 0 更改完selinux 不重啓,文件生效
[root@lecai home]# service iptables stop     有需求可選擇永久關閉
七.配置 rsync client
1.設定密碼
[root@localhost ~]# vi /etc/rsync.pas
[root@localhost etc]# cat /etc/rsync.pas
123456
[root@localhost etc]# cd /etc/
[root@localhost etc]# ll rsync.pas
-rw------- 1 root root 7 Apr 15 00:57 rsync.pas
[root@localhost etc]# chown root.root rsync.pas
[root@localhost etc]# chmod 600 rsync.pas
 同樣關閉防火牆和通信限制selinux。
從SERVER端取文件
2[root@localhost home]# /usr/bin/rsync -vzrtopg --progress --delete [email protected]::caiwubeifen /home/backup --password-file=/etc/rsync.pas
receiving incremental file list

sent 63 bytes  received 126 bytes  378.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost home]# cd backup/
[root@localhost backup]# ll
total 4
-rw-r--r-- 1 root root    0 Apr 15  2015 test
drwxr-xr-x 2 root root 4096 Apr 15  2015 test1
向SERVER端上傳文件
[root@localhost backup]#  /usr/bin/rsync -vzrtopg --progress --password-file=/etc/rsync.pas  /home/backup [email protected]::caiwubeifen
sending incremental file list
backup/
backup/test
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
backup/jiangwei/
backup/test1/

sent 140 bytes  received 39 bytes  119.33 bytes/sec
total size is 0  speedup is 0.00

rsync命令參數
-v表示verbose詳細顯示
-z表示壓縮
-r表示recursive遞歸
-t表示保持原文件創建時間
-o表示保持原文件屬主
-p表示保持原文件的參數
-g表示保持原文件的所屬組
-a存檔模式
-P表示代替-partial和-progress兩者的選項功能
-e ssh建立起加密的連接。
--partial阻止rsync在傳輸中斷時刪除已拷貝的部分(如果在拷貝文件的過程中,傳輸被中斷,rsync的默認操作是撤消前操作,即從目標機上
 刪除已拷貝的部分文件。)
--progress是指顯示出詳細的進度情況
--delete是指如果服務器端刪除了這一文件,那麼客戶端也相應把文件刪除,保持真正的一致。
--exclude不包含/ins目錄
--size-only 這個參數用在兩個文件夾中的差別僅是源文件夾中有一些新文件,不存在重名且被修改過的文件,因爲這種文件有可能會因爲內容被修改可大小一樣,而被略過。這個參數可以大大地提高同步的效率,因爲它不需要檢查同名文件的內容是否相同。
--password-file來指定密碼文件,內容包含server端指定認證用戶的密碼。
 這樣就可以在腳本中使用而無需交互式地輸入驗證密碼了,這裏需要注意的是這份密碼文件權限屬性要設得只有屬主可讀。
[email protected]::caiwubeifen
 caiwu是指server端指定認證的用戶
 172.16.22.141是指服務器端的ip
 ::caiwubeifen 表示服務器端需要同步的模塊名稱;

八、自動運行
vi /root/time.sh
內容:
/usr/bin/rsync -vzrtopg --progress --delete [email protected]::caiwubeifen /home/backup --password-file=/etc/rsync.pas

2) crontab -e
10 * * * * /root/time.sh   測試


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