rsync服务

1.rsync

1.1 简介

  1. rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
  2. 不同于 cp 或 scp 的一点是,使用 rsync 命令备份数据时,不会直接覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。

1.2 特性

  • 可以镜像保存整个目录树和文件系统
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等
  • 无须特殊权限即可安装
  • 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
  • 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
    支持匿名传输,以方便进行网站镜像

1.3 认证

rsync同步之前需要认证,认证过程用到ssh和rsync协议。两者区别在于,rsync协议在使用时需要额外配置,增加了工作量,但优势是更加安全;反之,ssh协议使用方便,无需进行配置,但有泄漏服务器密码的风险。

rsync server端不用启动rsync的daemon进程,因此不用配置文件/etc/rsyncd.conf。只要获取remotehost的用户名和密码就可以直接rsync同步文件

2.rsync命令

2.1 安装

  • 需要同步的主机都需要安装rsync
[root@vm3 ~]# yum -y install rsync
.............
Installed:
  rsync-3.1.3-7.el8.x86_64                                                      

2.1 工作模式

rsync [OPTION] SRC DEST              //用于本地备份
rsync [OPTION] SRC [USER@]HOST:DEST  //用于本地备份到远程,通过为ssh协议认证
rsync [OPTION] SRC [USER@]HOST::DEST //用于本地备份到远程,通过为协议rsync认证
rsync [OPTION] [USER@]HOST:SRC DEST  //用于远程备份到本地,通过为ssh协议认证
rsync [OPTION] [USER@]HOST::SRC DEST //用于远程备份到本地,通过为协议rsync认证

2.2 参数

参数 功能
-a 归档模式,表示以递归方式传输文件,
并保持所有属性,-a 后加--no-OPTION,
表示关闭 -r、-l、-p、-t、-g、-o、-D 中的某一个
-v 表示打印一些信息,比如文件列表、文件数量等
-z 传输过程中压缩
--delete 表示删除 DEST 中 SRC 没有的文件
--exclude 指定排除不需要传输的文件,+文件名,
可以是通配符模式(如 *.txt)
-r 递归模式处理子目录,它主要是针对目录
-l 保留软连接
-L 将会把软连接指向的目标文件复制
-p 保持文件权限
-o 保持文件属主信息
-g 保持文件属组信息
-D 保持设备文件信息
-t 保持文件时间信息
-u 把 DEST 中比 SRC 还新的文件排除掉,不会覆盖。
--progress 表示在同步的过程中可以看到同步的过程状态,
比如统计要同步的文件数量、 同步的文件传输速度等

3.测试

[root@vm2 ~]# tree .
.
├── 123.txt
└── rsync
    └── test1
        ├── 1
        ├── 123.txt -> /root/123.txt
        ├── 2
        └── 3
[root@vm2 test1]# ll
total 0
-rw-r--r--. 1 root root  0 Oct 15 03:39 1
lrwxrwxrwx. 1 root root 13 Oct 15 03:54 123.txt -> /root/123.txt
-rw-r--r--. 1 root root  0 Oct 15 03:39 2
-rw-r--r--. 1 root root  0 Oct 15 03:39 3

本地备份

  • 将test1目录里的文件备份到test2里
[root@vm2 rsync]# rsync -av test1 test2
sending incremental file list
created directory test2
test1/
test1/1
test1/123.txt -> /root/123.txt
test1/2
test1/3
[root@vm2 rsync]# tree .
.
├── test1
│   ├── 1
│   ├── 123.txt -> /root/123.txt
│   ├── 2
│   └── 3
└── test2
    └── test1
        ├── 1
        ├── 123.txt -> /root/123.txt
        ├── 2
        └── 3
## 与预期结果不符,修改一下
[root@vm2 rsync]# rm -rf test2
[root@vm2 rsync]# rsync -av test1/ test2/    //需要加上/,表示目录里的内容
sending incremental file list
created directory test2
./
1
123.txt -> /root/123.txt
2
3

sent 246 bytes  received 107 bytes  706.00 bytes/sec
total size is 13  speedup is 0.04
[root@vm2 rsync]# tree .
.
├── test1
│   ├── 1
│   ├── 123.txt -> /root/123.txt
│   ├── 2
│   └── 3
└── test2
    ├── 1
    ├── 123.txt -> /root/123.txt
    ├── 2
    └── 3
  • 将test1目录里的文件备份到test2里(不包含软链接文件)
[root@vm2 rsync]# rsync -av --no-l test1/ test2/
sending incremental file list
created directory test2
skipping non-regular file "123.txt"     //跳过软链接文件
./
1
2
3

sent 229 bytes  received 144 bytes  746.00 bytes/sec
total size is 13  speedup is 0.03
[root@vm2 rsync]# ll test2
total 0
-rw-r--r--. 1 root root 0 Oct 15 03:39 1
-rw-r--r--. 1 root root 0 Oct 15 03:39 2
-rw-r--r--. 1 root root 0 Oct 15 03:39 3

本地备份到远程

[root@vm2 rsync]# ll test1
total 0
-rw-r--r--. 1 root root  0 Oct 15 03:39 1
lrwxrwxrwx. 1 root root 13 Oct 15 03:54 123.txt -> /root/123.txt
-rw-r--r--. 1 root root  0 Oct 15 03:39 2
-rw-r--r--. 1 root root  0 Oct 15 03:39 3
        
[root@vm2 rsync]# rsync -avz /root/rsync/test1/ root@172.16.104.132:/root/test
root@172.16.104.132's password: 
sending incremental file list
created directory /root/test
./
1
123.txt -> /root/123.txt
2
3

sent 237 bytes  received 112 bytes  63.45 bytes/sec
total size is 13  speedup is 0.04

[root@vm2 rsync]# ssh root@172.16.104.132 'ls -l /root/test'
root@172.16.104.132's password: 
total 0
-rw-r--r--. 1 root root  0 Oct 15 03:39 1
lrwxrwxrwx. 1 root root 13 Oct 15 03:54 123.txt -> /root/123.txt
-rw-r--r--. 1 root root  0 Oct 15 03:39 2
-rw-r--r--. 1 root root  0 Oct 15 03:39 3

远程备份到本地

[root@vm2 rsync]# ssh root@172.16.104.132 'ls -l /root/script'
root@172.16.104.132's password: 
total 24
-rwxr-xr-x. 1 root root 2773 Oct  9 23:53 certificate_expect.sh
-rwxr-xr-x. 1 root root 1037 Sep 30 05:37 httpd
-rwxr-xr-x. 1 root root 1751 Oct  9 15:53 install_http.sh
-rwxr-xr-x. 1 root root  347 Sep 30 05:40 list_9x9.sh
-rwxr-xr-x. 1 root root 1220 Oct 10 07:25 openssh.sh
-rwxr-xr-x. 1 root root   68 Oct 10 05:17 test.sh
[root@vm2 rsync]# rsync -avz root@172.16.104.132:/root/script/ /root/rsync/script
root@172.16.104.132's password: 
receiving incremental file list
created directory /root/rsync/script
./
certificate_expect.sh
httpd
install_http.sh
list_9x9.sh
openssh.sh
test.sh

sent 141 bytes  received 3,145 bytes  938.86 bytes/sec
total size is 7,196  speedup is 2.19

[root@vm2 rsync]# ll script/
total 24
-rwxr-xr-x. 1 root root 2773 Oct  9 23:53 certificate_expect.sh
-rwxr-xr-x. 1 root root 1037 Sep 30 05:37 httpd
-rwxr-xr-x. 1 root root 1751 Oct  9 15:53 install_http.sh
-rwxr-xr-x. 1 root root  347 Sep 30 05:40 list_9x9.sh
-rwxr-xr-x. 1 root root 1220 Oct 10 07:25 openssh.sh
-rwxr-xr-x. 1 root root   68 Oct 10 05:17 test.sh

3. rsync+inotify

  • 环境(客户端,服务端)
[root@vm3 ~]# systemctl stop firewalld
[root@vm3 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor >
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@vm3 ~]# setenforce 0
[root@vm3 ~]# getenforce 
Permissive

3.1 服务端(目标端)配置

  • 1.安装软件
##rehat8.0版本需要安装rsync-daemon
[root@vm3 ~]# yum -y install rsync rsync-daemon

  • 2.配置/etc/rsyncd.conf
[root@vm3 ~]# vim /etc/rsyncd.conf 
log file = /var/log/rsyncd.log      ##日志文件,启动服务后自动生成
pidfile = /var/run/rsyncd.pid       ##pid文件
lock file = /var/run/rsync.lock     ##支持max connections参数的锁文件
secrets file = /etc/rsync.pass      ### 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
 
[test]                             ##自定义同步名称
path = /root/test/                 ##rsync服务端数据同步路径
comment = sync vm2:/root/rsync/ to vm3:/root/test/
uid = root
gid = root
port = 873
ignore errors                       
use chroot = no                    ##默认为true,修改为no,增加对目录文件软连接的备份
read only = no
list = no                          ##不显示rsync服务端资源列表
max connections = 200
timeout = 600
auth users = tom
hosts allow = 172.16.104.131
  • 3.创建认证用户文件
[root@vm3 ~]# vim  /etc/rsync.pass
tom:123456
[root@vm3 ~]# chmod 600 /etc/rsync*          ##修改文件权限,保护认证文件
[root@vm3 ~]# ll /etc/rsync*
-rw-------. 1 root root 824 10月 16 01:45 /etc/rsyncd.conf
-rw-------. 1 root root  11 10月 16 01:48 /etc/rsync.pass
  • 4.启动rsync服务并设置开机自启
[root@vm3 ~]# systemctl start rsyncd
[root@vm3 ~]# systemctl enable rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@vm3 ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor pres>
   Active: active (running) since Fri 2020-10-16 01:51:47 CST; 22s ago
 Main PID: 2159 (rsync)
    Tasks: 1 (limit: 12322)
   Memory: 312.0K
   CGroup: /system.slice/rsyncd.service
           └─2159 /usr/bin/rsync --daemon --no-detach

10月 16 01:51:47 vm3 systemd[1]: Started fast remote file copy program daemon.
[root@vm3 ~]# 

3.2 客户端(源服务器)配置

  • 1.安装软件
##inotify-tools软件包需要搭建阿里epel源仓库
##且需要检测系统内核是否支持inotify
##rsync只需要安装不用开启服务或配置
[root@vm2 ~]# ll /proc/sys/fs/inotify/    ##以下三个文件都存在时支持inotify
总用量 0
-rw-r--r--. 1 root root 0 10月 15 05:15 max_queued_events
-rw-r--r--. 1 root root 0 10月 15 05:15 max_user_instances 
-rw-r--r--. 1 root root 0 10月 15 05:15 max_user_watches
[root@vm2 ~]# yum -y install rsync inotify-tools

  • 2.创建认证文件
[root@vm2 ~]# echo "123456" > /etc/rsync.pass
[root@vm2 ~]# cat /etc/rsync.pass 
123456
[root@vm2 ~]# chmod 600 /etc/rsync.pass 
[root@vm2 ~]# ll /etc/rsync.pass 
-rw-------. 1 root root 7 10月 15 05:19 /etc/rsync.pass

  • 3.先测试一下同步功能
##服务器/root/test/目录
[root@vm3 test]# pwd
/root/test
[root@vm3 test]# ls
vm3
##客户端/root/rsync/目录
[root@vm2 rsync]# tree .
.
├── script
│   ├── certificate_expect.sh
│   ├── httpd
│   ├── install_http.sh
│   ├── list_9x9.sh
│   ├── openssh.sh
│   └── test.sh
└── test1
    ├── 1
    ├── 123.txt -> /root/123.txt
    ├── 2
    └── 3

##测试
[root@vm2 rsync]# rsync -avH --port 873 --progress --delete /root/rsync/ tom@172.16.104.132::test --password-file=/etc/rsync.pass 
sending incremental file list
deleting vm3
./
script/
script/certificate_expect.sh
          2,773 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=9/13)
script/httpd
          1,037 100% 1012.70kB/s    0:00:00 (xfr#2, to-chk=8/13)
script/install_http.sh
          1,751 100%    1.67MB/s    0:00:00 (xfr#3, to-chk=7/13)
script/list_9x9.sh
            347 100%  338.87kB/s    0:00:00 (xfr#4, to-chk=6/13)
script/openssh.sh
          1,220 100%    1.16MB/s    0:00:00 (xfr#5, to-chk=5/13)
script/test.sh
             68 100%   66.41kB/s    0:00:00 (xfr#6, to-chk=4/13)
test1/
test1/1
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=3/13)
test1/123.txt -> /root/123.txt
test1/2
              0 100%    0.00kB/s    0:00:00 (xfr#8, to-chk=1/13)
test1/3
              0 100%    0.00kB/s    0:00:00 (xfr#9, to-chk=0/13)

sent 7,944 bytes  received 216 bytes  16,320.00 bytes/sec
total size is 7,209  speedup is 0.88

##查看效果
[root@vm3 test]# tree .
.
├── script
│   ├── certificate_expect.sh
│   ├── httpd
│   ├── install_http.sh
│   ├── list_9x9.sh
│   ├── openssh.sh
│   └── test.sh
└── test1
    ├── 1
    ├── 123.txt -> /rsyncd-munged//root/123.txt
    ├── 2
    └── 3
  • 4.编写脚本自动同步(通过inotify功能实现实时同步)
[root@vm2 ~]# touch inotify.sh
[root@vm2 ~]# chmod 755 inotify.sh 
[root@vm2 ~]# ll 
总用量 0
-rwxr-xr-x. 1 root root  0 10月 15 05:38 inotify.sh
drwxr-xr-x. 4 root root 33 10月 15 04:17 rsync
[root@vm2 ~]# vi inotify.sh 
#!/bin/bash
host=172.16.104.132
src=/root/rsync/
dest=test
password=/etc/rsync.pass
user=tom
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 $user@$host::$dest
        echo "${files} was rsynced" >>/opt/rsync.log 2>&1
done
  • 5.启动脚本
##nohup命令使执行的脚本程序不随着终端的关闭而终止
[root@vm2 ~]# nohup bash /root/inotify.sh 
nohup: 忽略输入并把输出追加到"nohup.out"
  
##另一个终端
[root@vm2 ~]# ps -ef|grep inotify
root       4510   4212  0 05:54 pts/0    00:00:00 bash /root/inotify.sh
root       4511   4510  0 05:54 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/rsync/
root       4512   4510  0 05:54 pts/0    00:00:00 bash /root/inotify.sh
root       4530   4516  0 05:54 pts/3    00:00:00 grep --color=auto inotify

3.3 测试

##客户端
[root@vm2 ~]# cd rsync/
[root@vm2 rsync]# ls
script  test1
[root@vm2 rsync]# touch 123.txt
[root@vm2 rsync]# mkdir hello
[root@vm2 rsync]# touch 456.txt 
[root@vm2 rsync]# rm -f 456.txt 
[root@vm2 rsync]# echo hello >> 123.txt
[root@vm2 rsync]# ll
总用量 4
-rw-r--r--. 1 root root   6 10月 15 05:59 123.txt
drwxr-xr-x. 2 root root   6 10月 15 05:58 hello
drwxr-xr-x. 2 root root 123 10月 10 07:25 script
drwxr-xr-x. 2 root root  48 10月 15 03:54 test1
[root@vm2 rsync]# chown wisan.wisan 123.txt 
-rw-r--r--. 1 wisan wisan   6 10月 15 05:59 123.txt
drwxr-xr-x. 2 root  root    6 10月 15 05:58 hello
drwxr-xr-x. 2 root  root  123 10月 10 07:25 script
drwxr-xr-x. 2 root  root   48 10月 15 03:54 test1
##客户端日志文件
[root@vm2 rsync]# cat /opt/rsync.log 
20201015 06:11 /root/rsync/123.txtCREATE was rsynced
20201015 06:11 /root/rsync/123.txtATTRIB was rsynced
20201015 06:11 /root/rsync/helloCREATE,ISDIR was rsynced
20201015 06:11 /root/rsync/456.txtCREATE was rsynced
20201015 06:11 /root/rsync/456.txtATTRIB was rsynced
20201015 06:11 /root/rsync/456.txtDELETE was rsynced
20201015 06:11 /root/rsync/123.txtMODIFY was rsynced
20201015 06:12 /root/rsync/123.txtATTRIB was rsynced

##服务端
[root@vm3 test]# ll
总用量 4
-rw-r--r--. 1 wisan wisan   6 10月 15 06:11 123.txt
drwxr-xr-x. 2 root  root    6 10月 15 06:11 hello
drwxr-xr-x. 2 root  root  123 10月 10 07:25 script
drwxr-xr-x. 2 root  root   48 10月 15 03:54 test1

3.4 将客户端脚本设置为开机自启

  • /etc/rc.local开机最后执行的位置
[root@vm2 rsync]# vi /etc/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

/usr/bin/nohup /bin/bash /root/inotify.sh
  • 添加rc.local执行权限
[root@vm2 rsync]# chmod +x /etc/rc.local
[root@vm2 rsync]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Sep 10 18:01 /etc/rc.local -> rc.d/rc.local
  • 测试
[root@vm2 rsync]# reboot
Connection to 172.16.104.131 closed by remote host.
Connection to 172.16.104.131 closed.
[wisan@fyj ~]$ ssh root@172.16.104.131
Last login: Mon Oct 19 09:55:33 2020 from 172.16.104.1
[root@vm2 ~]# ps -ef|grep inotify
root        954    948  0 10:08 ?        00:00:00 /bin/bash /root/inotify.sh
root        965    954  0 10:08 ?        00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/rsync/
root        966    954  0 10:08 ?        00:00:00 /bin/bash /root/inotify.sh
root       1261   1247  0 10:08 pts/1    00:00:00 grep --color=auto inotify
[root@vm2 rsync]# ls
hello  script  test1
[root@vm2 rsync]# touch world
[root@vm2 rsync]# ll
total 0
drwxr-xr-x. 2 root root   6 Oct 15 06:11 hello
drwxr-xr-x. 2 root root 123 Oct 10 07:25 script
drwxr-xr-x. 2 root root  48 Oct 15 03:54 test1
-rw-r--r--. 1 root root   0 Oct 19 10:10 world
[root@vm2 rsync]# tail /opt/rsync.log 
20201015 06:11 /root/rsync/123.txtATTRIB was rsynced
20201015 06:11 /root/rsync/helloCREATE,ISDIR was rsynced
20201015 06:11 /root/rsync/456.txtCREATE was rsynced
20201015 06:11 /root/rsync/456.txtATTRIB was rsynced
20201015 06:11 /root/rsync/456.txtDELETE was rsynced
20201015 06:11 /root/rsync/123.txtMODIFY was rsynced
20201015 06:12 /root/rsync/123.txtATTRIB was rsynced
20201019 09:56 /root/rsync/123.txtDELETE was rsynced
20201019 10:10 /root/rsync/worldCREATE was rsynced
20201019 10:10 /root/rsync/worldATTRIB was rsynced

##服务端
[root@vm3 test]# ll
total 0
drwxr-xr-x. 2 root root   6 Oct 15 06:11 hello
drwxr-xr-x. 2 root root 123 Oct 10 07:25 script
drwxr-xr-x. 2 root root  48 Oct 15 03:54 test1
-rw-r--r--. 1 root root   0 Oct 19 10:10 world
posted @ 2020-10-16 08:56  小芃总  阅读(209)  评论(0)    收藏  举报