天天看点

搭建rsync服务

文章目录

      • 1.Rsync基本概述
      • 2.Rsync应用场景
      • 3.Rsync传输模式
    • 一、搭建rsync服务
      • 1.源服务器和目标服务器
    • 二、安装rsync
    • 三、目标服务器配置
      • 1.创建用户认证文件
      • 2.修改rsyncd.conf配置文件
      • 3.设置文件权限
      • 4.启动rsync服务并设置为开机自启
    • 四、源服务器配置
      • 1.安装rsync服务端软件
      • 2.创建认证密码文件,并设置权限为600
      • 3.创建测试目录,运行命令
      • 4.安装inotify-tools
      • 5.配置同步脚本
      • 6.启动脚本
    • 五、验证结果
      • 1.源服务器
      • 2.目标服务器
      • 3.源服务器
      • 4.目标服务器

1.功能说明

1.Rsync基本概述

rsync是一款开源的备份工具,可以在不同主机之间进行同步,

可实现全量备份与增量备份,因此非常适合用于架构集中式备份或异地备份等应用。

rsync监听端口:873

rsync运行模式:C/S 客户端/服务端 B/S 浏览器/服务端

2.Rsync应用场景

关于数据同步的两种方式

01.rsync应用场景-上传(推)

会导致数据同步缓慢(适合少量数据备份)

搭建rsync服务

02.rsync应用场景-下载(拉)

会导致备份服务器开销大

搭建rsync服务

03.rsync应用场景-多server备份

搭建rsync服务

04.异地备份实现思路

搭建rsync服务

3.Rsync传输模式

Rsync大致使用三种主要的数据传输方式

01.本地方式cp PC U盘

Local:  rsync [OPTION...] SRC... [DEST]
    [[email protected] ~]# rsync /etc/passwd /tmp/
    [[email protected] ~]# 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``
[[email protected] ~]# pwd
/root
[[email protected] ~]# echo "This Nfs" > file
[[email protected] ~]# rsync -avz [email protected]:/root/file /opt/
[[email protected] ~]# cat /opt/file
This Nfs
上传push(将backup的file2文件上传至NFS服务器的/mnt目录)
[[email protected] ~]# pwd
/root
[[email protected] ~]# echo "This Rsync" > file2
[[email protected] ~]# rsync -avz /root/file2 [email protected]:/mnt
[[email protected] ~]# cat /mnt/file2 
This Rsync
推送目录(推送/root/目录下面的所有文件和目录,不会推送/root/目录本身)
[[email protected] ~]# rsync -avz /root/ [email protected]:/tmp
推送目录,推送目录本身以及目录下面的所有文件
[[email protected] ~]# 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

[[email protected] ~]# systemctl stop firewalld(源服务器)
[[email protected] ~]# getenforce 
Disabled
[[email protected] ~]# systemctl stop firewalld(目标服务器)
[[email protected] ~]# getenforce
Disabled
           

二、安装rsync

[[email protected] ~]# yum install rsync -y
           

三、目标服务器配置

1.创建用户认证文件

[[email protected] run]# echo 'qinyong:123456'>/etc/rsync.pass
[[email protected] run]# cat /etc/rsync.pass 
qinyong:123456
           

2.修改rsyncd.conf配置文件

[[email protected] ~]# 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.设置文件权限

[[email protected] ~]# chmod 600 /etc/rsync*
[[email protected] ~]# 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服务并设置为开机自启

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

四、源服务器配置

1.安装rsync服务端软件

[[email protected] ~]# yum install epel-release -y
           

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

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

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

[[email protected] ~]# mkdir -pv /root/nihao
mkdir: 已创建目录 "/root/nihao"
[[email protected] ~]# 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

[[email protected] ~]# yum install inotify-tools
           

5.配置同步脚本

[[email protected] ~]# mkdir /scripts
[[email protected] ~]# cd /scripts/
[[email protected] scripts]# touch inotify.sh
[[email protected] scripts]# chmod 755 inotify.sh 
[[email protected] 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.启动脚本

[[email protected] scripts]# nohup bash /scripts/inotify.sh &
[1] 59758
           

五、验证结果

1.源服务器

[[email protected] ~]# cd nihao/
[[email protected] nihao]# ls
55  da  hehe  nohup.out
           

2.目标服务器

[[email protected] tmp]# cd /qinyong/
[[email protected] qinyong]# ls
55  da  hehe  nohup.out
           

3.源服务器

[[email protected] nihao]# mkdir qinyong
[[email protected] nihao]# ls
dad  555  aaa nihao  nohup.out
[[email protected] nihaoi]# touch lala
           

4.目标服务器

[[email protected] qinyong]# ls
dad  555  aaa nihao  nohup.out lala
           

继续阅读