- rsync
- rsync+inotify
- 实例演示
rsync简介
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
//Rsync的命令格式常用的有以下三种:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:
[root@localhost ~]# ls
anaconda-ks.cfg nohup.out
[root@localhost ~]# rsync -a nohup.out a.sh
[root@localhost ~]# ll
总用量 12
-rw-------. 1 root root 1284 9月 24 22:14 anaconda-ks.cfg
-rw-------. 1 root root 471 10月 11 21:27 a.sh
-rw-------. 1 root root 471 10月 11 21:27 nohup.out
[root@localhost ~]# ll -i
总用量 12
67182984 -rw-------. 1 root root 1284 9月 24 22:14 anaconda-ks.cfg
67145884 -rw-------. 1 root root 471 10月 11 21:27 a.sh
67145882 -rw-------. 1 root root 471 10月 11 21:27 nohup.out
2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包
含单个冒号":"分隔符时启动该模式。如:
[root@localhost ~]# rsync -avz a.sh root@192.168.129.135:/root/b.sh
sending incremental file list
a.sh
sent 270 bytes received 35 bytes 55.45 bytes/sec
total size is 471 speedup is 1.54
[root@localhost ~]# ssh root@192.168.129.135 'ls -l /root'
total 8
-rw-------. 1 root root 1454 Aug 6 04:39 anaconda-ks.cfg
-rwxr-xr-x 1 root root 1041 Aug 8 2018 b.sh
3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径
包含单个冒号":"分隔符时启动该模式。如:
[root@localhost ~]# ls /hhr/runtime/
jj jjyy yy
[root@localhost ~]# rsync -avz root@192.168.129.135:/hhr/runtime/ . #/hhr/runtime/ 只同步/runtime/目录以下的文件(不包含自身目录);而/hhr/runtime 同步/runtime目录以及文件或文件(包含自身目录)
receiving incremental file list
./
jj
jjyy
yy
sent 84 bytes received 211 bytes 118.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls
anaconda-ks.cfg a.sh jj jjyy nohup.out yy
//rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
rsync+inotify
| 名称 | 含义 |
|---|---|
| rsync | rsync与传统的cp、tar备份方式相比, rsync具有安全性高、备份迅速、支持增量备份等优点, 通过rsync可以解决对实时性要求不高的数据备份需求, 例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。 |
| Inotify | Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制, linux内核从2.6.13起,加入了Inotify支持, 通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件, 利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况, 而inotify-tools就是这样的一个第三方软件。 |
环境说明:
| 服务器类型 | IP地址 | 应用 | 操作系统 |
|---|---|---|---|
| 源服务器(source) | 192.168.129.134 | rsync inotify-tools 脚本 | Redhat 8 |
| 目标服务器(target) | 192.168.129.135 | rsync | Redhat 8 |
需求
部署rsync+inotify同步/runtime目录至目标服务器的/NAME/下。这里的NAME是指你的名字,比如你叫tom,则要把/runtime目录同步至目标服务器的/tom/下。
目标服务器
//关闭防火墙与SELINUX [root@target ~]# systemctl disable --now firewalld.service Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@target ~]# sed -ri 's/^(SELINUX=).*/1disabled/g' /etc/selinux/config [root@target ~]# setenforce 0 //安装rsync服务端软件 [root@target ~]# yum -y install rsync rsync-daemon //设置rsyncd.conf配置文件 [root@target ~]# cat >> /etc/rsyncd.conf </etc/rsync.pass [root@target ~]# cat /etc/rsync.pass admin:123456 //设置文件权限 [root@target ~]# chmod 600 /etc/rsync* [root@target ~]# ll /etc/rsync* -rw-------. 1 root root 381 10月 11 20:35 /etc/rsyncd.conf -rw-------. 1 root root 13 10月 11 20:38 /etc/rsync.pass //配置rsync服务并设置开机自启动 [root@target ~]# vim /usr/lib/systemd/system/rsyncd.service [root@target ~]# cat /usr/lib/systemd/system/rsyncd.service [Unit] Description=fast remote file copy program daemon [Service] User=root Group=root EnvironmentFile=/etc/sysconfig/rsyncd ExecStart=/usr/bin/rsync --daemon --config=/etc/rsyncd.conf --no-detach ExecReload=/bin/kill -HUP KillMode=process Restart=on-failure RestartSec=30s [Install] WantedBy=multi-user.target //启动rsync服务并设置开机自启动 [root@target ~]# systemctl enable --now rsyncd Job for rsyncd.service failed because of unavailable resources or another system error. See "systemctl status rsyncd.service" and "journalctl -xe" for details.
报错解决方法 安装rsync-daemon
[root@target ~]# yum -y install rsync-daemon [root@target ~]# systemctl enable --now rsyncd Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service. [root@target ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port 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 [::]:*
源服务器
//关闭防火墙与SELINUX [root@source ~]# systemctl disable --now firewalld.service Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@source ~]# sed -ri 's/^(SELINUX=).*/1disabled/g' /etc/selinux/config [root@source ~]# setenforce 0 //配置yum源 [root@source ~]# curl -o /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-8.repo [root@source ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-base.repo //安装epel源 [root@source ~]# yum -y install epel-release vim //安装rsync服务端软件,只需要安装,不要启动,不需要配置 [root@source ~]# yum -y install rsync //创建认证密码文件 [root@source ~]# echo '123456' > /etc/rsync.pass [root@source ~]# cat /etc/rsync.pass 123456 //设置文件权限,只设置文件所有者具有读取、写入权限即可 [root@source ~]# chmod 600 /etc/rsync.pass [root@source ~]# ll /etc/rsync.pass -rw-------. 1 root root 7 10月 11 21:13 /etc/rsync.pass
测试
//在目标服务器上创建目录
[root@target ~]# mkdir /hhr
//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@source ~]# mkdir /runtime
[root@source ~]# cd /runtime/
[root@source runtime]# touch jj yy
[root@source runtime]# rsync -avH --port 873 --progress --delete /runtime/ admin@192.168.129.135::runtime --password-file=/etc/rsync.pass
sending incremental file list
./
jj
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
yy
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 163 bytes received 65 bytes 456.00 bytes/sec
total size is 0 speedup is 0.00
//在目标服务器上查看测试结果
[root@target ~]# cd /hhr/
[root@target hhr]# ls
jj yy
实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@source ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r--. 1 root root 0 10月 11 21:17 max_queued_events
-rw-r--r--. 1 root root 0 10月 11 21:17 max_user_instances
-rw-r--r--. 1 root root 0 10月 11 21:17 max_user_watches
//源服务器安装inotify-tools
[root@source ~]# yum -y install make gcc gcc-c++ inotify-tools
//在源服务器编写同步脚本
[root@source ~]# vim /scripts/inotify.sh
[root@source ~]# cat /scripts/inotify.sh
host=192.168.129.135 # 目标服务器的ip(备份服务器)
src=/runtime # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=runtime # 自定义的模块名,需要与目标服务器上定义的同步名称一致
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@source ~]# nohup bash /scripts/inotify.sh &
[1] 109229
[root@source ~]# nohup: 忽略输入并把输出追加到'nohup.out'
[root@source ~]# ps -ef|grep inotify
root 109229 4210 0 21:26 pts/0 00:00:00 bash /scripts/inotify.sh
root 109235 109229 0 21:26 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 /runtime
root 109236 109229 0 21:26 pts/0 00:00:00 bash /scripts/inotify.sh
root 110302 4210 0 21:26 pts/0 00:00:00 grep --color=auto inotify
//在源服务器上生成一个新文件
[root@source ~]# ls /runtime/
jj yy
[root@source ~]# touch /runtime/jjyy
//查看inotify生成的日志
[root@source ~]# tail /tmp/rsync.log
20211011 21:27 /runtime/jjyyCREATE was rsynced
20211011 21:27 /runtime/jjyyATTRIB was rsynced
//设置监控runtime目录脚本开机自启
[root@source ~]# chmod +x /etc/rc.d/rc.local
[root@source ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 474 3月 24 2020 /etc/rc.d/rc.local
[root@source ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@source ~]# tail /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
//在目标服务器查看脚本是否成功触发并同步了文件
[root@target hhr]# ls /hhr/
jj runtime yy
[root@target hhr]# ls /hhr/runtime/
jj jjyy yy



