rsync

简单利用rsync实现文件的同步

实验环境

192.168.13.234 rsync服务端

192.168.13.226 rsync客户端

服务端的配置

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文件

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

测试

在客户端上测试

向服务端同步文件

将客户端/tmp下的文件通步在服务端的/test下

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

服务端向客户端同步文件

将服务端的/test下的文件通步在客户端/tmp下

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

rsync+inotify的实时文件自动同步

1 配置rsync服务

实验环境

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

测试成功,完成了同步文件

原文地址:https://www.cnblogs.com/lulin9501/p/11607780.html