rsync 简介
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync能够远程同步,支持本地复制,或者与其余SSH、rsync主机同步。linux
rsync特性
它的特性以下:c++
能够镜像保存整个目录树和文件系统。vim
能够很容易作到保持原来文件的权限、时间、软硬连接等等。centos
无须特殊权限便可安装。安全
快速:第一次同步时 rsync 会复制所有内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程当中能够实行压缩及解压缩操做,所以可使用更少的带宽。bash
安全:可使用scp、ssh等方式来传输文件,固然也能够经过直接的socket链接。服务器
支持匿名传输,以方便进行网站镜象。ssh
rsync命令
//Rsync的命令格式经常使用的有如下三种socket
rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [USER@]HOST:DEST rsync [OPTION]... [USER@]HOST:SRC DEST
//rsync经常使用选项ide
-a, --archive // 归档 -v, --verbose // 啰嗦模式 -q, --quiet // 静默模式 -r, --recursive // 递归 -p, --perms // 保持原有权限的属性 -z, --compress // 在传输时压缩,节省带宽,加快传输速度。 --delete // 在源服务器上作的删除操做也会在目标服务器上同步
INotify的介绍
inotify 是一种文件系统的变化通知机制,如文件增长、删除等事件能够马上让用户态得知。
1.Inotify 不须要对被监视的目标打开文件描述符,并且若是被监视目标在可移动介质上,那么在 umount 该介质上的文件系统后,被监视目标对应的 watch 将被自动删除,而且会产生一个 umount 事件。
2.Inotify 既能够监视文件,也能够监视目录。
3.Inotify 使用系统调用而非 SIGIO 来通知文件系统事件。
4.Inotify 使用文件描述符做为接口,于是可使用一般的文件 I/O 操做select 和 poll 来监视文件系统的变化。
环境说明
服务类型 | ip | 应用 | 操做系统 |
---|---|---|---|
源服务器 | 192.168.47.11 | rsync,inotify-tools 脚本 | centos7 /redhat7 |
目标服务器 | 192.168.7.12 | rsync | centos7 /redhat7 |
把源服务器上的/etc 目录实时同步到目标服务器 /tmp 下
在目标服务器上作如下操做
//关闭防火墙与SELINUX [root@yanyinglai3 ~]# systemctl stop firewalld [root@yanyinglai3 ~]# systemctl disable firewalld [root@yanyinglai3 ~]# getenforce Enforcing [root@yanyinglai3 ~]# setenforce 0 [root@yanyinglai3 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux 安装rsync服务端软件 [root@yanyinglai3 ~]# yum -y install rsync //设置rsyncd.conf 配置文件 root@yanyinglai3 ~]# cat >> /etc/rsyncd.conf <<EOF > log file = /var/log/rsyncd.log //日志文件位置,启动rsync后自动产生这个文件无需提早建立 > pidfile = /var/run/rsyncd.pid // pid文件的存放位置 > lock file = /var/run/rsync.lock // 支持max connections参数的锁文件 > secrets file = /etc/rsync.pass // 用户认证配置文件,里面保存用户名称和密码,必须手动建立文件 > [etc_from_client] // 自定义同步名称 > path = /tmp/ // rsync 服务端数据存放路径,客户端的数据将同步至此目录 > comment = sync etc from client > uid = root // 设置rsync 运行权限为root > gid = root // 设置rsync运行权限为root > port = 873 // 默认端口 > ignore errors // 表示出现错误忽略错误 > use chroot = no // 默认为true ,修改成no 增长对目录文件软链接的备份 > read only = no // 设置rsync 服务端为读写权限 > list = no // 不显示rsync 服务端资源列表 > max connections = 200 // 最大链接数 > timeout = 600 // 设置超时时间 > auth users = admin / / 执行数据同步的用户名,能够设置多个 > hosts allow = 192.168.47.11 // 容许进行数据同步的客户端ip地址,能够设置多个 > hosts deny = 192.168.1.1 // 禁止数据同步的客户端ip地址,能够设置多个 > EOF 建立用户认证文件 [root@yanyinglai3 ~]# echo 'admin:123456' > /etc/rsync.pass [root@yanyinglai3 ~]# cat /etc/rsync.pass admin:123456 设置文件权限 [root@yanyinglai3 ~]# chmod 600 /etc/rsync* [root@yanyinglai3 ~]# ll /etc/rsync* -rw-------. 1 root root 842 8月 16 10:26 /etc/rsyncd.conf -rw-------. 1 root root 13 8月 16 10:52 /etc/rsync.pass 启动rsync服务并设置开机自启动 [root@yanyinglai3 ~]# systemctl start rsyncd [root@yanyinglai3 ~]# systemctl enable rsyncd Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service. [root@yanyinglai3 ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 5 *:873 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 5 :::873 :::* 在源服务器上作如下操做 [root@yanyinglai ~]# systemctl stop firewalld [root@yanyinglai ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@yanyinglai ~]# getenforce Enforcing [root@yanyinglai ~]# setenforce 0 [root@yanyinglai ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux // 配置yum源 [root@yanyinglai ~]# cd /etc/yum.repos.d/ [root@yanyinglai yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo [root@yanyinglai ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo [root@yanyinglai ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo [root@yanyinglai ~]# yum -y install epel-release [root@yanyinglai ~]# yum -y update --skip-broken 安装rsync服务端软件,只须要安装,不要启动,不须要配置 [root@yanyinglai ~]# yum -y install rsync // 建立认证密码文件 [root@yanyinglai ~]# echo '123456' > /etc/rsync.pass [root@yanyinglai ~]# cat /etc/rsync.pass 123456 // 设置文件权限,只设置文件全部者具备读取,写入权限便可 [root@yanyinglai ~]# chmod 600 /etc/rsync.pass [root@yanyinglai ~]# ll /etc/rsync.pass -rw-------. 1 root root 7 8月 16 11:38 /etc/rsync.pass // 在源服务器上建立测试目录,而后在源服务器运行一下命令 [root@yanyinglai ~]# ls anaconda-ks.cfg [root@yanyinglai ~]# mkdir -pv /root/etc/test mkdir: 已建立目录 "/root/etc" mkdir: 已建立目录 "/root/etc/test" [root@yanyinglai ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.47.12::etc_from_client --password-file=/etc/rsync.pass // 运行完成后,在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功 安装inotify-tools [root@yanyinglai ~]# yum -y install make gcc gcc-c++ [root@yanyinglai ~]# yum -y install inotify-tools // 写同步脚本 [root@yanyinglai ~]# mkdir /scripts [root@yanyinglai ~]# touch /scripts/inotify.sh [root@yanyinglai ~]# chmod 755 /scripts/inotify.sh [root@yanyinglai ~]# ll /scripts/inotify.sh -rwxr-xr-x. 1 root root 0 8月 16 14:06 /scripts/inotify.sh [root@yanyinglai ~]# vim /scripts/inotify.sh host=192.168.47.12 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@yanyinglai ~]# nohup bash /scripts/inotify.sh & [1] 25034 [root@yanyinglai ~]# nohup: 忽略输入并把输出追加到"nohup.out" [root@yanyinglai ~]# ps -ef|grep inotify root 25034 9419 0 14:17 pts/1 00:00:00 bash /scripts/inotify.sh root 25035 25034 1 14:17 pts/1 00:00:01 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc root 25036 25034 0 14:17 pts/1 00:00:00 bash /scripts/inotify.sh root 25038 9419 0 14:18 pts/1 00:00:00 grep --color=auto inotify 在源服务器上生成一个新文件 [root@yanyinglai ~]# mkdir /etc/httpd24/ [root@yanyinglai ~]# echo 'hello world' > /etc/httpd24/test 查看inotify生成的日志 [root@yanyinglai ~]# tail /tmp/rsync.log 20180816 14:18 /etc/httpd24CREATE,ISDIR was rsynced 20180816 14:19 /etc/httpd24/testCREATE was rsynced 20180816 14:19 /etc/httpd24/testMODIFY was rsynced 设置脚本开机自动启动 [root@yanyinglai ~]# chmod +x /etc/rc.d/rc.local [root@yanyinglai ~]# ll /etc/rc.d/rc.local -rwxr-xr-x. 1 root root 473 4月 11 15:36 /etc/rc.d/rc.local [root@yanyinglai ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local [root@yanyinglai ~]# tail /etc/rc.d/rc.local # to run scripts during boot instead of using this file. # # 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@yanyinglai3 ~]# cd /tmp [root@yanyinglai3 tmp]# pwd /tmp [root@yanyinglai3 tmp]# ls etc test