rsync

  • rsync已关闭评论
  • 125 次浏览
  • A+
所属分类:linux技术
摘要

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


rsync

rsync是什么

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

rsync特性

1)可以镜像保存整个目录树和文件系统。  2)可以很容易做到保持原来文件的权限、时间、软硬连接等。  3)无需特殊权限即可安装。  4)快速:第一次同步时rsync复制全部内容,但在下一次值传输修改过的内容  5)压缩传输:rysnc在传输的过程中可以实行压缩及解压缩操作,可以使用更少的带宽  6)安全:可以使用scp、ssh等方式来进行文件传输  7)支持匿名传输,以方便进行网站镜像  8)rsync不仅可以远程同步数据(类似于scp),而且可以本地同步数据(类似于cp),做差异同步  9)openssh 8.0已经把scp标记为过时不建议使用了。建议用sftp或者rsync替代scp 

rsync的ssh认证协议

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的命令格式常用的有以下三种:     rsync [OPTION]... SRC DEST     rsync [OPTION]... SRC [USER@]HOST:DEST     rsync [OPTION]... [USER@]HOST:SRC DEST 
rsync常用选项:     -a, --archive       //归档     -v, --verbose       //详细模式输出     -q, --quiet         //精简输出模式     -r, --recursive     //递归     -p, --perms         //保持原有的权限属性     -z, --compress      //在传输时压缩,节省带宽,加快传输速度     --delete            //在源服务器上做的删除操作也会在目标服务器上同步 

rsync实时同步

环境说明:

服务器类型 IP地址 应用 操作系统
源服务器 192.168.111.135 rsync inotify-tools 脚本 centos8
目标服务器 192.168.111.137 rsync centos8

需求:

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

两台虚拟机都需要关闭防火墙

[root@localhost ~]# systemctl disable --now firewalld [root@localhost ~]# setenforce 0 [root@localhost ~]# vim /etc/selinux/config  SELINUX=disabled 

目标服务器上做以下操作:

[root@localhost ~]# hostnamectl set-hostname 137 [root@localhost ~]# bash  安装rsync [root@137 ~]# dnf -y install rsync [root@137 ~]# yum -y install rsync-daemon  #修改配置文件 [root@137 ~]# vim /etc/rsyncd.conf 		 log file = /var/log/rsyncd.log		#添加内容 pidfile = /var/run/rsyncd.pid lock file = /var/run/rsync.lock secrets file = /etc/rsync.pass  [etc_from_client] 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  #创建用户认证文件 [root@137 ~]# echo 'admin:123456' > /etc/rsync.pass [root@137 ~]# cat /etc/rsync.pass admin:123456  #设置文件权限 [root@137 ~]# chmod 600 /etc/rsync* [root@137 ~]# ll /etc/rsync* -rw-------. 1 root root  13 Sep 22 18:41 /etc/rsync.pass -rw-------. 1 root root 790 Sep 22 18:39 /etc/rsyncd.conf  #启动rsync服务并设置开机自启 [root@137 ~]# systemctl enable --now rsyncd Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service. [root@137 ~]# ss -anlt  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@localhost ~]# hostnamectl set-hostname 135 [root@localhost ~]# bash  #安装rsync服务端软件,只需要安装,不要启动,不需要配置 [root@135 ~]# yum -y install rsync  #创建认密码文件 [root@135 ~]# echo '123456' > /etc/rsync.pass [root@135 ~]# cat /etc/rsync.pass 123456  #设置文件权限,只设置文件所有者具有读取、写入权限即可 [root@135 ~]# chmod 660 /etc/rsync.pass  [root@135 ~]# ll /etc/rsync.pass  -rw-rw----. 1 root root 7 Sep 22 18:50 /etc/rsync.pass  #在源服务器上创建测试目录,然后在源服务器运行以下命令 [root@135 ~]# mkdir -pv /root/etc/test mkdir: created directory '/root/etc' mkdir: created directory '/root/etc/test' [root@135 ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass sending incremental file list deleting vmware-root_933-3988752732/ ./ test/  sent 77 bytes  received 58 bytes  270.00 bytes/sec total size is 0  speedup is 0.00  #安装inotify-tools工具,实时触发rsync进行同步 [root@135 ~]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r--. 1 root root 0 Sep 22 18:53 max_queued_events -rw-r--r--. 1 root root 0 Sep 22 18:53 max_user_instances -rw-r--r--. 1 root root 0 Sep 22 18:53 max_user_watches 如果有这三个max开头的文件则表示服务器内核支持inotify  #安装inotify-tools [root@135 ~]# yum -y install make gcc gcc-c++ --allowerasing [root@135 ~]# yum -y install inotify-tools  #编写脚本 [root@135 ~]# mkdir /scripts [root@135 ~]# touch /scripts/inotify.sh [root@135 ~]# chmod 755 /scripts/inotify.sh [root@135 ~]# ll /scripts/inotify.sh -rwxr-xr-x. 1 root root 0 Sep 22 19:03 /scripts/inotify.sh [root@135 ~]# vim /scripts/inotify.sh host=192.168.111.137 src=/etc des=etc_from_clien 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@135 ~]# nohup bash /scripts/inotify.sh & [1] 198988 [root@135 ~]# nohup: ignoring input and appending output to 'nohup.out'  [root@135 ~]# ps -ef|grep inotify root      198988   24963  0 19:11 pts/0    00:00:00 bash /scripts/inotify.sh root      198989  198988  0 19:11 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 /etc root      198990  198988  0 19:11 pts/0    00:00:00 bash /scripts/inotify.sh root      200024  199782  0 19:11 pts/2    00:00:00 grep --color=auto inotify 

测试脚本

//在源服务器上生成一个新文件 [root@135 ~]# touch /etc/123  #查看inotify生成的日志 [root@135 ~]# tail /tmp/rsync.log 20220922 19:15 /etc/123CREATE was rsynced 20220922 19:15 /etc/123ATTRIB was rsynced  #在目标服务器中也可以看见创建的123文件 [root@137 ~]# ls etc  test [root@137 ~]# ls etc/123 etc/123 

设置脚本开机自动启动:

[root@135 ~]# chmod +x /etc/rc.d/rc.local [root@135 ~]# vim /etc/rc.d/rc.local # # 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 nohup /bin/bash /scripts/inotify.sh &     //添加此行 
  • 版权声明:本站原创文章,于2022年9月22日21:57:53,由 发表,共 5062 字。
  • 转载请注明:rsync - 张拓的天空