CentOS6.x7.x中用rsync遠程同步文件

CentOS 6.x中用rsync遠程同步文件

十多年的南康網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整南康建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“南康網(wǎng)站設(shè)計”,“南康網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

系統(tǒng)環(huán)境:Centos 6.9 x64  (Centos 7.3 x64

目的:

服務(wù)器110.112.200.12中/u01文件夾需要同步復(fù)制到110.210.250.58里面進行備份。

將200.12做xinetd 服務(wù)器,將其/u01 文件夾復(fù)制同步到250.58里面去,250.58做客戶端。

一、服務(wù)器端的配置

Centos 6.9 x64

在源服務(wù)器110.112.200.12中配置

[root@mail test]#  yum -y install xinetd rsync

再修改配置: # vi /etc/xinetd.d/rsync

service rsync

{

        disable = NO

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

把原來的disable ,由YES改成NO

[root@mail test]#  vi /etc/rsyncd.conf

uid = root 

gid = root 

use chroot = no

max connections = 20

secrets file = /etc/rsync_pass

pid file = /var/run/rsyncd.pid 

log file = /var/log/rsyncd.log 

 

[backup] 

path = /u01 

comment = Rsync share test 

auth users = ruser 

read only = yes 

hosts allow =110.210.250.58

hosts deny = *

注意,path = /u01 ;表示要備份的文件夾為/u01

配置同步的帳號、密碼

[root@mail test]# vi /etc/rsync_pass 

ruser:123456

[root@mail test]# chown root:root /etc/rsync_pass

[root@mail test]# chmod 600 /etc/rsync_pass

[root@mail test]# chkconfig xinetd on

[root@mail test]# service xinetd restart

檢查是否出現(xiàn)873端口

[root@mail test]# netstat -natp

tcp   0    0 0.0.0.0:873       0.0.0.0:*    LISTEN      20959/xinetd 

注意:如果服務(wù)器上裝有防火墻記得要打開端口,默認端口是873

另外一種啟動、停止方法(不建議使用)

有些人喜歡用命令rsync --daemon --config=/etc/rsyncd.conf來啟動rsync,停用它時要用pkill命令來殺掉進程,不如用xinetd來管理啟動rsync方便。

再有些人喜歡寫個腳本來啟動、停止rsync,系統(tǒng)本身有xinetd來管理啟動rsync而不用它,有點舍近求遠的味道。

[root@mail test]# rsync --daemon --config=/etc/rsyncd.conf

[root@mail test]# pgrep -l rsync 

5132 rsync

[root@mail test]# pkill rsync

[root@mail test]# pgrep -l rsync

------------------------------------------------------------------------------------------

如果你是用Centos 7.3 x64做服務(wù)端,請看下面的配置

[root@mail test]# rpm -qa|grep rsync

rsync-3.0.9-17.el7.x86_64

4、配置rsync的配置文件

vi /etc/rsyncd.conf

uid = root

gid = root

use chroot = no

port = 873

max connections = 20

timeout = 200

secrets file = /etc/rsync_pass

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log

lock file = /var/run/rsyncd.lock

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

 

[backup]

path = /u01

comment = Rsync share test 

list = yes

auth users = ruser

read only = yes

hosts allow = 110.210.250.58

hosts deny = *

[root@mail test]# yum install xinetd.x86_64

[root@mail test]# rpm -qa xinetd

xinetd-2.3.15-13.el7.x86_64

再修改配置: # vi /etc/xinetd.d/rsync

service rsync

{

        disable = NO

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

把原來的disable ,由YES改成NO

安裝完成后,將xinetd服務(wù)加入開機自啟動:

[root@mail test]#  systemctl enable xinetd.service

最后,重新啟動服務(wù)即可:

[root@mail test]#  systemctl restart xinetd

檢查是否出現(xiàn)873端口

[root@mail test]# netstat -natp

tcp6     0      0 :::873     :::*       LISTEN      25167/xinetd 

加入防火墻允許

[root@mail test]# firewall-cmd --add-service=rsyncd --permanent

[root@mail test]# firewall-cmd --reload

[root@mail test]# systemctl restart firewalld


-------------------------------------------------------------------------------------------

二、客戶端的配置

在目標服務(wù)器250.58中配置

[root@vmevan test]#  yum -y install rsync

[root@vmevan test]#  vi /etc/rsync_pass

123456

注意,客戶端的密碼文件只需要密碼,而不需要用戶名!

[root@vmevan test]# chmod 600 /etc/rsync_pass


建立一個備份用的文件夾

[root@vmevan test]# mkdir /bakcup200.12

在客戶端中,有些文件(如named.run)不需要同步,所以要添加到排除列表exclude.list中去。

[root@vmevan test]# vi /etc/exclude.list

named.run

[root@vmevan test]# chmod 600 /etc/exclude.list

[root@vmevan test]# vi /usr/sbin/rsyncDNS

#!/bin/bash

# by evan.li  2017.6.21

rsync -vzrtopgu --progress --delete --exclude-from="/etc/exclude.list" --password-file=/etc/rsync_pass  ruser@110.112.200.12::backup /bakcup200.12

[root@vmevan test]# chmod +x /usr/sbin/rsyncdns

每1小時同步

[root@vmevan test]# vi /etc/crontab

0 */1 * * * root /usr/sbin/rsyncdns

1、在客戶端中,設(shè)定異地主機之間的同步 

[root@vmevan test]#  rsync -vzrtopgu --progress --delete --password-file=/etc/rsync_pass   ruser@110.112.200.12::backup /bakcup200.12

或者用

[root@vmevan test]# rsync -zrtopgu --delete --password-file=/etc/rsync_pass   ruser@110.112.200.12::backup  /bakcup200.12

這個命令行中-vzrtopg里的v是verbose,詳細的。

z是壓縮傳輸,

r是recursive,遞歸。

topg都是保持文件原有屬性如屬主、時間的參數(shù)。

u是只同步已經(jīng)更新的文件,避免沒有更新的文件被重復(fù)更新一次,不過要注意兩者機器的時鐘的同步。

–progress是指顯示出詳細的進度情況,

–delete是指如果服務(wù)器端刪除了這一文件,那么客戶端也相應(yīng)把文件刪除,保持真正的一致。

后 面的ruser@110.112.200.12::backup中,之后的backup是模塊名, 也就是在/etc/rsyncd.conf中自定義的名稱, ruser是指定模塊中指定的可以同步的用戶名。

最后的/bakcup200.12是備份到本地的目錄名。

在這里面,還可以用-e ssh的參數(shù)建立起加密的連接。

可以用–password-file=/password/path/file來指定密碼文件,這樣就可以在腳本中使用而無需交互式地輸入驗證密碼了,這里需要注意的是這份密碼文件權(quán)限屬性要設(shè)得只有屬主可讀。

在客戶端中,將rsync放入crontab計劃任務(wù),每天早上5點同步一次

[root@vmevan test]# vi /etc/crontab 

0 5 * * * root /usr/bin/rsync -vzrtopgu --delete --password-file=/etc/rsync_pass   ruser@114.112.200.12::backup  /bakcup200.12

50 6 * * * root /usr/bin/rsync -avu –progress –delete /u01  /u03/backup

2、在服務(wù)端(源服務(wù)器中)中, 能建立本地目錄之間的同步 

[root@vmevan test]#  rsync -avu -progress -delete  /u01/555  /mnt/sdb1/u01

將源目錄/u01/555,同步放到/mnt/sdb1/u01文件夾下。對src-dir目錄內(nèi)容向dst-dir目錄下進行差異更新,有增加/更新則添加替換,有減少則對其刪減 。

測試完成于2017.06.21

by evan.li

當前文章:CentOS6.x7.x中用rsync遠程同步文件
網(wǎng)站鏈接:http://bm7419.com/article0/gigpio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作網(wǎng)站制作、域名注冊、企業(yè)網(wǎng)站制作、微信小程序網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司