Rsync是一款开源、快速、多功能、可实现全量及增量的本地或者远程数据同步的优秀工具。并且支持多系统平台运行。Rsync具有本地与远程两台主机之间的数据快速复制同步镜像、远程备份等功能,该功能类似scp,但是优于scp功能,还具有本地不同分区目录之间全量及增量复制数据。

scp同步实验

主机A: scp  192.168.2.2:/root/a.txt    .

Rsync命令格式
SRC:源文件或者目录  DEST:目标目录或者文件
本地: rsync [option...] SRC...    [DEST]
rsync -aP  /etc/passwd  /tmp/1.txt
通过shell远程访问:
拉取: rsync [option...] user@host:src...  [dest]
推送: rsync  [option...] src...  user@host:dest
举例:  rsync -av  192.168.2.2:/root .
     rsync  -av    /root/a.txt    192.168.2.2:/root
实时同步架构(文件、目录的实时同步)
rsync+sersync架构功能实现
serync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录。
应用场景:数据在两台物理服务器上各存储一份,确保第一份备份失效后,第二份有效。
rsync  --daemon
netstat -antp |grep 873
给密码文件赋权限:chmod 600 /etc/rsync.passwd 
远程数据同步rsync -avzP /data/www/  rsync_back@192.168.2.2::www/ --passwordfile=/etc/rsync.passwd
 
 
 
 
使用系统用户的rsync备份的详细案例过程:
实验拓扑 huya23(源主机192.168.1.23)====huya24(目标主机 192.168.1.24)
Rsync服务依赖Xinetd,是使用超级服务来管理的需要在目标机器上安装rsync服务端
[root@huya24 ~]# yum -y install xinetd rsync
[root@huya24 ~]# rsync --daemon
[root@huya24 ~]# netstat -antup | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      2056/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      2056/rsync
使用rsync备份数据
对huya23网站根目录的/var/www/html目录备份到huya24的/web-back
源服务器:huya23
目标服务器:huya24
建立测试用户
[root@huya24 ~]# useradd rget1  ;    echo rget1:123456 | chpasswd
[root@huya23 ~]# useradd rget1   ; echo rget1:123456 | chpasswd
 //测试用户,rget1用于下载
对目录赋予ACL权限
[root@huya23 ~]# mkdir /var/www/html/ -p
[root@huya23 ~]# setfacl -R -m user:rget1:rwx /var/www/html/                          //设置rget1的权限
[root@huya23~]# setfacl -R -m default:rget1:rwx /var/www/html/
[root@huya23 ~]# getfacl  /var/www/html
user::rwx
user:rget1:rwx
default:user:rget1:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
创建测试数据
[root@huya3 ~]# cp -r /boot/* /var/www/html/                  //boot目录下的所有数据作为测试数据
[root@huya24 ~]# mkdir /web-back
[root@huya24 ~]# chown rget1:rget1 -R /web-back/
[root@huya23 ~]# rsync -avz --delete /var/www/html/ rget1@192.168.1.24: /web-back/
 
 
 
 
 
 
使用非系统用户的rsync备份的详细案例过程:
使用系统配置文件/etc/rsyncd.conf来备份数据,创建备份账户,最后把rsync以deamon方式运行
rsyncd.conf配置文件
配置文件分为两部分:全局参数,模块参数
全局参数:对rsync服务器生效,如果模块参数和全局参数冲突,冲突的地方模块参数生效
模块参数:定义需要通过rsync输出的目录定义的参数
用配置文件定义目录输出
 [root@huya24 ~]# vim /etc/rsyncd.conf                    //文件不存在,需要自己创建
【centos6.X 系统上是没有这个文件的,在7系统上,运行来rsync – daemon 就会自动生成这个文件】
 
 
uid = root                           #传输文件时守护进程应该具有的uid身份,默认为nobody
gid = root                            #传输文件时守护进程应该具有组
address =192.168.0.64                       #监听IP,为本机ip地址
port =873                               #监听端口
hosts allow =192.168.0.0/24                        #允许同步客户端的IP地址,可以是网段,或者用*表示所有 192.168.1.0/24或192.168.1.0/255.255.255.0
use chroot = yes                               #是否囚牢,锁定家目录,rsync被黑之后,黑客无法再rsync运行的家目录之外创建文件,选项设置为yes
max connections =5                             #最大连接数
pid file =/var/run/rsyncd.pid          #进程PID,自动生成
lock file =/var/run/rsync.lock         #指max connectios参数的锁文件
log file =/var/log/rsyncd.log          #日志文件位置
motd file =/etc/rsyncd.motd    #客户端登陆之后弹出的消息,需要创建

[wwwroot]                              #共享模块名称
path =/web-back/                      #指定该模块的供备份的目录树路径

comment = used for web-data root    #描述信息
read only = false                                 #设置服务端文件读写权限
list = yes                                         #是否允许查看模块信息,该选项设定当客户请求可以使用的模块列表时,该模块是否应该被列出
auth users = rsyncuser                        #备份的用户,和系统用户无关
secrets file =/etc/rsync.passwd        #存放用户的密码文件,格式是  用户名:密码
 
 
 
 
uid = root                          
gid = root                          
address =192.168.1.24                  
port =873                              
hosts allow =192.168.1.0/24                   
use chroot = yes         
max connections =5                        
pid file =/var/run/rsyncd.pid       
lock file =/var/run/rsync.lock       
log file =/var/log/rsyncd.log  
motd file =/etc/rsyncd.motd
[wwwroot]                             
path =/web-back/                      
comment = used for web-data root    
read only = false                    
list = yes                                     
auth users = rsyncuser                       
secrets file = /etc/rsync.passwd
 
创建提示文件和用户密码
[root@huya24 ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd
[root@huya24 ~]# vim /etc/rsync.passwd
rsyncuser:password123
[root@huya24 ~]# chmod 600 /etc/rsync.passwd                 //目录权限必须是700或者600,否则的话身份验证会失效,设置rsync user的时候
启动服务测试
     启动rsync与xinetd服务
systemctl start xinetd  #启动xinetd服务
systemctl enable xinetd  #将xinetd服务加入开机项
rsync --daemon --config=/etc/rsyncd.conf  #加载配置文件rsyncd.conf启动rsync服务
[root@huya24 ~]# ps aux|grep rsync
root      2056  0.0  0.0 114652   316 ?        Ss   20:53   0:00 rsync --daemon
[root@huya24 ~]# kilall  rsync
[root@huya24 ~]#  rsync --daemon --config=/etc/rsyncd.conf
[root@huya24 ~]# ps aux|grep rsync
root      2155  0.0  0.0 114652   520 ?        Ss   21:33   0:00 rsync --daemon --config=/etc/rsyncd.conf
root      2157  0.0  0.0 112660   972 pts/0    S+   21:33   0:00 grep --color=auto rsync
[root@huya24  ~]# netstat -antup | grep :873
tcp        0      0:::873                      :::*                        LISTEN      45089/xinetd       
 
测试,rsync语法:   rsync 选项 用户名@目标服务器IP::共享模块名 
[root@huya23 ~]# rsync -avz --delete  /var/www/html/ rsyncuser@192.168.1.24::wwwroot
Welcome to Backup Server
Password:       #输入密码password123
 
密码处理
新建一个文件保存好密码,然后在rsync命令中使用--password-file指定此文件即可
[root@huya23 ~]# vim /etc/rsync.passwd 
password123 
[root@huya23 ~]# chmod 600  /etc/rsync.passwd 
[root@huya23 ~]#rsync -avz --delete  /var/www/html rsyncuser@192.168.0.64::wwwroot --password-file=/etc/rsync.passwd 
 
脚本实现定时自动备份
[root@huya23 ~]# vim autobackup.sh
#!/bin/bash
rsync -avz --delete  /var/www/html rsyncuser@192.168.0.64::wwwroot --password-file=/opt/passfile 
[root@huya23 ~]# chmod +x autobackup.sh
[root@huya24 ~]# rm –rf  /web-back/*                       //测试脚本
[root@huya23~]# sh autobackup.sh
[root@huya24 ~]# echo "01 3 * * *  sh /root/autoback.sh &" >> /var/spool/cron/root