Products
GG网络技术分享 2025-03-18 16:14 0
大家好,这里是关于[centos系统安装Rsync进行数据增量同步,宝塔linux环境],[Linux安装rsync和inotify实现文件夹实时同步]问题的解答,希望对您有所帮助。如果你还想了解更多这方面的信息,请关注本站其他相关内容,共同学习吧!
2020年9月3日教程大全rsync,Rsync同步参数,Rsync增量同步,宝塔面板
centos系统安装Rsync进行数据增量同步,宝塔linux环境
手把手教你在两台宝塔linux面板的服务器进行定时数据增量同步。
假设我有一台服务器A(服务端),一台服务器B(客户端),我要把A服务器的某个目录内的数据增量同步到服务器B。就可以使用Rsync来进行同步。
教程在两台服务器上都安装Rsync,A服务器发送,B服务器接收。Rsync检测到文件新增或者文件修改都自动执行同步,每次只同步更改的文件,网络资源占用小。
A和B两台服务器都放行873端口,Rsync会使用该端口进行通信。
1.在A服务器安装Rsync
yum -y install rsync
安装速度很快。
2.修改Rsync配置文件
安装完成后我们进入/etc/rsyncd.conf文件修改配置文件。
默认显示的是#开头的说明,我们在下面直接加上配置就行。
[cheshirex这个名字可以随便写]
path=/www/wwwroot/cdn.cheshirex.com/这个是A服务器要同步文件的目录
use chroot=no
max connections=10
read only=yes
write only=no
list=no
uid=root
gid=root
auth users=root这个是密码文件里的用户名,教程默认root
secrets file=/etc/rsync_server.pas这个是密码文件,下一步我们会创建
strict modes=yes
hosts allow=允许连接的服务器IP,这里填服务器B的IP地址,也可以填*允许所有IP连接
#hosts deny=1.1.1.1
ignore errors=yes
timeout=120 #秒按照我上面的配置文件,自己修改下,直接填入rsyncd.conf。如下图:
3.手动创建密码文件
在/etc目录创建文件rsync_server.pas文件里填用户名:密码,如下图:
4.修改rsync_server.pas文件权限
将权限改为600,用户组root
5.启动A服务器的Rsync服务
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
A服务器上我们就配置完成了。
1.同A一样,先安装Rsync,这里就不写安装步骤了
2.在/etc目录创建密码文件rsync_client.pas内容里只填密码!如下图:
3.修改rsync_client.pas文件权限和用户组。
权限600,用户组root。跟A服务器的一样。
4.在B服务器宝塔面板上添加计划任务。
rsync -avztopg --delete rsync://root@这里填A服务器IP/cheshirex /www/wwwroot/cdn.cheshirex.com --password-file=/etc/rsync_client.pas
执行周期自己选择多久一次,根据实际需求自己选择。
首次添加定时任务后,因为文件较多。可以手动点一下执行同步。不影响以后的定时同步。
同步结果都可以查看任务日志。
任务参数说明:
-avztopg和–delete
这两个都是同步参数,文末会附上更多的参数说明。a、v、z、topg这是4个参数。
root@这个填A服务器IP/cheshirex
这里是服务器rsync链接用户、A服务器IP地址、A服务器配置文件名
/www/wwwroot/cdn.cheshirex.com
这是B服务器的目录,文件会同步到这个目录
rsync_client.pas
这是B服务器的密码配置文件
A服务器修改文件README.md名字为test.md,会自动删除B服务器的README.md,然后把A服务器的test.md同步过去。
如果提示错误:
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1659) [generator=3.1.3]
代表目录内有文件无法同步,可能是A服务器的文件无法读取,也可能是B服务器的文件无法修改。
比如宝塔网站目录默认创建的.user.ini这个文件就无法更改。不过不影响其他文件的同步。
附加内容(Rsync同步参数):
-q,--quiet:suppress non-error messages 静默模式
-v,--verbose:increase verbosity
-a,--archive:archive mode; equals -rlptgoD (no -H,-A,-X) 归档模式,相当于-rlptgoD,不包括(no -H,-A,-X);最常用的参数
-H,--hard-links:preserve hard links 保留硬链接
-A,--acls:preserve ACLs (implies --perms) 保留ACL权限
-X,--xattrs:preserve extended attributes 保留扩展属性
-c, --checksum:skip based on checksum, not mod-time & size
-r,--recursive:recurse into directories 递归
-l,--links:copy symlinks as symlinks 保留软链接,而不跟踪原文件
-p,--perms:preserve permissions 保留权限
-t,--times:preserve modification times 保留mtime
-g,--group:preserve group 保留属组
-o,--owner:preserve owner (super-user only) 保留属主
-D:same as --devices,--specials 保留设备文件和特殊文件
--devices:preserve device files (super-user only)
--specials:preserve special files
-z,--compress:compress file data during the transfer 输过程中压缩文件数据
-n, --dry-run:perform a trial run with no changes made 干跑测试
-u,--update:skip files that are newer on the receiver 增量同步,跳过比本地较新的文件
--delete:delete extraneous files from destination dirs 删除目标目录多余文件
--progress:show progress during transfer 显示传输进度以上参数,自己考虑是否使用。
2022年6月6日,增加一个限速参数:
--bwlimit=500
这里的值就是限速大小,500KB/s。直接在其他参数后面空格加上此限速参数即可。
示例:
rsync -avztopg --delete --bwlimit=500 rsync://root@*******
在web服务器中,作为代码发布机A,文件同步到服务器B,C,D等集群中,可以忽略某个文件和目录。
A服务器:内网IP: 192.168.1.2
B服务器:内网IP: 192.168.1.3
A和B的www用户,或者root用户免密登录。
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
A和B都做
yum -y install xinetd
yum -y install rsync
chkconfig rsync on
service xinetd restart
systemctl restart xinetd
A上操作:
rsync -av root@192.168.1.3:/rsynctest/1.txt /root
B上操作
rsync -av /rsynctest/2.txt root@192.168.1.2:/root
rsync -av -e "ssh -p 22" /rsynctest/2.txt root@192.168.1.2:/root 【如果ssh的开启的端口不是22 则用-e指定ssh端口】
只在A上操作即可。
安装inotify-tools
wget http://js.地址funet8地址.com/centos_software/inotify-tools-3.14.tar.gz
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
inotifywait -m /root 【查看inotify-tools是否运行正常】
新开一个终端:
[root@localhost ~]# cd /root
[root@localhost ~]# touch bb.txt
监控到
# inotifywait -m /root
Setting up watches.
Watches established.
/root/ OPEN .bash_profile
/root/ ACCESS .bash_profile
/root/ CLOSE_NOWRITE,CLOSE .bash_profile
/root/ OPEN .bashrc
/root/ ACCESS .bashrc
/root/ CLOSE_NOWRITE,CLOSE .bashrc
/root/ CREATE bb.txt
/root/ OPEN bb.txt
/root/ ATTRIB bb.txt
/root/ CLOSE_WRITE,CLOSE bb.txt
test.sh 为要运行网站实时同步脚本 其中定义了要同步的网站的路径,要同步到的ip地址,哪些后缀名的文件忽略监控,同步的用户名,同步的文件列表,哪些文件不需要同步。
cat test.sh
#!/bin/sh
SRC=/data/wwwroot/web/test/ #代码发布服务器目录
DST=/data/wwwroot/web/test/ #目标服务器目录
IP="192.168.1.3 192.168.1.4" # 这里可以用hostname,多个主机用空格
USER=www
inotifywait -mrq $SRC -e modify,delete,create,close_write,attrib | while read D E F
do
for i in $IP
do
#排除后缀名和目录
/usr/bin/rsync -e 'ssh -p 60920' \\
-ahqzt --exclude "*.swp" \\
--exclude "*.svn" \\
--exclude "test/" \\
--exclude "runtime/" \\
--delete $SRC $USER@$i:$DST
done
done
运行:
增加权限:
chmod +x test.sh
后台运行:
nohup ./test.sh > nohup_test 2>&1 &
生成一个文件才能触发文件同步
touch /data/wwwroot/web/test/test_rsync_`date +%Y%m%d-%H:%M:%S`.html
删除测试文件
rm -rf /data/wwwroot/web/test/test_rsync*.html
测试文件是否同步Demand feedback