centos7自帶rsync,今天簡單記錄下。

centos7自帶rsync,今天簡單記錄下。centos

yum -y install rsync #啓動rsync服務bash

systemctl start rsyncd.service systemctl enable rsyncd.service #檢查是否已經成功啓動服務器

netstat -lnp|grep 873測試

rsync安裝配置步驟ui

服務器端:centos7

1.修改默認配置文件/etc/rsyncd.conf,該成以下:spa

# /etc/rsyncd: configuration file for rsync daemon mode.net

# See rsyncd.conf man page for more options.日誌

# configuration example:code

 uid = root                               //設置運行rsync 進程的用戶

 gid = root

 use chroot = no

 max connections = 4

# pid file = /var/run/rsyncd.pid        //CentOS7中yum安裝不需指定pid file 不然報錯

 lock file=/var/run/rsyncd.lock

 log file = /var/log/rsyncd.log      //此文件定義完成後系統會自動建立

 exclude = lost+found/

 transfer logging = yes

 timeout = 900

 ignore nonreadable = yes          //同步時跳過沒有權限的目錄

 dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2            //傳輸時不壓縮的文件

# [ftp]   //系統自帶例子,沒刪

#        path = /home/ftp

#      comment = ftp export area

[simba]          //此名字即客戶端使用rsync來同步的路徑,只是模塊名稱,無需和文件夾名字相同

path=/usr/local/simba    //實際須要同步的路徑

comment=simba    //和中括號里名字同樣就行,備註啓動進程能夠顯示的內容

ignore errors

read only=yes    //表示能夠pull 

write only=no      //表示能夠push

list=no

auth user=rsyncuser    //客戶端獲取文件的身份此用戶並非本機中確實存在的用戶

secrets file=/etc/rsyncd.passwd     //用來認證客戶端的祕鑰文件 格式 USERNAME:PASSWD 此文件權       

                                                        //限必定須要改成600,且屬主必須與運行rsync的用戶一致。

hosts allow=*    //容許全部主機訪問

2.建立密鑰文件

echo 'rsyncuser:123456'>/etc/rsyncd.passwd   //文件用戶名和路徑爲上面定義,別寫錯,密碼本身定

chmod 600 /etc/rsyncd.passwd        //修改權限

3.啓動rsync服務

systemctl start rsyncd.service

systemctl enable rsyncd.service

啓動後能夠查看下日誌,是否正常

tail /var/log/rsyncd.log

是否有以下提示:

rsyncd version 3.0.9 starting, listening on port 873

客戶端:

1.建立密碼文件   

echo '123456' >>/etc/rsyncd.passwd     //注意這裏只須要服務器rsyncd.passwd 中的密碼

chmod 600 /etc/rsyncd.passwd

2.測試

將服務器這個ip下的simba目錄中內容同步到本地客戶機 /tmp目錄

rsync -auv --password-file=/etc/rsyncd.passwd rsyncuser@10.5.5.235::simba /tmp   

若要在crontab中添加自動同步,則必須指定--password-file 且rsyncuser必定爲rsyncd.passwd中定義的,rsynctest 爲服務器端【】中定義的

rsync -vzrtopg --delete  --port 878  --progress /da/ lgsync@192.168.30.171::data  --password-file=/etc/rsyncd.passwd

rsync -vzrtopg --delete  --port 878  --progress lgsync@192.168.30.171::data /da/ --password-file=/etc/rsyncd.passwd

 

服務端

vi /etc/rsyncd.conf

       uid = root
        gid = root
        port = 878
        use chroot = no
        max connections = 100
        timeout = 600
        pid file = /var/run/rsyncd.pid
        lock file = /var/run/rsyncd.lock
        log file = /var/log/rsyncd.log
        exclude = lost+found/
        transfer logging = yes
        ignore nonreadable = yes
        dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
        [data]
        comment=data
        path = /data/rsyncdata/
        ignore errors
        read only = no
        list = no
        hosts allow = *
        auth users = lgsync
        secrets file = /etc/rsyncd.passwd
        [lgbak]
        path = /data/leguan_bak/
        ignore errors
        read only = no
        list = no
        hosts allow = 10.30.188.194 120.35.10.209
        hosts deny = *
        auth users = lgsync
        secrets file = /etc/rsyncd.passwd
 

 

服務端 vi /etc/rsyncd.passwd

lgsync:root@2018

客戶端 vi /etc/rsyncd.passwd

root@2018

rsync -vzrtopg --delete  --port 878  --progress /da/ lgsync@192.168.30.171::data  --password-file=/etc/rsyncd.passwd

rsync -vzrtopg --delete  --port 878  --progress lgsync@192.168.30.171::data /da/ --password-file=/etc/rsyncd.passwd

CentOS / RHEL 7

inotify-tools可經過EPEL存儲庫得到。安裝EPEL:

客戶端

yum install -y epel-release && yum update

而後安裝包:

yum install inotify-tools

 

vi /data/inotify.sh

#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /da/"
RSYNC_CMD="rsync -vzrtopg --delete  --port 878  --progress /da/ lgsync@192.168.30.171::data  --password-file=/etc/rsyncd.passwd"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done

 

 

./inotify.sh

後面運行 nohup ./inotify.sh &

或 nohup sh inotify.sh &
 

v3.14-8.el7。×86_64截至18-18-2018

相關文章
相關標籤/搜索