天天看点

rsyncrsync简介rsync特性rsync的基础用法rsync+inotify同步备份

rsync

  • rsync简介
  • rsync特性
  • rsync的基础用法
  • rsync+inotify同步备份
      • 1.目标主机配置
      • 2.源主机配置
      • 3.测试同步

rsync简介

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

rsync特性

rsync支持很多特性:

可以镜像保存整个目录树和文件系统

可以很容易做到保持原来文件的权限、时间、软硬链接等等

无须特殊权限即可安装

快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽

安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接

支持匿名传输,以方便进行网站镜像*

所以rsync是专门用来备份成百上千的小文件(目录),不是用来备份大数据

rsync的基础用法

主机名 IP
192(源主机) 192.168.30.138/24
zyy180(目标主机) 192.168.30.150/24

rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:

ssh协议

rsync协议

rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件

rsync server端因为不用启动daemon进程,所以也不用配置/etc/rsyncd.conf

ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道

//Rsync的命令格式常用的有以下三种:
    rsync [OPTION]... 目录 备份目录                         ##在本地
    rsync [OPTION]... 目录 [USER@]HOST:备份目录        ##将目录备份到其他主机
    rsync [OPTION]... [USER@]HOST:目录   备份目录     ##将其他主机目录本分到本地

//rsync常用选项:
    -a, --archive       //归档
    -v, --verbose       //啰嗦模式
    -q, --quiet         //静默模式
    -r, --recursive     //递归
    -p, --perms         //保持原有的权限属性
    -z, --compress      //在传输时压缩,节省带宽,加快传输速度
    --delete            //在源服务器上做的删除操作也会在目标服务器上同步




两边都需要安装rsync

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

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





1.本地备份
[[email protected] zyy]# pwd
/zyy
[[email protected] zyy]# ls
123  acc  yyz

[[email protected] /]# rsync -avz /zyy /yyz
sending incremental file list
created directory /yyz
zyy/
zyy/123
zyy/acc
zyy/yyz/
zyy/yyz/456/






2.备份到其他主机
[[email protected] /]# rsync -avz /zyy -e ssh [email protected]:/
[email protected]'s password: 
sending incremental file list
zyy/
zyy/123
zyy/acc
zyy/yyz/
zyy/yyz/456/


[[email protected] /]# ls zyy/
123  acc  yyz





3.把其他主机目录备份到本地
[[email protected] /]# tree 777
777
├── 666
├── 888
└── zyy
    └── yyz


[[email protected] /]# rsync -avz 192.168.30.150:/777 /
[email protected]'s password: 
receiving incremental file list
777/
777/666
777/888
777/zyy/
777/zyy/yyz/


[[email protected] /]# ls 777
666  888  zyy

           

rsync+inotify同步备份

Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,就可以监控文件系统下文件的各种变化情况.

在前面有讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

rsync+inotify就可以实现实时同步目录

源主机
[[email protected] /]# systemctl stop firewalld
[[email protected] /]# systemctl disable firewalld

[[email protected] ~]# yum -y install rsync
[[email protected] /]# yum -y install inotify-tools.x86_64 




目标主机
[[email protected] ~]# yum -y install rsync
[[email protected] /]# systemctl stop firewalld
[[email protected] /]# systemctl disable firewald
           

1.目标主机配置

1.编写rsyncd.conf配置文件(在目标主机上执行)
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   

[zyy]                            ##自行设定模块名称
path = /tmp/                                ##存放位置
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 = admin          ##执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.30.138               ##允许该主机备份到本机的IP
#hosts deny = 192.168.1.1  
EOF





2.创建用户认证文件
[[email protected] /]# echo 'admin:1' > /etc/rsync.pass       要与配置文件对应





3.设置文件权限
[[email protected] /]# chmod 600 /etc/rsync*





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.
[[email protected] /]# ss -anlt
State       Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN      0      128                *:22                             *:*                  
LISTEN      0      100        127.0.0.1:25                             *:*                  
LISTEN      0      5                  *:873                            *:*                  
LISTEN      0      128               :::22                            :::*                  
LISTEN      0      100              ::1:25                            :::*                  
LISTEN      0      5                 :::873                           :::*          

           

2.源主机配置

5.创建认证密码文件
[root@192 /]# echo '1' > /etc//rsync.pass




6.设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@192 /]# chmod 600 /etc/rsync.pass 
[root@192 /]# ll /etc/rsync.pass 
-rw-------. 1 root root 2 7月  27 20:48 /etc/rsync.pass




7.测试
[root@192 /]# tree runtime/
runtime/
├── 123
│   └── 456
└── zyy


[root@192 /]# rsync -avH --port 873 --progress --delete /runtime [email protected]::zyy --password-file=/etc/rsync.pass
sending incremental file list
runtime/
runtime/zyy
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)
runtime/123/
runtime/123/456/



成功
[[email protected] tmp]# tree runtime/
runtime/
├── 123
│   └── 456
└── zyy




8.写同步脚本,让脚本自行去完成同步动作
[root@192 /]# mkdir /scripts               创建脚本目录
[root@192 /]# cd scripts/
[root@192 scripts]# touch inotify.sh
[root@192 scripts]# chmod  755  inotify.sh 



[root@192 scripts]# vim inotify.sh 

host=192.168.30.150     
src=/runtime    
des=zyy                 
password=/etc/rsync.pass        
user=admin          
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::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done





9.放入后台执行,并设置为开机自启
[root@192 scripts]# nohup bash /scripts/inotify.sh &
[1] 72133
[root@192 scripts]# nohup: 忽略输入并把输出追加到"nohup.out"



开机自启
[root@192 ~]# chmod +x /etc/rc.d/rc.local 
[root@192 ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local

           

3.测试同步

[root@192 runtime]# mkdir 456

[[email protected] runtime]# ls
123  456  zyy




[root@192 runtime]# mkdir 523522

[[email protected] runtime]# ls
123  456  523522  zyy




[root@192 runtime]# rm -rf 123

[[email protected] runtime]# ls
456  523522  zyy




[root@192 ~]# tail /tmp/rsync.log
20200727 21:09 /runtime/456CREATE,ISDIR was rsynced
20200727 21:10 /runtime/523522CREATE,ISDIR was rsynced
20200727 21:10 /runtime/123/456DELETE,ISDIR was rsynced
20200727 21:10 /runtime/123DELETE,ISDIR was rsynced

           

配置文件解释:

log file = /var/log/rsyncd.log       # 日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid        # pid文件的存放位置
lock file = /var/run/rsync.lock   # 支持max connections参数的锁文件
secrets file = /etc/rsync.pass    # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件

[etc_from_client]     # 自定义同步名称
path = /tmp/          # rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = sync etc from client
uid = root        # 设置rsync运行权限为root
gid = root        # 设置rsync运行权限为root
port = 873        # 默认端口
ignore errors     # 表示出现错误忽略错误
use chroot = no       # 默认为true,修改为no,增加对目录文件软连接的备份
read only = no    # 设置rsync服务端为读写权限
list = no     # 不显示rsync服务端资源列表
max connections = 200     # 最大连接数
timeout = 600     # 设置超时时间
auth users = admin        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 172.16.12.128   # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.1      # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

           

脚本解释:

host=172.16.12.129      # 目标服务器的ip(备份服务器)
src=/etc        # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=etc_from_client     # 自定义的模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.pass        # 执行数据同步的密码文件
user=admin          # 执行数据同步的用户名
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::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1