rsync是linux系统下的数据镜像备份工具——remotesync。
是用来同步海量小文件
可以镜像保存整个目录树和文件系统
可以很容易做到保持原来文件的权限、时间、软硬链接等等
无须特殊权限即可安装
快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
支持匿名传输,以方便进行网站镜像
什么是套接字?
套接字:进程通过一个为套接字的软件接口向网络发送报文和从网络接受报文。
什么是端口号?
同时,书中表明,如果A主机想要给B主机通讯,需要知道B主机的IP地址,同时还要知道是哪个进程,用于标识进程的标识符就是端口号。
端口:每个端口号对应一个进程。
rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:
第一种:ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道
实例:
#准备两台,两台都需要安装rsync
[root@1 ~]# yum -y install rsync
[root@2 ~]# yum -y install rsync
#在1上面传文件给2,操作其实和scp操作类似
[root@1 ~]# rsync -avz anaconda-ks.cfg root@192.168.230.132:/tmp/
The authenticity of host '192.168.230.132 (192.168.230.132)' can't be established.
ECDSA key fingerprint is SHA256:JsrnPStzaWrA+yjXQWuTAqoe/Nw1gCzY6e6mUZqFVm4.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.230.132' (ECDSA) to the list of known hosts.
root@192.168.230.132's password:
sending incremental file list
anaconda-ks.cfg
sent 847 bytes received 35 bytes 160.36 bytes/sec
total size is 1,253 speedup is 1.42
#去2上查看
[root@2 ~]# ls /tmp/
[root@2 ~]# ls /tmp/
anaconda-ks.cfg
#如果端口号发生改变则使用-e和-p进行端口号指定
[root@1 ~]# rsync -avz anaconda-ks.cfg -e ssh root@192.168.230.132:/tmp/
root@192.168.230.132's password:
sending incremental file list
anaconda-ks.cfg
sent 847 bytes received 35 bytes 352.80 bytes/sec
total size is 1,253 speedup is 1.42
#2上查看
[root@2 ~]# rm -rf /tmp/anaconda-ks.cfg
[root@2 ~]# ls /tmp/
[root@2 ~]# ls /tmp/
anaconda-ks.cfg
//Rsync的命令格式常用的有以下三种:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
1)第一种:拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式(同台主机上拷贝同一个文件,大小,权限都一致)
[root@1 ~]# rsync -avz anaconda-ks.cfg abc
sending incremental file list
anaconda-ks.cfg
sent 847 bytes received 35 bytes 1,764.00 bytes/sec
total size is 1,253 speedup is 1.42
[root@1 ~]# ll
总用量 8
-rw-------. 1 root root 1253 9月 30 10:01 abc
-rw-------. 1 root root 1253 9月 30 10:01 anaconda-ks.cfg
2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包 \含单个冒号":"分隔符时启动该模式。如:(在本地主机下实现,把文件同步到其他目录下)
[root@1 ~]# ls
abc anaconda-ks.cfg
[root@1 ~]# ls /tmp/
[root@1 ~]# rsync -avz abc /tmp/
sending incremental file list
abc
sent 835 bytes received 35 bytes 1,740.00 bytes/sec
total size is 1,253 speedup is 1.44
[root@1 ~]# ll abc /tmp/abc
-rw-------. 1 root root 1253 9月 30 10:01 abc
-rw-------. 1 root root 1253 9月 30 10:01 /tmp/abc
3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径 \包含单个冒号":"分隔符时启动该模式。如:
实例:
[root@1 ~]# rsync -avz /tmp root@192.168.230.132:/opt
root@192.168.230.132's password:
sending incremental file list
tmp/
tmp/abc
sent 870 bytes received 39 bytes 363.60 bytes/sec
total size is 1,253 speedup is 1.38
[root@2 ~]# ls /opt/
abc data tmp
//rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
如果要实现定时同步数据,可以在客户端将rsync加入定时任务,但是定时任务的同步时间粒度并不能达到实时同步的要求。在Linux kernel 2.6.13后提供了inotify文件系统监控机制。通过rsync+inotify组合可以实现实时同步。
是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
环境说明:
服务器类型 | IP地址 | 应用 |
---|---|---|
源服务器 | 192.168.230.131 | rsync |
目标服务器 | 192.168.230.132 | rsync centos7/redhat7 |
需求:
部署rsync+inotify同步/runtime目录至目标服务器的/NAME/下。这里的NAME是指你的名字,比如你叫tom,则要把/runtime目录同步至目标服务器的/tom/下。
实例:
#两台都需要关闭防火墙和selinux
[root@1 ~]# systemctl stop firewalld
[root@1 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@1 ~]# vim /etc/selinux/config
[root@1 ~]# setenforce 0
[root@2 ~]# systemctl stop firewalld
[root@2 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@2 ~]# vim /etc/selinux/config
[root@2 ~]# setenforce 0
#两台安装rsync
[root@1 ~]# yum -y install rsync
[root@2 ~]# yum -y install rsync
#好了后先在第一台主机进行环境配置
#设置rsyncd.conf配置文件
[root@1 ~]# vim /etc/rsyncd.conf
[root@1 ~]# cat /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 = /ll/
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@1 ~]# echo 'admin:123456' > /etc/rsync.pass
[root@1 ~]# cat /etc/rsync.pass
admin:123456
[root@1 ~]# chmod 600 /etc/rsync*
[root@1 ~]# ll /etc/rsync*
-rw-------. 1 root root 330 10月 11 07:26 /etc/rsyncd.conf
-rw-------. 1 root root 13 10月 11 07:28 /etc/rsync.pass
#安装依赖包
[root@1 ~]# yum -y install rsync-daemon
#然后进行开机自启
[root@1 ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@1 ~]# 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 128 127.0.0.1:9000 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@2 ~]# yum -y install epel-release
[root@2 ~]# echo '123456' > /etc/rsync.pass
[root@2 ~]# cat /etc/rsync.pass
123456
[root@2 ~]# chmod 600 /etc/rsync.pass
[root@2 ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 10月 11 08:29 /etc/rsync.pass
所有的环境配置完成,开始测试
[root@2 ~]# mkdir /runtime
[root@2 ~]# yum -y install inotify-tools
[root@2 ~]# mkdir /scripts
[root@2 ~]# cd /scripts/
[root@2 scripts]# ls
[root@2 scripts]# vi inotify.sh
[root@2 scripts]# cat inotify.sh
host=192.168.230.131
src=/runtime
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@2 scripts]# chmod +x inotify.sh
[root@2 scripts]# ll
总用量 4
-rwxr-xr-x. 1 root root 403 10月 11 08:58 inotify.sh
#此时还不会执行什么,所以需要再开个终端创建个文件进行触发
[root@2 ~]# cd /runtime/
[root@2 runtime]# touch lly
[root@2 ~]# /scripts/inotify.sh
sending incremental file list
runtime/
runtime/lly
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2)
sent 121 bytes received 47 bytes 336.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 82 bytes received 25 bytes 214.00 bytes/sec
total size is 0 speedup is 0.00
#在第一台上查看是否成功
[root@1 ~]# cd /ll/
[root@1 ll]# ls
runtime
[root@1 ll]# ll runtime/
总用量 0
-rw-r--r--. 1 root root 0 10月 11 09:11 lly
#设置后台运行,开机自启
[root@2 ~]# chmod +x /etc/rc.d/rc.local
[root@2 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 12月 1 2020 /etc/rc.local -> rc.d/rc.local
[root@2 ~]# vim /etc/rc.d/rc.local
#
# 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 /scripts/inotify.sh &
#最后进行查看
[root@2 ~]# reboot
[root@2 ~]# ps -ef | grep inotify
root 950 1 0 09:29 ? 00:00:00 /bin/sh /scripts/inotify.sh
root 953 950 0 09:29 ? 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /runtime
root 954 950 0 09:29 ? 00:00:00 /bin/sh /scripts/inotify.sh
root 2711 1883 0 09:29 pts/0 00:00:00 grep --color=auto inotify