搭建rsync服务

版权声明:勇哥出品必属精品违者必究,相信勇哥幸福一生,不信勇哥抱憾终身,盗版一时爽全家火葬场! https://blog.csdn.net/weixin_42837637/article/details/82763118


1.功能说明

1.Rsync基本概述

rsync是一款开源的备份工具,可以在不同主机之间进行同步,
可实现全量备份与增量备份,因此非常适合用于架构集中式备份或异地备份等应用。
rsync监听端口:873
rsync运行模式:C/S 客户端/服务端 B/S 浏览器/服务端

2.Rsync应用场景

关于数据同步的两种方式
01.rsync应用场景-上传(推)
会导致数据同步缓慢(适合少量数据备份) 在这里插入图片描述
02.rsync应用场景-下载(拉)
会导致备份服务器开销大 在这里插入图片描述
03.rsync应用场景-多server备份
在这里插入图片描述
04.异地备份实现思路
在这里插入图片描述

3.Rsync传输模式

Rsync大致使用三种主要的数据传输方式
01.本地方式cp PC U盘

    Local:  rsync [OPTION...] SRC... [DEST]
    [root@backup ~]# rsync /etc/passwd /tmp/
    [root@backup ~]# ls /tmp/passwd 
    /tmp/passwd

02.远程方式scp

     Access via remote shell: 远程传输
     Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]  下载(拉)
     Push: rsync [OPTION...] SRC... [USER@]HOST:DEST    上传(推)
     
下载pull``
[root@nfs ~]# pwd
/root
[root@nfs ~]# echo "This Nfs" > file
[root@backup ~]# rsync -avz [email protected]:/root/file /opt/
[root@backup ~]# cat /opt/file
This Nfs
上传push(将backup的file2文件上传至NFS服务器的/mnt目录)
[root@backup ~]# pwd
/root
[root@backup ~]# echo "This Rsync" > file2
[root@backup ~]# rsync -avz /root/file2 [email protected]:/mnt
[root@nfs ~]# cat /mnt/file2 
This Rsync
推送目录(推送/root/目录下面的所有文件和目录,不会推送/root/目录本身)
[root@backup ~]# rsync -avz /root/ [email protected]:/tmp
推送目录,推送目录本身以及目录下面的所有文件
[root@backup ~]# rsync -avz /root [email protected]:/tmp
远程方式存在的缺陷:
        1.需要使用系统用户(不安全)
        2.使用普通用户(权限存在问题)
        3.需要走ssh协议

03.守护进程(服务,持续后台运行)

Access via rsync daemon:    守护进程方式传输
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] 下载
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST

首先环境说明

服务器说明 ip地址 应用 系统
源服务器 192.168.100.100 rsync inotify tools脚本 red hat7
目标服务器 192.168.100.129 rsync centos7

1.部署rsync+inotify同步/dxk目录至目标服务器/tmp/dxk下
部署rsync服务同步
部署rsync+inotify同步/qinyong目录至目标服务器/tmp/qinyong下
源服务器

一、搭建rsync服务

1.源服务器和目标服务器

关闭源服务器和目标服务器的防火墙和SELINUX

[root@localhost ~]# systemctl stop firewalld(源服务器)
[root@localhost ~]# getenforce 
Disabled
[root@localhost ~]# systemctl stop firewalld(目标服务器)
[root@localhost ~]# getenforce
Disabled

二、安装rsync

[root@localhost ~]# yum install rsync -y

三、目标服务器配置

1.创建用户认证文件

[root@localhost run]# echo 'qinyong:123456'>/etc/rsync.pass
[root@localhost run]# cat /etc/rsync.pass 
qinyong:123456

2.修改rsyncd.conf配置文件

[root@localhost ~]# cat >> /etc/rsyncd.conf <<EOF
> log file = /var/log/rsyncd.log
> pidfile = /var/run/rsyncd.pid
> lock file = /var/run/rsync.lock
> secrets file = /etc/rsync.pass
> [qinyong] 
> path = /nihao/
> comment = sync etc from client
> uid = root
> gid = root
> port = 873
> ignore errors
> use chroot = no
> read only = no
> list = no
> max connections = 200
> timeout = 600
> auth users = qinyong
> EOF

3.设置文件权限

[root@localhost ~]# chmod 600 /etc/rsync*
[root@localhost ~]# ll /etc/rsync*
-rw------- 1 root root 793 9月  19 10:03 /etc/rsyncd.conf
-rw------- 1 root root  16 9月  19 10:06 /etc/rsync.pass

4.启动rsync服务并设置为开机自启

[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

四、源服务器配置

1.安装rsync服务端软件

[root@localhost ~]# yum install epel-release -y

2.创建认证密码文件,并设置权限为600

[root@localhost ~]# echo '123456' > /etc/rsync.pass
[root@localhost ~]# chmod 600 /etc/rsync.pass 
[root@localhost ~]# ll /etc/rsync.pass
-rw------- 1 root root 8 9月  19 10:32 /etc/rsync.pass

3.创建测试目录,运行命令

[root@localhost ~]# mkdir -pv /root/nihao
mkdir: 已创建目录 "/root/nihao"
[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/nihao/ [email protected]::qinyong --password-file=/etc/rsync.pass
sending incremental file list
./
.inotify.sh.swp
          4,096 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=4/6)
abc
             20 100%   26.53kB/s    0:00:00 (xfr#2, to-chk=3/6)
nohup.out
            151 100%  230.46kB/s    0:00:00 (xfr#3, to-chk=2/6)
123/
aaaa/

sent 4,586 bytes  received 92 bytes  9,356.00 bytes/sec
total size is 4,267  speedup is 0.91

4.安装inotify-tools

[root@localhost ~]# yum install inotify-tools

5.配置同步脚本

[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch inotify.sh
[root@localhost scripts]# chmod 755 inotify.sh 
[root@localhost scripts]# vim inotify.sh 

host=192.168.100.129
src=/root/nihao
des=qinyong
password=/etc/rsync.pass
user=root
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
 rsync -avzP --delete --timeout=100 --password-file=${password} $src $u
ser@$host::$des
 echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

6.启动脚本

[root@localhost scripts]# nohup bash /scripts/inotify.sh &
[1] 59758

五、验证结果

1.源服务器

[root@localhost ~]# cd nihao/
[root@localhost nihao]# ls
55  da  hehe  nohup.out

2.目标服务器

[root@localhost tmp]# cd /qinyong/
[root@localhost qinyong]# ls
55  da  hehe  nohup.out

3.源服务器

[root@localhost nihao]# mkdir qinyong
[root@localhost nihao]# ls
dad  555  aaa nihao  nohup.out
[root@localhost nihaoi]# touch lala

4.目标服务器

[root@localhost qinyong]# ls
dad  555  aaa nihao  nohup.out lala

猜你喜欢

转载自blog.csdn.net/weixin_42837637/article/details/82763118