RSYNC

rsync

1. rsync简介

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

2. rsync特性

rsync支持很多特性:

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

3. 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打通通道

4. rsync命令

5. rsync+inotify

rsync与传统的cptar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。

环境说明:

服务器类型 IP地址 应用 操作系统
源服务器 192.168.10.145 rsync inotify-tools 脚本 centos7/redhat7
目标服务器 192.168.10.150 rsync centos7/redhat7

需求:

  • 把源服务器上/etc目录实时同步到目标服务器的/tmp/下
//关闭防火墙与SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

//安装rsync服务端软件
[root@localhost ~]# yum -y install rsync
[root@localhost ~]# yum -y install rsync-daemon

//设置rsyncd.conf配置文件
[root@localhost ~]# 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@localhost ~]# 

//创建用户认证文件
[root@localhost ~]# touch /etc/rsync.pass
[root@localhost ~]# echo 'admin:123456' > /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass 
admin:123456
[root@localhost ~]# 
//设置文件权限
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw------- 1 root root 13 Sep 22 22:10 /etc/rsync.pass
//启动rsync服务并设置开机自启动
[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl enable rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@localhost ~]# 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               [::]:*                
LISTEN   0        80                     *:3306                 *:*                
[root@localhost ~]# 

在源服务器上做以下操作

/关闭防火墙与SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
//安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@localhost ~]# yum -y install rsync
//配置yum源
[root@localhost ~]# echo '123456' > /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
123456
//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 Sep 22 22:23 /etc/rsync.pass
//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@localhost ~]# mkdir -pv /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'
[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.10.150::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting .font-unix/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
./
test/

sent 77 bytes  received 99 bytes  352.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# 
//运行完成后,在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
[root@localhost tmp]# ls
test
//安装inotify-tools工具,实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@localhost ~]# dnf -y install epel-release
[root@localhost ~]# dnf -y install inotify-tools
[root@localhost ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Sep 22 23:49 max_queued_events
-rw-r--r--. 1 root root 0 Sep 22 23:49 max_user_instances
-rw-r--r--. 1 root root 0 Sep 22 23:49 max_user_watches
[root@localhost ~]# 
//写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下 \
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch inotify.sh
[root@localhost scripts]# chmod +x inotify.sh 
[root@localhost scripts]# ls
inotify.sh
[root@localhost scripts]# ll /scripts/inotify.sh 
-rwxr-xr-x. 1 root root 0 Sep 22 23:52 /scripts/inotify.sh
[root@localhost scripts]# 
[root@localhost scripts]# vim inotify.sh 
[root@localhost scripts]# cat inotify.sh 
host=192.168.10.150 
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 scripts]# 
[root@localhost scripts]# cd
让脚本开机自运行
[root@localhost ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Oct  5  2021 /etc/rc.local -> rc.d/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local 
[root@localhost ~]# vim /etc/rc.d/rc.local 
nohup /bin/bash /scripts/inotify.sh  //添加此行
手动执行一下脚本
[root@localhost ~]# /scripts/inotify.sh &
[1] 216697
创建一个文件测试一下
[root@localhost ~]# touch /etc/123
[root@localhost tmp]# ls
etc  test
[root@localhost tmp]# cd /etc/
[root@localhost etc]# ls
123

posted @   niebangrui  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示