当前位置:网站首页>Rsync远程同步
Rsync远程同步
2022-07-03 16:58:00 【星哥玩云】
rsync简介:
rsync是一款优秀的、快速的、多平台的本地或远程数据镜像同步备份工具。适用于Unix/Linux/Windows等多种平台。
在同步备份时,默认情况下,rsync通过其独特的quick check算法,仅同步大小或者最后修改时间发生变化的文件或目录(也可根据权限,属主等变化同步,需指定参数)甚至是只同步一个文件里有变化的内容部分,所以,可以实现快速的同步数据的功能。
rsync和cp,scp的区别:
cp,scp工具拷贝每次均为完整拷贝,而rsync除了完整备份,还具备增量拷贝的功能,因此,从性能及效率上更胜一筹。
rsync工作方式: rsync大致使用三种主要的方式来传输数据: 1.本地数据传输 2.通过rcp,ssh等通道传输 3.以守护进程的方式传输数据
本地数据传输 语法: rsync [OPTION] SRC... [DEST] SRC:源文件 DEST:目标文件 option参数说明: #一般使用-avz就可以 -a:归档模式,递归并保留对象属性,等同于 -rlptgoD -r:递归模式,包含目录及子目录中所有文件 -l:对于符号链接文件仍然复制为符号链接文件 -p:保留文件的权限标记 -t:保留文件的时间标记 -g:保留文件的属组标记(仅超级用户使用) -o:保留文件的属主标记(仅超级用户使用) -D:保留设备文件及其他特殊文件 -v:显示同步过程的详细(verbose)信息 -z:在传输文件时进行压缩(compress) -H:保留硬连接文件 -A:保留ACL属性信息 --delete:删除目标位置有而原始位置没有的文件 --checksum:根据对象的校验和来决定是否跳过文件
演示:将/etc 目录下的文件拷贝到/tmp 目录下 [[email protected] ~]# rsync -avz -P /etc /tmp/ #第一次拷贝的时候时间比较长,输出信息也比较多 第二次拷贝非常快,因为/etc目录下没有数据更新 [[email protected] ~]# rsync -avz -P /etc /tmp/ building file list ... 2141 files to consider
sent 61983 bytes received 20 bytes 124006.00 bytes/sec total size is 88435897 speedup is 1426.32 注意:rsync -avz -P /etc/ /tmp/ 仅同步etc目录里的内容,etc本身不同步 rsync -avz -P /etc /tmp/ 把etc和etc里面的内容全部考到tmp目录下 通过远程shell进行数据传输: 语法: 拉取:rsync [option] [[email protected]]HOST:SRC... [DEST] 推送:rsync [option] SRC... [[email protected]]HOST:DEST [[email protected]]HOST:为Rsync同步的远程的连接用户和主机地址 SRC:为源,即拷贝的分区、文件或目录等,和HOST之间用一个冒号连接; [DEST]为目的分区、文件或目录等
演示:要事先做好ssh通道 将本地的etc目录推送到tmp目录下,由于有ssh通道,所以没有要密码 [[email protected] ~]# rsync -avz -P /etc -e 'ssh -p 22' [email protected]:/tmp/
将远端的etc目录备份到本地的tmp目录下 [[email protected] ~]# rsync -avz -P -e 'ssh -p 22' [email protected]:/etc /tmp/
守护进程的方式: [[email protected] ~]# vim /etc/rsyncd.conf uid = root gid = root use chroot = no #锁定在家目录 max connections = 200 #最大连接数 timeout = 300 #超时时间 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [www] #模块,读取数据的时候需要用这个名 path= /var/www/html/ #远端备份的路径,抓取也是从这里抓,如果用户上传uid和gid用户需要用上传权限,如果度也要有读取权限 ignore errors #忽略错误 read only = false #只读为假就是可以上传数据,如果是true那么就不能上传数据了 list = false hosts allow = 192.168.0.0/24 hosts deny = 0.0.0.0/32 auth users = rbackup #认证的用户 secrets file = /etc/rsync.password #存放用户和密码文件
创建存放认证用户的文件 [[email protected] ~]# vim /etc/rsync.password rbackup:RedHat [[email protected] ~]# chmod 600 /etc/rsync.password #权限必须是600 [[email protected] ~]# ls /etc/rsync.password -l -rw------- 1 root root 15 12-10 10:18 /etc/rsync.password
启动服务 [[email protected] ~]# rsync --daemon #--daemon表示以守护进程的方式启动 [[email protected] ~]# ps -ef | grep rsync | grep -v grep root 7915 1 0 10:20 ? 00:00:00 rsync --daemon [[email protected] ~]# netstat -lnt | grep 873 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN tcp 0 0 :::873 :::* LISTEN
设置开机自动启动 [[email protected] ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.d/rc.local [[email protected] ~]# tail -1 /etc/rc.d/rc.local /usr/bin/rsync --daemon
客户端部署 [[email protected] ~]# echo "redhat" > /etc/rsync.password #仅需密码无须帐号 [[email protected] ~]# chmod 600 /etc/rsync.password #文件权限600 [r[email protected] ~]# cat /etc/rsync.password redhat
使用守护进程的方式数据传输 拉取:rsync[OPTION][[email protected]]HOST::SRC [DEST] rsync[OPTION] rsync://[[email protected]]HOST[:PORT]/SRC... [DEST] 推送:rsync[OPTION] SRC... [[email protected]]HOST::DEST rsync[OPTION] SRC... rsync://[[email protected]]HOST[:PORT]/DEST 客户端创建一个文件推送到服务器端 [[email protected] html]# touch a.txt [[email protected] html]# rsync -avz -P /var/www/html/ [email protected]::www/ --password-file=/etc/rsync.password 或者 [[email protected] html]# rsync -avz -P /var/www/html/ rsync://[email protected]:/www --password-file=/etc/rsync.password building file list ... 2 files to consider ./ a.txt 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 100 bytes received 44 bytes 96.00 bytes/sec total size is 0 speedup is 0.00
服务器端查看推送成功 [[email protected] html]# ls a.txt index.html index.php
--deleted参数:保证本地数据和远端数据完全一致,本地有啥远端就有啥,如果本地没有远端就删除 客户端执行 [[email protected] ~]# mkdir aaa创建一个空目录 [[email protected] ~]# rsync -avz -P --delete /root/aaa/ [email protected]::www/ --password-file=/etc/rsync.password building file list ... 1 file to consider deleting index.php deleting index.html deleting a.txt ./
sent 50 bytes received 22 bytes 13.09 bytes/sec total size is 0 speedup is 0.00 服务端查看/var/www/html目录下面什么都没有了 [[email protected] ~]# ll /var/www/html/ 总计 0
服务器端多个目录,配置文件写法 [[email protected] ~]# vim /etc/rsyncd.conf uid = root gid = root use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 192.168.0.0/24 hosts deny = 0.0.0.0/32 auth users = rbackup secrets file = /etc/rsync.password [www] path = /www [bbs] path = /bbs [blog] path = /blog
更改完成重启服务 [[email protected] ~]# pkill rsync && rsync --daemon
rsync+inotify实现触发式自动同步,inotify端创建或者一个文件rsync备份源也自动创建一个一模一样的文件,inotify删除一个文件rsync备份源也自动删除文件。
注意:inotify是部署在rsync服务成功的情况下
演示: 查看当前系统是否支持inotify [[email protected] ~]# ls -l /proc/sys/fs/inotify/ 总计 0 -rw-r--r-- 1 root root 0 12-11 10:36 max_queued_events #监控事件队列 -rw-r--r-- 1 root root 0 12-11 10:36 max_user_instances #最多监控实例数 -rw-r--r-- 1 root root 0 12-11 10:36 max_user_watches #每个实例最多监控文件数
编译安装inotify tar zxf inotify-tools-3.14.tar.gz cd inotify-tools-3.14 ./configure --prefix=/usr/local/inotify-tools-3.14 make && make install
脚本
host01=10.0.0.191 #定义IP src=/data0/www/www/ #定义源目录 dst=www #目标目录 user=rsync_backup #备份的用户 rsync_passfile=/etc/rsync.password #密码文件 inotify_home=/usr/local/inotify-tools-3.14/ #inotify的目录 #judge if [ ! -e "$src" ] \ || [ ! -e "${rsync_passfile}" ] \ || [ ! -e "${inotify_home}/bin/inotifywait" ] \ || [ ! -e "/usr/bin/rsync" ]; then echo "Check File and Folder" #判断上面的文件是否存在如果不存在就报错并且退出执行 exit 9 fi ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \ | while read file #监控的事件delete,create,attrib监控到之后通过read读取 do cd $src && rsync -aruz -R --delete ./ --timeout=100 [email protected]$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1 done exit 0
把脚本放到后台执行 [[email protected] ~]# sh inotify.sh & [1] 8216 [[email protected] ~]# ps -ef | grep inotify root 8216 7503 0 11:15 pts/2 00:00:00 sh inotify.sh root 8217 8216 0 11:15 pts/2 00:00:00 /usr/local/inotify-tools-3.14//bin/inotifywait -mrq --timefmt %d/%m/%y %H:%M --format %T %w%f -e close_write,delete,create,attrib /var/www/html root 8218 8216 0 11:15 pts/2 00:00:00 sh inotify.sh root 8220 7503 0 11:15 pts/2 00:00:00 grep inotify
这时创建几个文件 [[email protected] ~]# cd /var/www/html/ [[email protected] html]# touch a b c d 备份服务器查看就已经同步了 [[email protected] etc]# cd /var/www/html/ [[email protected] html]# ls a a.txt b c d
边栏推荐
- What is your income level in the country?
- C语言字符串反转
- 什么是质押池,如何进行质押呢?
- CC2530 common registers
- What is the maximum number of concurrent TCP connections for a server? 65535?
- [combinatorics] recursive equation (outline of recursive equation content | definition of recursive equation | example description of recursive equation | Fibonacci Series)
- [combinatorics] recursive equation (example 1 of recursive equation | list recursive equation)
- Mysql database -dql
- LeetCode 1656. Design ordered flow
- Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
猜你喜欢
Analysis of variance summary
Talk about several methods of interface optimization
什么是质押池,如何进行质押呢?
Network security web penetration technology
C language modifies files by line
聊聊接口优化的几个方法
Atom QT 16_ audiorecorder
CC2530 common registers for port initialization
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
随机推荐
CC2530 common registers for crystal oscillator settings
Overview of satellite navigation system
What is the maximum number of concurrent TCP connections for a server? 65535?
C language string inversion
Mysql database DDL and DML
【剑指 Offer 】64. 求1+2+…+n
聊聊接口优化的几个方法
[combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
Kindeditor editor upload image ultra wide automatic compression -php code
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
RF analyze demo build step by step
ucore概述
LeetCode 1658. Minimum operand to reduce x to 0
[sword finger offer] 58 - I. flip the word order
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
Netease UI automation test exploration: airtest+poco
Assembly instance analysis -- screen display in real mode
How to promote cross department project collaboration | community essay solicitation
How to delete a specific line from a text file using the SED command?
Idea configuration plug-in