##rsync
1.rsync简介
- rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
2.rsync特性
rsync支持很多特性:
- 可以镜像保存整个目录树和文件系统
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等
- 无须特殊权限即可安装
- 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
- 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
- 支持匿名传输,以方便进行网站镜象
3.rsync企业工作场景说明
- 利用定时任务实现数据备份(crond+rsync)
- 利用实时同步方式实现数据备份(inotify/sersync+rsync)
4.rsync的ssh认证协议
默认是省略-e ssh的:
‘rsync -avz /SRC -e ssh root@192.168.228.20:/DEST’
-a 文件宿主变化,时间戳不变
-z 压缩数据传输
修改端口:
‘rsync -avz /SRC -e “ssh -p2222” root@192.168.228.20:/DEST’(修改ssh协议的端口,默认是22)
5.rsync常见选项
rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
–delete //在源服务器上做的删除操作也会在目标服务器上同步
6.rsync+inotify
- Inotify 是一种强大的,细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是第三方软件。
- rsync 可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据与实际数据会有差异,但是inotify可以监控文件系统的各种变化。
环境说明:
服务器类型 | IP地址 | 应用 |
---|---|---|
源服务器 | 192.168.26.128 | rsyncinotify-tools 脚本 |
目标服务器 | 192.168.26.129 | rsync |
需求:
* 把源服务器上/etc目录实时同步到目标服务器的/tmp/下
在目标服务器上做以下操作:
- 关闭防火墙与selinux
[root@wuliyong ~]# systemctl stop firewalld
[root@wuliyong ~]# systemctl disable firewalld
[root@wuliyong ~]# getenforce
Enforcing
[root@wuliyong ~]# setenforce 0
[root@wuliyong ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
- 安装rsync服务端软件
[root@wuliyong ~]# yum -y install rsync
- 设置rsyncd.conf配置文件
[root@wuliyong ~]# cat >> /etc/rsyncd.conf <<EOF
> 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 = /wuzhiyong/
> 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
> hosts allow = 192.168.26.128
> hosts deny = 192.168.1.1
> EOF
- 创建用户认证文件
[root@wuliyong ~]# echo 'admin:123456' > /etc/rsync.pass
[root@wuliyong ~]# cat /etc/rsync.pass
admin:123456
- 设置文件权限
[root@wuliyong ~]# chmod 600 /etc/rsync*
[root@wuliyong ~]# ll /etc/rsync*
-rw-------. 1 root root 841 2月 19 22:53 /etc/rsyncd.conf
-rw-------. 1 root root 13 2月 19 22:53 /etc/rsync.pass
- 启动rsync服务并且设置开机自启动
[root@wuliyong ~]# systemctl start rsyncd
[root@wuliyong ~]# systemctl enable rsyncd
ln -s '/usr/lib/systemd/system/rsyncd.service' '/etc/systemd/system/multi-user.target.wants/rsyncd.service'
[root@wuliyong ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 50 *:445 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 50 *:139 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 50 :::445 :::*
LISTEN 0 5 :::873 :::*
LISTEN 0 50 :::139 :::*
LISTEN 0 128 :::22 :::*
在源服务器上做以下操作:
- 关闭防火墙与selinux
[root@yige ~]# systemctl stop firewalld.service
[root@yige ~]# setenforce 0
[root@yige ~]# getenforce
Permissive
[root@yige ~]# setenforce 0
[root@yige ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
- 安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@yige ~]# yum - y install rsync
- 创建认证密码文件:
[root@yige ~]# echo '123456' > /etc/rsync.pass
[root@yige ~]# cat /etc/rsync.pass
123456
- 设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@yige ~]# chmod 600 /etc/rsync.pass
[root@yige ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 2月 19 22:59 /etc/rsync.pass
- 在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@yige ~]# mkdir -pv /root/etc/test
mkdir: 已创建目录 "/root/etc"
mkdir: 已创建目录 "/root/etc/test"
[root@yige ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.26.129::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 49 bytes received 15 bytes 128.00 bytes/sec
total size is 0 speedup is 0.00
- 运行完成后,在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
[root@wuliyong ~]# ll /wuzhiyong
drwxr-xr-x. 2 root root 6 2月 19 04:25 test
- 安装inotify-tools工具,实时触发rsync进行同步
我们这边已经提起将inotify-tools下到root里面
[root@yige ~]# ls
anaconda-ks.cfg etc inotify-tools-3.14-8.el7.x86_64.rpm
apr-1.6.3.tar.bz2 httpd-2.4.34
apr-util-1.6.1.tar.bz2 httpd-2.4.34.tar.bz2
- 查看服务器内核是否支持inotify
[root@yige ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r--. 1 root root 0 2月 19 23:49 max_queued_events
-rw-r--r--. 1 root root 0 2月 19 23:49 max_user_instances
-rw-r--r--. 1 root root 0 2月 19 23:49 max_user_watches
- 安装inotify-tools
[root@localhost ~]# rpm -ivh inotify-tools-3.14-8.el7.x86_64.rpm
- 写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下
- 文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@yige ~]# mkdir /scripts
[root@yige ~]# touch /scripts/inotify.sh
[root@yige ~]# chmod 755 /scripts/inotify.sh
[root@yige ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 2月 19 23:56 /scripts/inotify.sh
[root@yige tmp]# vim /scripts/inotify.sh
host=192.168.26.129
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@yige ~]# nohup bash /scripts/inotify.sh &
[1] 2840
[root@yige ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@yige ~]# ps -ef|grep inotify
root 2840 2595 0 00:01 pts/0 00:00:00 bash /scripts/inotify.sh
root 2841 2840 0 00:01 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 2843 2595 0 00:01 pts/0 00:00:00 grep --color=auto inotify
- 在源服务器上生成一个文件或者目录,然后查看inotify生成的日志
[root@yige ~]# echo "hello world">>/etc/httpd24/test
[root@yige ~]# tail /tmp/rsync.log
20190219 16:12 /etc/httpd24/testCREATE was rsynced
[root@yige ~]# cat /etc/httpd24/test
xiaxia
hello world
- 在目标服务器上进行验证
[root@wuliyong ~]# ls /wuzhiyong
etc test
[root@wuliyong ~]# cat /wuzhiyong/etc/httpd24/test
hello world
设置脚本为开机自动启动
[root@yige ~]# chmod +x /etc/rc.d/rc.local
[root@yige ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 477 4月 2 2014 /etc/rc.d/rc.local
[root@yige ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@yige ~]# tail /etc/rc.d/rc.local
# to run scripts during boot instead of using this file.
#
# In constrast 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