rsync使用和介绍

rsync

rsync简介

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

rsync特性

rsync支持很多特性:

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

rsync认证方式

sync有两种常用的认证方式,一种为rsync-daemon方式,另外一种则是ssh。
在一些场合,使用rsync-daemon方式会比较缺乏灵活性,ssh方式则成为首选。

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

//这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh root@172.16.12.129:/DEST 
    -a  //文件宿主变化,时间戳不变
    -z  //压缩数据传输
 
//当遇到要修改端口的时候,我们可以:
rsync -avz /SRC -e "ssh -p2222" root@172.16.12.129:/DEST  
//修改了ssh 协议的端口,默认是22

rsync命令

//Rsync的命令格式常用的有以下三种:
    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]HOST:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST
    
//对应于以上三种命令格式,rsync有三种不同的工作模式:
1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如: 
[root@localhost ~]# rsync -a anaconda-ks.cfg  anaconda-ks.cfg.sh
[root@localhost ~]# ls
anaconda-ks.cfg  anaconda-ks.cfg.sh
[root@localhost ~]# ll
total 8
-rw-------. 1 root root 1087 Sep 22 05:17 anaconda-ks.cfg
-rw-------. 1 root root 1087 Sep 22 05:17 anaconda-ks.cfg.sh
[root@localhost ~]# 
2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包 \
含单个冒号":"分隔符时启动该模式。如:
[root@localhost ~]# rsync -avz anaconda-ks.cfg.sh root@192.168.171.133:/root/
The authenticity of host '192.168.171.133 (192.168.171.133)' can't be established.
ECDSA key fingerprint is SHA256:vhEAEKvc9QvjXaFvfS/FJ17juXp7BA38wPghfiFRRMI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '192.168.171.133' (ECDSA) to the list of known hosts.
root@192.168.171.133's password: 
sending incremental file list
anaconda-ks.cfg.sh

sent 725 bytes  received 35 bytes  66.09 bytes/sec
total size is 1,087  speedup is 1.43
[root@localhost ~]# 
可以看到已经将源主机上的文件拷贝到目标主机上了。
[root@jjyy ~]# ls
anaconda-ks.cfg  anaconda-ks.cfg.sh  lamp
[root@jjyy ~]# 
3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径 \
包含单个冒号":"分隔符时启动该模式。如:
[root@jjyy ~]# mkdir kang
[root@jjyy ~]# ls
anaconda-ks.cfg  anaconda-ks.cfg.sh  kang  lamp
[root@jjyy ~]# 
[root@localhost ~]# rsync -avz root@192.168.171.133:/root/kang /root/root@192.168.171.133's password:  //这里只需要输入目标主机的密码就可以了 
receiving incremental file list
kang/

sent 28 bytes  received 54 bytes  32.80 bytes/sec
total size is 0  speedup is 0.00
可以看到已经将目标主机的东西拷贝到源主机上了
[root@localhost ~]# ls
anaconda-ks.cfg  anaconda-ks.cfg.sh  kang
[root@localhost ~]# 
     
//rsync常用选项:
    -a, --archive       //归档
    -v, --verbose       //啰嗦模式
    -q, --quiet         //静默模式
    -r, --recursive     //递归
    -p, --perms         //保持原有的权限属性
    -z, --compress      //在传输时压缩,节省带宽,加快传输速度
    --delete            //在源服务器上做的删除操作也会在目标服务器上同步

rsync+inotify配置

在内核2.6.13起,加入了inotify支持
inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系统的变化如文件修改、新增、删除等,并可以将相应的事件通知给应用程序。该机制由著名的桌面搜索引擎项目beagle引入用于替代此前具有类似功能但存在诸多缺陷的dnotify。
inotify既可以监控文件,也可以监控目录。当监控目录时,它可以同时监控目录及目录中的各子目录及文件的。此外,inotify 使用文件描述符作为接口,因而可以使用通常的文件I/O操作select、poll和epoll来监视文件系统的变化。

环境说明:

服务器类型IP地址应用操作系统
源服务器192.168.171.137rsync inotify-tools 脚本centos7/redhat7及以上
目标服务器192.168.171.133rsynccentos7/redhat7及以上

需求:

  • 把源服务器上/etc目录实时同步到目标服务器的/tmp/下

在目标主机上做准备工作

[root@jjyyt ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@jjyy ~]# get
getcap       getent       getopt       getsebool    
getconf      getfacl      getopts      gettext      
getenforce   getkeycodes  getpcaps     gettext.sh   
[root@jjyy ~]# getenforce 0
Enforcing
[root@jjyy ~]# setenforce 0
[root@jjyy ~]# 
//安装rsyjjyync服务端软件
[root@jjyy ~]# yum -y install rsync
//下载守护进程的包
[root@jjyy ~]# yum -y install rsync-daemon
#在配置文件里面添加这些
[root@jjyy ~]# cat >> /etc/rsyncd.conf <<EOF
log file = /var/log/rsyncd.log    # 日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid     # pid文件的存放位置
lock file = /var/run/rsync.lock   # 支持max connections参数的锁文件
secrets file = /etc/rsyncd.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        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
EOF
[root@jjyy ~]# echo 'admins:123456' > /etc/rsyncd.pass
[root@jjyy ~]# chmod 600 /etc/rsyncd.pass 
[root@jjyy ~]# ll /etc/rsyncd.pass 
-rw-------. 1 root root 14 Sep 22 06:12 /etc/rsyncd.pass
[root@jjyy ~]# 
//设置服务rsync服务开机自启
[root@jjyy ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@jjyy ~]# ss -antl
State               Recv-Q              Send-Q                           Local Address:Port                           Peer Address:Port             Process              
LISTEN              0                   128                                    0.0.0.0:22                                  0.0.0.0:*                                     
LISTEN              0                   5                                      0.0.0.0:873                                 0.0.0.0:*                                     
LISTEN              0                   128                                       [::]:22                                     [::]:*                                     
LISTEN              0                   5                                         [::]:873                                    [::]:*                                     
[root@jjyy ~]# 

在源服务器上做以下操作

/关闭防火墙与SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
//安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@localhost ~]# yum -y install rsync
[root@localhost ~]# echo '123456' >/etc/rsync.pass
[root@localhost ~]# chmod 600 /etc/rsyncd.pass
[root@localhost ~]# ll /etc/rsync.pass 
-rw-------. 1 root root 7 Sep 22 06:25 /etc/rsync.pass
[root@localhost ~]# 
//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@localhost ~]# mkdir /etc/test/
#然后这里把两个主机之间同步一下,注意这同步时如果同步的目录是/etc  那这就是同步整个目录,如果是/etc/那就是同步etc下的所有东西。
[root@localhost ~]# rsync -avH --port 873 --progress  --delete /etc admin@192.168.171.133::etc_from_client --password-file=/etc/rsyncd.pass
sending incremental file list
etc/
etc/.pwd.lock
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=689/691)
etc/.updated
            208 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=688/691)
etc/DIR_COLORS
          4,536 100%    4.33MB/s    0:00:00 (xfr#3, to-chk=687/691)
etc/DIR_COLORS.256color
          5,214 100%    4.97MB/s    0:00:00 (xfr#4, to-chk=686/691)
etc/DIR_COLORS.lightbgcolor
          4,618 100%    4.40MB/s    0:00:00 (xfr#5, to-chk=685/691)
#测试之间是否同步,可以在刚刚创建的测试目录下创建一个文件。
[root@localhost ~]# touch /etc/test/123
[root@localhost ~]# ls /etc/test/
123
[root@localhost ~]# rsync -avH --port 873 --progress  --delete /etc admin@192.168.171.133::etc_from_client --password-file=/etc/rsyncd.pass
sending incremental file list
etc/test/
etc/test/123
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=51/692)

sent 20,266 bytes  received 256 bytes  41,044.00 bytes/sec
total size is 22,999,343  speedup is 1,120.72
[root@localhost ~]# 
#再去看一下目标主机上test目录下有没有创建的文件
[root@jjyy ~]# ls /tmp/etc/test/
123
[root@jjyy ~]# 

安装使用inotify-tools工具

安装这个工具触发实时同步的功能。

//查看服务器内核是否支持inotify

//如过有这三个文件开头的就是服务器内核支持。
[root@localhost ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Sep 22 06:57 max_queued_events
-rw-r--r--. 1 root root 0 Sep 22 06:57 max_user_instances
-rw-r--r--. 1 root root 0 Sep 22 06:57 max_user_watches
[root@localhost ~]# 

//安装inotify-tools,需要先安装epel扩展包
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install inotify-tools
//写同步脚本
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@localhost ~]# vim /scripts/inotify.sh
[root@localhost ~]# cat /scripts/inotify.sh 
host=192.168.171.133
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
done
[root@localhost ~]# 
//启动脚本
[root@localhost ~]# nohup bash /scripts/inotify.sh &
[1] 232242
[root@localhost ~]# nohup: ignoring input and appending output to 'nohup.out'
[root@localhost ~]# echo "hello" >/etc/test/123
[root@localhost ~]# 
#可以看到这边已经同步过来了。
[root@jjyy ~]# cat /tmp/etc/test/123
hello
[root@jjyy ~]# 
[root@localhost ~]# echo "hello kk" >/etc/test/123
[root@localhost ~]# 
#文件发生修改也会同步
[root@jjyy ~]# cat /tmp/etc/test/123
hello kk
[root@jjyy ~]# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值