rsync是一个开源的快速备份工具,能够在不一样主机之间镜像同步整个目录树,支持增量备份,保持连接和权限,很是适用于异地备份html
在远程同步过程当中,负责发起rsync同步操做的客户机称为++发起端++,而负责响应来及客户机的rsync同步操做的服务器为++同步源++。在同步过程当中,同步源负责提供文档的原始位置,而发起端对该位置具备读写权限。nginx
备份的基本格式:“rsync [选项] 原始位置 目标位置”
-v, --verbose 详细模式输出 -q, --quiet 精简输出模式 -c, --checksum 打开校验开关,强制对文件传输进行校验 -a, --archive 归档模式,表示以递归方式传输文件,并保持全部文件属性,等于-rlptgoD -r, --recursive 对子目录以递归模式处理 -R, --relative 使用相对路径信息 -b, --backup 建立备份,也就是对于目的已经存在有一样的文件名时,将老的文件从新命名为~filename。可使用--suffix选项来指定不一样的备份文件前缀 --backup-dir 将备份文件(如~filename)存放在在目录下 -suffix=SUFFIX 定义备份文件前缀 -u, --update 仅仅进行更新,也就是跳过全部已经存在于DST,而且文件时间晚于要备份的文件,不覆盖更新的文件 -l, --links 保留软链结 -L, --copy-links 想对待常规文件同样处理软链结 --copy-unsafe-links 仅仅拷贝指向SRC路径目录树之外的链结 --safe-links 忽略指向SRC路径目录树之外的连接 -H, --hard-links 保留硬链结 -p, --perms 保持文件权限 -o, --owner 保持文件属主信息 -g, --group 保持文件属组信息 -D, --devices 保持设备文件信息 -t, --times 保持文件时间信息 -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间 -n, --dry-run现实哪些文件将被传输 -w, --whole-file 拷贝文件,不进行增量检测 -x, --one-file-system 不要跨越文件系统边界 -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节 -e, --rsh=command 指定使用rsh、ssh方式进行数据同步 --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息 -C, --cvs-exclude 使用和CVS同样的方法自动忽略文件,用来排除那些不但愿传输的文件 --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新建立的文件 --delete 删除那些DST中SRC没有的文件 --delete-excluded 一样删除接收端那些被该选项指定排除的文件 --delete-after 传输结束之后再删除 --ignore-errors 及时出现IO错误也进行删除 --max-delete=NUM 最多删除NUM个文件 --partial 保留那些因故没有彻底传输的文件,以是加快随后的再次传输 --force 强制删除目录,即便不为空 --numeric-ids 不将数字的用户和组id匹配为用户名和组名 --timeout=time ip超时时间,单位为秒 -I, --ignore-times 不跳过那些有一样的时间和长度的文件 --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间 --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0 -T --temp-dir=DIR 在DIR中建立临时文件 --compare-dest=DIR 一样比较DIR中的文件来决定是否须要备份 -P 等同于 --partial --progress 显示备份过程 -z, --compress 对备份的文件在传输时进行压缩处理 --exclude=PATTERN 指定排除不须要传输的文件模式 --include=PATTERN 指定不排除而须要传输的文件模式 --exclude-from=FILE 排除FILE中指定模式的文件 --include-from=FILE 不排除FILE指定模式匹配的文件 --version 打印版本信息 --address 绑定到特定的地址 --config=FILE 指定其余的配置文件,不使用默认的rsyncd.conf文件 --port=PORT 指定其余的rsync服务端口 --blocking-io 对远程shell使用阻塞IO -stats 给出某些文件的传输状态 --progress 在传输时现实传输过程 --log-format=formAT 指定日志文件格式 --password-file=FILE 从FILE中获得密码 --bwlimit=KBPS 限制I/O带宽,KBytes per second -h, --help 显示帮助信息
准备条件:centos 7.4版本 同步源端ip:192.168.100.129 发起端ip: 192.168.100.130 [root@localhost ~]# rpm -q rsync //系统默认已安装 rsync-3.0.9-18.el7.x86_64
[root@localhost ~]# vim /etc/rsyncd.conf uid = nobody gid = nobody use chroot = yes //禁锢在源目录 address = 192.168.100.129 //监听本地端口 port 873 //监听端口 log file = /var/log/rsyncd.log //日志文件位置 pid file = /var/run/rsyncd.pid //进程id存放位置 hosts allow = 192.168.100.0/24 //容许访问的地址段 [wwwroot] //共享模块名称 path = /var/www/html //源目录路径 comment = www.kgc.cn //描述信息 read only = yes //只读,同步时需改成“no” dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 auth users = backuper //受权帐户 secrets file = /etc/rsyncd_users.db //存放帐户信息的数据文件
[root@localhost ~]# vim /etc/rsyncd_users.db backuper:abc123 //添加密码,这样发起端发起同步须要此密码认证 [root@localhost ~]# chmod 600 /etc/rsyncd_users.db //赋予读写权限
[root@localhost ~]# yum install httpd -y [root@localhost ~]# ls -ld /var/www/html/ drwxr-xr-x. 2 root root 6 6月 27 21:49 /var/www/html/ [root@localhost ~]# cd /var/www/ [root@localhost www]# ll 总用量 0 drwxr-xr-x. 2 root root 6 6月 27 21:49 cgi-bin drwxr-xr-x. 2 root root 6 6月 27 21:49 html [root@localhost www]# chmod 777 html //赋予最大权限,不然会同步失败 [root@localhost www]# ll 总用量 0 drwxr-xr-x. 2 root root 6 6月 27 21:49 cgi-bin drwxrwxrwx. 2 root root 6 6月 27 21:49 html
[root@localhost www]# rsync --daemon //启动rsync服务 [root@localhost www]# netstat -antp |grep rsync tcp 0 0 192.168.100.129:++873++ 0.0.0.0:* LISTEN 4254/rsync //默认端口:873 重启操做:先kill进程,再到/var/run/rsyncd.pid中删除pid文件,再启动服务完成重启,不然会失败 [root@localhost html]# systemctl stop firewalld.service [root@localhost html]# setenforce 0 //关闭防火墙和网络加强×××
[root@localhost www]# cd html/ [root@localhost html]# touch 111.txt [root@localhost html]# rsync -avz backuper@192.168.100.129::wwwroot /opt/ Password: receiving incremental file list ./ 111.txt [root@localhost html]# cd /opt [root@localhost opt]# ls 111.txt //显示备份成功
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384 //监控事件队列 fs.inotify.max_user_instances = 1024 //最多监控实例数 fs.inotify.max_user_watches = 1048576 //最多监控文件数
[root@localhost html]# systemctl stop firewalld.service [root@localhost html]# setenforce 0 //关闭防火墙和网络加强×××
[root@localhost abc]# mount.cifs //192.168.100.1/mem /abc [root@localhost abc]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ [root@localhost opt]# cd /opt/inotify-tools-3.14/ [root@localhost inotify-tools-3.14]# ./configure [root@localhost inotify-tools-3.14]# make && make install
[root@localhost ~]# yum install httpd -y [root@localhost ~]# ls -ld /var/www/html/ drwxr-xr-x. 2 root root 6 6月 27 21:49 /var/www/html/ [root@localhost www]# chmod 777 html //赋予最大权限,不然会同步失败 [root@localhost www]# ll 总用量 0 drwxr-xr-x. 2 root root 6 6月 27 21:49 cgi-bin drwxrwxrwx. 2 root root 6 6月 27 21:49 html
vim /etc/server.pass abc123 //和源端的验证密码需一致 [root@localhost ~]# chmod 600 /etc/server.pass
[root@localhost html]# vim /etc/rsyncd.conf read only =yes //这里将yes改成no,赋予写的权限 [root@localhost html]# netstat -antp |grep rsync tcp 0 0 192.168.100.129:873 0.0.0.0:* LISTEN 4254/rsync [root@localhost html]# kill -9 4254 [root@localhost run]# cd /var/run [root@localhost run]# rm -rf rsyncd.pid [root@localhost run]# rsync --daemon [root@localhost run]# netstat -antp |grep rsync tcp 0 0 192.168.100.129:873 0.0.0.0:* 注:修改配置文件后,源端需重启:先kill进程,再到/var/run/rsyncd.pid中删除pid文件
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ (-e:指定监控事件;-m:持续监控;-r;递归整个目录;-q:简化输出信息
)算法
[root@localhost html]# vim /opt/inotify.sh #!/bin/bash INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/" RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.100.129::wwwroot/" //同步源的ip $INOTIFY_CMD | while read DIRECTORY EVENT FILE do if [ $(pgrep rsync | wc -l) -le 0 ] ; then $RSYNC_CMD fi done [root@localhost html]#chmod +x /opt/inotify.sh //赋予执行权限
发起端:
[root@localhost html]# touch 1.txt abc.txt [root@localhost html]# ls 1.txt abc.txt [root@localhost html]# rm -rf 1.txt 源端查看: [root@localhost html]# ls 1.txt abc.txt [root@localhost html]# ls abc.txt //同步成功