Rsync使用教程

一、安装

1、直接yum安装

yum install rsync –y

二、配置

1、修改配置文件

vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.137.129
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.137.0/24
[rsync]
    path = /data/
    comment = Document Root of www.gateway.com
    read only = yes
    dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
    auth users = rsync
	secrets file = /etc/rsyncd_users.db

2、启动服务

systemctl start rsyncd

3、编写账户信息

vim /etc/rsyncd_users.db
rsync:123456
chmod 600 /etc/rsyncd_users.db

#客户端创建密码文件
vim /etc/rsyncpasswd
123456
chmod 600/etc/rsyncpasswd

三、同步方式

1、手动同步,删除客户端文件(服务端没有,客户端有)

客户端执行:
rsync -avzH --delete --password-file=/etc/rsyncpasswd [email protected]::rsync  /data

在这里插入图片描述

3、手动同步,不删除客户端文件

rsync -avzH --password-file=/etc/rsyncpasswd [email protected]::rsync  /data

在这里插入图片描述

3、实时同步

#配置免密
ssh-keygen
ssh-copy-id -i id_rsa.pub  [email protected]

(1) 修改内核配置文件

vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

#餐宿含义
max_queue_events:监控队列大小
max_user_instances:最多监控实例数
max_user_watches:每个实例最多监控文件数

#刷新配置
sysctl –p

(2) 编译安装inotify

yum install gcc -y
tar zxcf inotify-tools-3.14.tar.gz

./configure
make && make install

(3) 编写实时监控脚本

vim rsync-inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /data/"
RSYNC_CMD="rsync -azH --delete /data/ 192.168.137.130:/data/"

$INOTIFY_CMD | while read DIRECTORY EVENT FILE

do
    if [ $(pgrep rsync | wc -l) -ge 0 ] ;
    then  $RSYNC_CMD
    fi
done

(4) 执行脚本

nohup bash rsync-inotify.sh  &

(5)服务端

vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
address = 0.0.0.0
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
[rsync]
    path = /data/mongodbdataexport/
    comment = Document Root of www.gateway.com
    read only = yes
    dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
    auth users = rsync
	secrets file = /etc/rsyncd_users.db

#创建同步用户
useradd rsync
echo “123abcABC” |passwd –stdin rsync

(6)客户端

ssh-keygen
cd .ssh/
ssh-copy-id -i id_rsa.pub -p53639  [email protected]
rsync -avzh -e 'ssh -p 53639' --delete [email protected]:/data/mongodbdataexport/  /data/mongodbdataexport/

猜你喜欢

转载自blog.csdn.net/qq_37837432/article/details/121578251