rsync

簡單利用rsync實現文件的同步

實驗環境 git

192.168.13.234 rsync服務端github

192.168.13.226 rsync客戶端vim

服務端的配置

yum -y install rsync
# rpm -qa|grep rsync
rsync-3.0.9-17.el7.x86_64
uname -r
#3.10.0-514.el7.x86_64
systemctl stop firewalld
rsync --version
# rsync version 3.0.9 protocol version 30
mkdir /test  
chown -R rsync.rsync /data/test

編輯vim /etc/rsyncd.conf文件bash

vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = yes
max connections = 10
pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
timeout = 300
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#       path = /home/ftp
#       comment = ftp export area
[test]
path = /test
ignore errors
read only = false
list = false
hosts allow = *
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password

 

#編輯密碼
echo 'rsync_backup:123456' >/etc/rsync.password
chmod 600 /etc/rsync.password
systemctl restart rsyncd
ss -lntup|grep rsync
#tcp   LISTEN     0     5         *:873                   *:*                   users:(("rsync",pid=10210,fd=4))
#tcp   LISTEN     0     5     [::]:873               [::]:*                   users:(("rsync",pid=10210,fd=5))
cd /test/
touch file{1..4}

客戶端

echo '123456' >/etc/rsync.password
chmod 600 etc/rsync.password
systemctl stop firewalld
yum -y install rsync

rsync --version

測試

在客戶端上測試服務器

向服務端同步文件tcp

將客戶端/tmp下的文件通步在服務端的/test下測試

rsync  -Rav /tmp rsync_backup@192.168.13.234::test   --password-file=/etc/rsync.password

服務端向客戶端同步文件ui

將服務端的/test下的文件通步在客戶端/tmp下atom

rsync -Rav  rsync_backup@192.168.13.234::test   /tmp --password-file=/etc/rsync.password

rsync+inotify的實時文件自動同步

1 配置rsync服務spa

實驗環境

192.168.13.234 rsync的服務器

192.168.13.226 inotify的服務器

實現將192.168.13.226 服務器的/backup 目錄下的文件實時的同步到192.168.13.234 的test模塊下即 /test目錄下

1 在rsync的服務器 配置rsync

具體操做見上面的操做

2 在inotify的服務器 配置inotify

yum -y install gcc*
cd /usr/local/
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify-3.14
make && make install
cd /usr/local/inotify-3.14/

建立監控腳本inotify.sh

[root@laso inotify-3.14]# cat inotify.sh 
#!/bin/bash
#para
host01=192.168.13.234  #rsync服務器地址
src=/backup        #本地監控的目錄
dst=test         #rsync服務器的模塊名稱
user=rsync_backup    #rsync服務器的虛擬用戶
rsync_passfile=/usr/local/inotify-3.14/rsync.password  #本地調用rsync服務的密碼文件
inotify_home=/usr/local/inotify-3.14    #inotify的安裝目錄
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
# --delete 保持文件的一致性
done
exit 0

建立/backup 目錄

mkdir /backup 

建立認證密碼的文件

[root@laso inotify-3.14]# pwd
/usr/local/inotify-3.14
[root@laso inotify-3.14]# cat rsync.password
123456

腳本建立完畢,丟給後臺運行。(開機啓動的話放到rc.local文檔便可)

sh inotify.sh &

檢查服務

[root@laso inotify-3.14]# ps -ef |grep inotify
root     22786 20066  0 11:57 pts/0    00:00:00 sh inotify.sh
root     22787 22786  0 11:57 pts/0    00:00:00 /usr/local/inotify-3.14/bin/inotifywait -mrq --timefmt %d/%m/%y %H:%M --format %T %w%f -e close_write,delete,create,attrib /backup
root     22788 22786  0 11:57 pts/0    00:00:00 sh inotify.sh
root     22790 20066  0 11:57 pts/0    00:00:00 grep --color=auto inotify

測試

在192.168.13.226 inotify的服務器 /backup目錄建立測試文件

[root@laso backup]# touch  inotify{1..14}
[root@laso backup]# ls
inotify1   inotify11 inotify13 inotify2 inotify4 inotify6 inotify8
inotify10 inotify12 inotify14 inotify3 inotify5 inotify7 inotify9

在192.168.13.234 rsync的服務器/test目錄下檢測同步的文件

[root@laso test]# ls
inotify1   inotify11 inotify13 inotify2 inotify4 inotify6 inotify8
inotify10 inotify12 inotify14 inotify3 inotify5 inotify7 inotify9
[root@laso test]# pwd
/test

測試成功,完成了同步文件

相關文章
相關標籤/搜索