通过rsync同步系统文件

1、服务配置安装:
[root@kylin-server01 /]# yum install rsync -y #安装rsync服务
[root@kylin-server01 /]# touch /mnt/test{1..5}.txt#创建测试文件
[root@kylin-server01 /]# ls mnt/#检查文件是否创建
test1.txt test2.txt test3.txt test4.txt test5.txt
2、本地文件同步:
[root@kylin-server01 /]# rsync -a /mnt/ /backup#手动同步
[root@kylin-server01 /]# cd backup/ [root@kylin-server01 backup]# ls#检查同步成功
test1.txt test2.txt test3.txt test4.txt test5.txt
删除再同步测试
[root@kylin-server01 backup]# rm -rf test1.txt #删除/backup/test1.txt [root@kylin-server01 backup]# ls#查看删除成功
test2.txt test3.txt test4.txt test5.txt
[root@kylin-server01 backup]# rsync -a /mnt/ /backup##再次手动同步mnt下的文件到backup [root@kylin-server01 backup]# ls#检查同步成功
test1.txt test2.txt test3.txt test4.txt test5.txt
删除源文件夹文件再次测试
[root@kylin-server01 backup]# rm -rf /mnt/test5.txt#删除mnt文件测试同步结果 [root@kylin-server01 backup]# ls#删除成功
test1.txt test2.txt test3.txt test4.txt
[root@kylin-server01 backup]# rsync -a -v --delete /mnt/ /bakup/#再次同步测试并显示同步过程
sending incremental file listcreated directory /bakup./test1.txttest2.txttest3.txttest4.txtsent 282 bytes received 124 bytes 812.00 bytes/sectotal size is 0 speedup is 0.00
配合定时任务本地自动同步
[root@kylin-server01 backup]# crontab -e #设置本地自动同步
*/1 * * * * rsync -a -v --delete /mnt/ /backup/ #每分钟同步一次
[root@kylin-server01 backup]# rm -rf /mnt/test4.txt #删除测试
[root@kylin-server01 backup]# ls /mnt/
test1.txt test2.txt test3.txt
[root@kylin-server01 backup]# ls /backup
test1.txt test2.txt test3.txt test4.txt test5.txt 75 ls /mnt
静待一分钟再次查看
[root@kylin-server01 backup]# ls /backup
test1.txt test2.txt test3.
3、远程同步:
两台服务器节点
server01(server端):192.168.235.166
server02(目的端):192.168.235.167
server端执行:
[root@kylin-server01 ~]# ssh-keygen -t rsa#三次回车生成密钥
Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'.Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:SHA256:+jEKEUnYO+OBT/HYZ/dGY9henvMTrAqpnAnb5dW/tn0 root@kylin-server01The key's randomart image is:+---[RSA 2048]----+|   o.           || ..o.           ||   .o*     o     || . B.o o o = . ||   +.+ oS. = +.. ||   o. . . = +o ||   ... o+ o ..o.||     .=o*oo ..oE||     ..*.. .. .+*|+----[SHA256]-----+
[root@kylin-server01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.235.167#发送密钥到目的主机
[root@kylin-server01 ~]# ssh root@192.168.235.167#远程链接测试(输入密码认证)
[root@kylin-server02 ~]# exit#退出
注销 Connection to 192.168.235.167 closed.
[root@kylin-server01 ~]# rsync -a -v -z -e ssh --delete /mnt/ root@192.168.235.167:/mnt/#远程同步
Authorized users only. All activities may be monitored and reported.sending incremental file listdeleting public/deleting kylinclient/./test1.txttest2.txttest3.txtsent 220 bytes received 103 bytes 646.00 bytes/sectotal size is 0 speedup is 0.00
登陆server2节点验证
[root@kylin-server01 ~]# ssh root@192.168.235.167#登陆服务器
Authorized users only. All activities may be monitored and reported.
Authorized users only. All activities may be monitored and reported. Web console: https://kylin-server02:9090/ or https://192.168.235.167:9090/
Last login: Mon Jun 7 00:26:17 2021 from 192.168.235.166 [root@kylin-server02 ~]# ls /mnt/#查看/mnt目录验证 test1.txt test2.txt test3.txt
测试成功!实验完成!
4、服务详解:
rsync的选项说明。
-v:显示rsync过程中详细信息。可以使用"-vvvv"获取更详细信息。
-P:显示文件传输的进度信息。(实际上"-P"="--partial --progress",其中的"--progress"才是显示进度信息的)。
-n --dry-run:仅测试传输,而不实际传输。常和"-vvvv"配合使用来查看rsync是如何工作的。
-a --archive:归档模式,表示递归传输并保持文件属性。等同于"-rtopgDl"。
-r --recursive:递归到目录中去。
-t --times:保持mtime属性。强烈建议任何时候都加上"-t",否则目标文件mtime会设置为系统时间,导致下次更新检查出mtime不同从而导致增量传输无效。
-o --owner:保持owner属性(属主)。
-g --group:保持group属性(属组)。
-p --perms:保持perms属性(权限,不包括特殊权限)。
-D :是"--device --specials"选项的组合,即也拷贝设备文件和特殊文件。
-l --links:如果文件是软链接文件,则拷贝软链接本身而非软链接所指向的对象。
-z:传输时进行压缩提高效率。
-R --relative:使用相对路径。意味着将命令行中指定的全路径而非路径最尾部的文件名发送给服务端,包括它们的属性。用法见下文示例。
--size-only:默认算法是检查文件大小和mtime不同的文件,使用此选项将只检查文件大小。
-u --update:仅在源mtime比目标已存在文件的mtime新时才拷贝。注意,该选项是接收端判断的,不会影响删除行为。
-d --dirs:以不递归的方式拷贝目录本身。默认递归时,如果源为"dir1/file1",则不会拷贝dir1目录,使用该选项将拷贝dir1但不拷贝file1。
--max-size:限制rsync传输的最大文件大小。可以使用单位后缀,还可以是一个小数值(例如:"--max-size=1.5m")
--min-size:限制rsync传输的最小文件大小。这可以用于禁止传输小文件或那些垃圾文件。
--exclude:指定排除规则来排除不需要传输的文件。
--delete:以SRC为主,对DEST进行同步。多则删之,少则补之。注意"--delete"是在接收端执行的,所以它是在
            :exclude/include规则生效之后才执行的。
-b --backup :对目标上已存在的文件做一个备份,备份的文件名后默认使用"~"做后缀。
--backup-dir:指定备份文件的保存路径。不指定时默认和待备份文件保存在同一目录下。
-e :指定所要使用的远程shell程序,默认为ssh。
--port :连接daemon时使用的端口号,默认为873端口。
--password-file:daemon模式时的密码文件,可以从中读取密码实现非交互式。注意,这不是远程shell认证的密码,而是rsync模块认证的密码。
-W --whole-file:rsync将不再使用增量传输,而是全量传输。在网络带宽高于磁盘带宽时,该选项比增量传输更高效。
--existing  :要求只更新目标端已存在的文件,目标端还不存在的文件不传输。注意,使用相对路径时如果上层目录不存在也不会传输。
--ignore-existing:要求只更新目标端不存在的文件。和"--existing"结合使用有特殊功能,见下文示例。
--remove-source-files:要求删除源端已经成功传输的文件。
posted @   猎博人  阅读(130)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示