CentOS下使用rsync實現定時複製功能

來源:互聯網
上載者:User

一、下載、安裝rsync
**** 正常安裝RH的時候是帶RSYNC的,運行檔案放在/usr/bin/rsync

#tar zxvf rsync-2.6.9.tar.gz

#cd rsync-2.6.9

#./configure --prefix=/usr/local/rsync

#make

#make install

二、配置rsync server

1、啟動RSYNC

#vi /etc/xinetd.d/rsync

  把disable原來的YES改成NO

service rsync

{

        disable = no

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

隨系統啟動RSYNC

     #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

          

模組參數

[backup]                   #這裡是認證的模組名,在client端需要指定

path = /home/backup/        #需要做鏡像的目錄,不可缺少!

comment = This is a test       #這個模組的注釋資訊

ignore errors                #可以忽略一些無關的IO錯誤

read only = yes              # 唯讀

list = no                   #不允許列檔案

auth users = hening             #認證的使用者名稱,如果沒有這行則表明是匿名,此使用者與系統無關

secrets file = /etc/rsyncd.pw           #密碼和使用者名稱對比表,密碼檔案自己產生

hosts allow = 192.168.1.1,10.10.10.10      #允許主機

hosts deny = 0.0.0.0/0                   #禁止主機

#transfer logging = yes

注釋:下面這些綠色檔案是安裝完RSYNC服務後自動產生的檔案

pid file = /var/run/rsyncd.pid      #pid檔案的存放位置

lock file = /var/run/rsyncd.lock     #鎖檔案的存放位置

log file = /var/log/rsyncd.log      #日誌記錄檔案的存放位置

==============執行個體====================

uid = nobody

gid = nobody

use chroot = no

max connections = 4

stirict modes = yes

port = 873

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

[html]

path = /home/html/

comment = This is a html

ignore errors

read only = no

list = no

hosts allow = 192.168.152.155,10.10.10.10

hosts deny = 0.0.0.0/0

auth users = devilzy

secrets file =/etc/rsyncd.pw

[index]

path = /home/index/

comment = This is a index

ignore errors

read only = no

list = no

hosts allow = 192.168.152.155

hosts deny = 0.0.0.0/0

auth users = devilzy

secrets file =/etc/rsyncd.pw

==============執行個體====================

 

3、配置rsync密碼(在上邊的設定檔中已經寫好路徑) /etc/rsyncd.pw(名字隨便寫,只要和上邊設定檔裡的一致即可),格式(一行一個使用者)

帳號:密碼

  #vi /etc/rsyncd.pw

例子:

Hening:111111

許可權:因為rsyncd.pw儲存了rsync服務的使用者名稱和密碼,所以非常重要。要將rsyncd.pw設定為root擁有, 且許可權為600。

#cd /etc

#chown root.root rsyncd.pw

#chmod 600 rsyncd.pw

3.rsyncd.motd(配置歡迎資訊,可有可無)

# vi /etc/rsyncd.motd

rsyncd.motd記錄了rsync服務的歡迎資訊,你可以在其中輸入任何文本資訊,如:

Welcome to use the rsync services!

4、讓配置生效

#service xinetd restart

===============出現問題================================

1.xinetd: unrecognized service

執行如下操作:

yum -y install xinetd

===============出現問題================================

三、啟動rsync server

  RSYNC服務端啟動的兩種方法

1、啟動rsync服務端(獨立啟動)

#/usr/bin/rsync --daemon

2、啟動rsync服務端 (有xinetd超級進程啟動)

# /etc/rc.d/init.d/xinetd reload

3.重啟方法:kill -9 rsync

四:加入rc.local

在各種作業系統中,rc檔案存放位置不盡相同,可以修改使系統啟動時把rsync --daemon載入進去。

#vi /etc/rc.local

加入一行/usr/bin/rsync --daemon

五.檢查rsync

#netstat -a | grep rsync

   tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN

六、開啟連接埠 iptables

iptables -A INPUT -p tcp -s ! 192.168.152.155 --dport 873 -j DROP

如此, 只有 11.22.33.44 這個 client IP 能進入這台 rsync server.

七.配置rsync client

1、設定密碼

#vi /etc/rsyncd.pw

111111

修改許可權

#cd /etc

#chown root.root rsyncd.pw

#chmod 600 rsyncd.pw

2、client串連SERVER

  從SERVER端取檔案

/usr/bin/rsync -vzrtopg --progress --delete devilzy@192.168.152.154::index /home/index --password-file=/etc/rsyncd.pw

/usr/bin/rsync -vzrtopg --progress --delete devilzy@192.168.152.154::html /home/html --password-file=/etc/rsyncd.pw

向SERVER端上傳檔案

/usr/bin/rsync -vzrtopg --progress --password-file=/root/rsyncd.pw  /home/index devilzy@192.168.152.154::index

/usr/bin/rsync -vzrtopg --progress --password-file=/root/rsyncd.pw  /home/html devilzy@192.168.152.154::html

這個命令將把本地機器/home/backup目錄下的所有檔案(含子目錄)全部備份到RSYNC SERVER(172.20.0.6)的backup模組的設定的備份目錄下。

請注意如果路徑結束後面帶有"/",表示備份該目錄下的東東,但不會建立該目錄,如不帶"/"則建立該目錄。

RSYNC用法:

       rsync [OPTION]... [USER@]HOST::SRC  [DEST]              #從RSYNC SERVER備份檔案到本地機器

    rsync [OPTION]... SRC [SRC]...      [USER@]HOST::DEST   #從本地機器備份檔案到RSYNC SERVER

3、自動運行

1)vi /usr/local/rsync/time.sh     //製作指令檔

把下邊的內容複寫進去

#!/bin/bash

/usr/bin/rsync -vzrtopg --progress --delete devilzy@192.168.152.154::index /home/index --password-file=/etc/rsyncd.pw

/usr/bin/rsync -vzrtopg --progress --delete devilzy@192.168.152.154::html /home/html --password-file=/etc/rsyncd.pw

#chmod +x /usr/local/rsync/time.sh

2) crontab -e

加入* * * * * /usr/local/rsync/time.sh        //每隔一分鐘運行一次time.sh指令檔

加入55 * * * * /usr/local/rsync/time.sh        //每到某個小時的55分鐘就運行一次time.sh指令檔(每隔一個小時運行一次)

命令介紹:-rvlHpogDtS

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端指定認證使用者的密碼。

這樣就可以在指令碼中使用而無需互動式地輸入驗證密碼了,這裡需要注意的是這份密碼檔案許可權屬性要設得只有屬主可讀。

hening@192.168.0.217::backup

hening是指server端指定認證的使用者

192.168.0.217是指伺服器端的ip

::backup 表示伺服器端需要同步的模組名稱;

/home/quack/backup/$DATE是同步後的檔案指存放在原生目錄位址。

/var/log/rsync.$DATE是同步後的記錄檔存放在原生目錄位址。

注意

不放/  則目錄名也包含mirror,放 / 則只有目錄裡面的東西mirror了

執行個體總結流程:

1.配置主控端

# vim /etc/rsyncd.conf

###################################

uid = nobody

gid = nobody

use chroot = no

max connections = 4

stirict modes = yes

port = 873

[html]

path = /home/html

comment = This is a html

ignore errors

read only = false

list = no

hosts allow = 192.168.152.*

hosts deny = 0.0.0.0/0

auth users = devilzy

secrets file =/etc/rsyncd.pw

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

[index]

path = /home/index

comment = This is a index

ignore errors

read only = false

list = no

hosts allow = 192.168.152.*

hosts deny = 0.0.0.0/0

auth users = devilzy

secrets file =/etc/rsyncd.pw

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

###################################

# vim /etc/rsyncd.pw

###################################

devilzy:123456

###################################

# cd /etc

# chown root.root rsyncd.pw

# chmod 600 rsyncd.pw

啟動rsync server

# rsync --daemon

查看連接埠873是否開啟

加入啟動

# echo "rsync --daemon" >>/etc/rc.local

給/usr/local/test目錄寫入權限

# chown -R nobody.nobody /usr/local/test

# chmod -R 770 /usr/local/test

主控配置完成

2.用戶端配置

# vim /etc/rsyncd.pw

####################################

123456

####################################

# chown root.root /etc/rsyncd.pw

# chmod 600 /etc/rsyncd.pw

再使用命令直接更新到伺服器資料檔案就不要求輸入密碼

/usr/bin/rsync -vzrtopg --progress --password-file=/root/rsyncd.pw  /home/index devilzy@192.168.152.154::index

/usr/bin/rsync -vzrtopg --progress --password-file=/root/rsyncd.pw  /home/html devilzy@192.168.152.154::html

注意:

1.這裡的index名字為主控conf配置裡面的[index],一定要同名

2.devilzy為主控conf配置裡面的devilzy,可以隨意命名,不是系統使用者

==================遇到的問題和解決辦法========================================

1. @ERROR: chdir failed

rsync error: error starting client-server protocol (code 5) at main.c(1296) [receiver=2.6.8]

原因及解決辦法:

SELinux;

setsebool -P rsync_disable_trans on

==================遇到的問題和解決辦法========================================

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.