rsync备份同步
rsync常用语法
1.安装
yum install rsync -y
2.命令语法,分几个模式
- 本地模式
rsync 参数 源路径 目标路径
- 远程模式,推送方式,把自己的数据推送到另一台机器上(上传)
语法1
rsync 参数 源路径 user@ip:目标路径
语法2
rsync 参数 源路径 user@ip::目标路径
- 远程模式,拉取方式,拉取别人机器的数据到自己的机器上(下载)
rsync 参数 user@ip:源路径 目标路径
rsync 参数 user@ip::源路径目标路径
参数解释
-v 详细模式输出
-a 归档模式,递归的方式传输文件,并保持文件的属性,等同于 -rlptgoD
-r 递归拷贝目录
-l 保留软链接
-p 保留原有权限
-t 保留原有时间(修改)
-g 保留属组权限
-o 保留属主权限
-D 等于--devices --specials 表示支持b,c,s,p类型的文件
-R 保留相对路径
-H 保留硬链接
-A 保留ACL策略
-e 指定要执行的远程shell命令
-E 保留可执行权限
-X 保留扩展属性信息 a属性
比较常用的组合参数
rsync -avzP
-a 保持文件原有属性
-v 显示传输细节情况
-z 对传输数据压缩传输
-P 显示文件传输的进度信息
本地同步
语法
注意:
1. 本地数据同步的时候,源目录后面的“/”会影响同步的结果
# rsync -av /dir1/ /dir3 //只同步目录下面的文件到指定的路径
# rsync -av /dir1 /dir2 //将当前目录dir1和目录下的所有文件一起同步
2. -R:不管加不加"/",都会将源数据的绝对路径一起同步
# rsync -avR /dir1/ /dir2/
3. --delete:删除目标目录里多余的文件
# rsync -avR --delete /dir1/ /dir2/
rsync配置需要限速,因为rsync在传输数据时,会占用大量的磁盘IO,如果是网络传输,会占用网络带宽,导致其他程序收到影响
所以rsync备份服务一般是在夜里或凌晨操作
选项 --bwlimit=10
iotop 用来监视磁盘I/O使用状况的工具,iotop具有与top相似的UI,其中包括PID、用户、I/O、进程等相关信息。Linux下的IO统计工具如iostat,nmon等大多数是只能统计到per设备的读写情况,如果你想知道每个进程是如何使用IO的就比较麻烦,使用iotop命令可以很方便的查看。
iotop常用快捷键:
左右箭头:改变排序方式,默认是按IO排序。
r:改变排序顺序。
o:只显示有IO输出的进程。
p:进程/线程的显示方式的切换。
a:显示累积使用量。
q:退出。
###文件同步
[root@rsync-41 ~]#rsync -avzP /var/log/messages /opt/
sending incremental file list
messages
434,619 100% 42.58MB/s 0:00:00 (xfr#1, to-chk=0/1)
sent 58,303 bytes received 35 bytes 116,676.00 bytes/sec
total size is 434,619 speedup is 7.45
[root@rsync-41 ~]#ls /opt
messages
[root@rsync-41 ~]#
###生成一个5G文件
[root@rsync-41 ~]#dd if=/dev/zero bs=100M count=50 of=/var/log/my_self.log
50+0 records in
50+0 records out
5242880000 bytes (5.2 GB) copied, 217.995 s, 24.1 MB/s
[root@rsync-41 ~]#rsync -avzP --bwlimit=20M /var/log/my_self.log /opt/
sending incremental file list
my_self.log
725,647,360 13% 27.05MB/s 0:02:43
###文件夹同步
[root@rsync-41 /opt]#rsync -avzP /var/log /opt
sending incremental file list
log/
log/boot.log
27,038 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=34/36)
log/btmp
384 100% 375.00kB/s 0:00:00 (xfr#2, to-chk=33/36)
log/cron
11,927 100% 11.37MB/s 0:00:00 (xfr#3, to-chk=32/36)
log/dmesg
123,225 100% 58.76MB/s 0:00:00 (xfr#4, to-chk=31/36)
log/dmesg.old
123,368 100% 29.41MB/s 0:00:00 (xfr#5, to-chk=30/36)
log/firewalld
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=29/36)
log/grubby_prune_debug
193 100% 47.12kB/s 0:00:00 (xfr#7, to-chk=28/36)
log/lastlog
292,000 100% 46.41MB/s 0:00:00 (xfr#8, to-chk=27/36)
log/maillog
590 100% 96.03kB/s 0:00:00 (xfr#9, to-chk=26/36)
log/messages
439,507 100% 26.20MB/s 0:00:00 (xfr#10, to-chk=25/36)
log/secure
9,171 100% 76.55kB/s 0:00:00 (xfr#11, to-chk=24/36)
log/spooler
0 100% 0.00kB/s 0:00:00 (xfr#12, to-chk=23/36)
log/tallylog
0 100% 0.00kB/s 0:00:00 (xfr#13, to-chk=22/36)
log/vmware-vgauthsvc.log.0
4,447 100% 36.80kB/s 0:00:00 (xfr#14, to-chk=21/36)
log/vmware-vmsvc.log
5,681 100% 21.34kB/s 0:00:00 (xfr#15, to-chk=20/36)
log/wtmp
9,600 100% 23.44kB/s 0:00:00 (xfr#16, to-chk=19/36)
log/yum.log
2,912 100% 2.42kB/s 0:00:01 (xfr#17, to-chk=18/36)
log/anaconda/
log/anaconda/X.log
20,654 100% 15.15kB/s 0:00:01 (xfr#18, to-chk=12/36)
log/anaconda/anaconda.log
25,614 100% 18.63kB/s 0:00:01 (xfr#19, to-chk=11/36)
log/anaconda/ifcfg.log
11,781 100% 8.46kB/s 0:00:01 (xfr#20, to-chk=10/36)
log/anaconda/journal.log
1,792,088 100% 1.09MB/s 0:00:01 (xfr#21, to-chk=9/36)
log/anaconda/ks-script-AZcAyM.log
0 100% 0.00kB/s 0:00:00 (xfr#22, to-chk=8/36)
log/anaconda/ks-script-sXd5Y_.log
0 100% 0.00kB/s 0:00:00 (xfr#23, to-chk=7/36)
log/anaconda/packaging.log
109,746 100% 515.26kB/s 0:00:00 (xfr#24, to-chk=6/36)
log/anaconda/program.log
31,911 100% 147.00kB/s 0:00:00 (xfr#25, to-chk=5/36)
log/anaconda/storage.log
89,650 100% 407.20kB/s 0:00:00 (xfr#26, to-chk=4/36)
log/anaconda/syslog
391,974 100% 1.48MB/s 0:00:00 (xfr#27, to-chk=3/36)
log/audit/
log/audit/audit.log
316,361 100% 983.91kB/s 0:00:00 (xfr#28, to-chk=2/36)
log/rhsm/
log/sa/
log/sa/sa16
25,472 100% 78.97kB/s 0:00:00 (xfr#29, to-chk=1/36)
log/tuned/
log/tuned/tuned.log
4,044 100% 12.54kB/s 0:00:00 (xfr#30, to-chk=0/36)
sent 392,768 bytes received 618 bytes 262,257.33 bytes/sec
total size is 3,869,338 speedup is 9.84
[root@rsync-41 /opt]#ls
log
[root@rsync-41 /opt]#
###无差异化同步
--delete选项 将目标目录的数据清空 保证完全与源目录数据一致
[root@rsync-41 /opt]#ls
cc10.log cc1.log cc2.log cc3.log cc4.log cc5.log cc6.log cc7.log cc8.log cc9.log log
[root@rsync-41 /opt]#rsync -avzP --delete /var/log/ /opt/
sending incremental file list
deleting log/tuned/tuned.log
deleting log/tuned/
deleting log/sa/sa16
deleting log/sa/
deleting log/rhsm/
deleting log/audit/audit.log
deleting log/audit/
deleting log/anaconda/syslog
deleting log/anaconda/storage.log
deleting log/anaconda/program.log
deleting log/anaconda/packaging.log
deleting log/anaconda/ks-script-sXd5Y_.log
deleting log/anaconda/ks-script-AZcAyM.log
deleting log/anaconda/journal.log
deleting log/anaconda/ifcfg.log
deleting log/anaconda/anaconda.log
deleting log/anaconda/X.log
deleting log/anaconda/
deleting log/yum.log
deleting log/wtmp
deleting log/vmware-vmsvc.log
deleting log/vmware-vgauthsvc.log.0
deleting log/tallylog
deleting log/spooler
deleting log/secure
deleting log/messages
deleting log/maillog
deleting log/lastlog
deleting log/grubby_prune_debug
deleting log/firewalld
deleting log/dmesg.old
deleting log/dmesg
deleting log/cron
deleting log/btmp
deleting log/boot.log
deleting log/
deleting cc9.log
deleting cc8.log
deleting cc7.log
deleting cc6.log
deleting cc5.log
deleting cc4.log
deleting cc3.log
deleting cc2.log
deleting cc10.log
deleting cc1.log
./
boot.log
27,038 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=34/36)
btmp
384 100% 375.00kB/s 0:00:00 (xfr#2, to-chk=33/36)
cron
15,727 100% 15.00MB/s 0:00:00 (xfr#3, to-chk=32/36)
dmesg
123,225 100% 58.76MB/s 0:00:00 (xfr#4, to-chk=31/36)
dmesg.old
123,368 100% 23.53MB/s 0:00:00 (xfr#5, to-chk=30/36)
firewalld
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=29/36)
grubby_prune_debug
193 100% 37.70kB/s 0:00:00 (xfr#7, to-chk=28/36)
lastlog
292,000 100% 39.78MB/s 0:00:00 (xfr#8, to-chk=27/36)
maillog
590 100% 82.31kB/s 0:00:00 (xfr#9, to-chk=26/36)
messages
444,507 100% 28.26MB/s 0:00:00 (xfr#10, to-chk=25/36)
secure
9,171 100% 597.07kB/s 0:00:00 (xfr#11, to-chk=24/36)
spooler
0 100% 0.00kB/s 0:00:00 (xfr#12, to-chk=23/36)
tallylog
0 100% 0.00kB/s 0:00:00 (xfr#13, to-chk=22/36)
vmware-vgauthsvc.log.0
4,447 100% 289.52kB/s 0:00:00 (xfr#14, to-chk=21/36)
vmware-vmsvc.log
5,681 100% 369.86kB/s 0:00:00 (xfr#15, to-chk=20/36)
wtmp
9,600 100% 625.00kB/s 0:00:00 (xfr#16, to-chk=19/36)
yum.log
2,912 100% 177.73kB/s 0:00:00 (xfr#17, to-chk=18/36)
anaconda/
anaconda/X.log
20,654 100% 876.95kB/s 0:00:00 (xfr#18, to-chk=12/36)
anaconda/anaconda.log
25,614 100% 1.06MB/s 0:00:00 (xfr#19, to-chk=11/36)
anaconda/ifcfg.log
11,781 100% 500.21kB/s 0:00:00 (xfr#20, to-chk=10/36)
anaconda/journal.log
1,792,088 100% 23.41MB/s 0:00:00 (xfr#21, to-chk=9/36)
anaconda/ks-script-AZcAyM.log
0 100% 0.00kB/s 0:00:00 (xfr#22, to-chk=8/36)
anaconda/ks-script-sXd5Y_.log
0 100% 0.00kB/s 0:00:00 (xfr#23, to-chk=7/36)
anaconda/packaging.log
109,746 100% 1.36MB/s 0:00:00 (xfr#24, to-chk=6/36)
anaconda/program.log
31,911 100% 399.53kB/s 0:00:00 (xfr#25, to-chk=5/36)
anaconda/storage.log
89,650 100% 1.08MB/s 0:00:00 (xfr#26, to-chk=4/36)
anaconda/syslog
391,974 100% 4.15MB/s 0:00:00 (xfr#27, to-chk=3/36)
audit/
audit/audit.log
374,481 100% 3.80MB/s 0:00:00 (xfr#28, to-chk=2/36)
rhsm/
sa/
sa/sa16
36,480 100% 378.99kB/s 0:00:00 (xfr#29, to-chk=1/36)
tuned/
tuned/tuned.log
4,044 100% 42.01kB/s 0:00:00 (xfr#30, to-chk=0/36)
sent 397,163 bytes received 1,439 bytes 797,204.00 bytes/sec
total size is 3,947,266 speedup is 9.90
[root@rsync-41 /opt]#ls
anaconda boot.log cron dmesg.old grubby_prune_debug maillog rhsm secure tallylog vmware-vgauthsvc.log.0 wtmp
audit btmp dmesg firewalld lastlog messages sa spooler tuned vmware-vmsvc.log yum.log
[root@rsync-41 /opt]#
远程拷贝
push 推送模式 上传模式
###目的机器没有装rsync报错
[root@rsync-41 ~]#rsync -avzP /var/log root@10.0.0.31:/opt/
The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:4JelK2y929VcdvxG8CUwC85uqkB/jhmGqPc1xha5Fq8.
ECDSA key fingerprint is MD5:b6:45:3a:30:e4:d4:d6:c0:60:69:73:2f:66:ab:83:56.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.31' (ECDSA) to the list of known hosts.
root@10.0.0.31's password:
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(226) [sender=3.1.2]
[root@rsync-41 ~]#
###成功案例
[root@rsync-41 ~]#rsync -avzP /var/log root@10.0.0.31:/opt/
root@10.0.0.31's password:
sending incremental file list
log/
log/boot.log
35,798 100% 2.89MB/s 0:00:00 (xfr#1, to-chk=34/36)
log/btmp
384 100% 375.00kB/s 0:00:00 (xfr#2, to-chk=33/36)
log/cron
19,822 100% 439.94kB/s 0:00:00 (xfr#3, to-chk=32/36)
log/dmesg
123,415 100% 2.56MB/s 0:00:00 (xfr#4, to-chk=31/36)
log/dmesg.old
123,225 100% 575.77kB/s 0:00:00 (xfr#5, to-chk=30/36)
log/firewalld
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=29/36)
log/grubby_prune_debug
193 100% 0.76kB/s 0:00:00 (xfr#7, to-chk=28/36)
log/lastlog
292,000 100% 1.02MB/s 0:00:00 (xfr#8, to-chk=27/36)
log/maillog
776 100% 2.79kB/s 0:00:00 (xfr#9, to-chk=26/36)
log/messages
568,166 100% 1.51MB/s 0:00:00 (xfr#10, to-chk=25/36)
log/secure
10,094 100% 25.67kB/s 0:00:00 (xfr#11, to-chk=24/36)
log/spooler
0 100% 0.00kB/s 0:00:00 (xfr#12, to-chk=23/36)
log/tallylog
0 100% 0.00kB/s 0:00:00 (xfr#13, to-chk=22/36)
log/vmware-vgauthsvc.log.0
6,009 100% 15.16kB/s 0:00:00 (xfr#14, to-chk=21/36)
log/vmware-vmsvc.log
7,745 100% 19.49kB/s 0:00:00 (xfr#15, to-chk=20/36)
log/wtmp
12,288 100% 30.30kB/s 0:00:00 (xfr#16, to-chk=19/36)
log/yum.log
2,912 100% 7.13kB/s 0:00:00 (xfr#17, to-chk=18/36)
log/anaconda/
log/anaconda/X.log
20,654 100% 48.60kB/s 0:00:00 (xfr#18, to-chk=12/36)
log/anaconda/anaconda.log
25,614 100% 59.98kB/s 0:00:00 (xfr#19, to-chk=11/36)
log/anaconda/ifcfg.log
11,781 100% 27.52kB/s 0:00:00 (xfr#20, to-chk=10/36)
log/anaconda/journal.log
1,792,088 100% 3.82MB/s 0:00:00 (xfr#21, to-chk=9/36)
log/anaconda/ks-script-AZcAyM.log
0 100% 0.00kB/s 0:00:00 (xfr#22, to-chk=8/36)
log/anaconda/ks-script-sXd5Y_.log
0 100% 0.00kB/s 0:00:00 (xfr#23, to-chk=7/36)
log/anaconda/packaging.log
109,746 100% 227.06kB/s 0:00:00 (xfr#24, to-chk=6/36)
log/anaconda/program.log
31,911 100% 65.74kB/s 0:00:00 (xfr#25, to-chk=5/36)
log/anaconda/storage.log
89,650 100% 183.93kB/s 0:00:00 (xfr#26, to-chk=4/36)
log/anaconda/syslog
391,974 100% 736.13kB/s 0:00:00 (xfr#27, to-chk=3/36)
log/audit/
log/audit/audit.log
452,711 100% 664.81kB/s 0:00:00 (xfr#28, to-chk=2/36)
log/rhsm/
log/sa/
log/sa/sa16
47,536 100% 67.67kB/s 0:00:00 (xfr#29, to-chk=1/36)
log/tuned/
log/tuned/tuned.log
5,477 100% 7.60kB/s 0:00:00 (xfr#30, to-chk=0/36)
sent 421,391 bytes received 622 bytes 93,780.67 bytes/sec
total size is 4,181,969 speedup is 9.91
[root@rsync-41 ~]#
###验证
[root@nfs-31 ~]#ls /opt
log
[root@nfs-31 ~]#
pull 拉取模式
[root@nfs-31 /opt]#rsync -aczP root@10.0.0.41:/var/log/ /opt/
The authenticity of host '10.0.0.41 (10.0.0.41)' can't be established.
ECDSA key fingerprint is SHA256:4JelK2y929VcdvxG8CUwC85uqkB/jhmGqPc1xha5Fq8.
ECDSA key fingerprint is MD5:b6:45:3a:30:e4:d4:d6:c0:60:69:73:2f:66:ab:83:56.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.41' (ECDSA) to the list of known hosts.
root@10.0.0.41's password:
receiving incremental file list
./
boot.log
35,798 100% 34.14MB/s 0:00:00 (xfr#1, to-chk=34/36)
btmp
384 100% 375.00kB/s 0:00:00 (xfr#2, to-chk=33/36)
cron
20,508 100% 19.56MB/s 0:00:00 (xfr#3, to-chk=32/36)
dmesg
123,415 100% 117.70MB/s 0:00:00 (xfr#4, to-chk=31/36)
dmesg.old
123,225 100% 117.52MB/s 0:00:00 (xfr#5, to-chk=30/36)
firewalld
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=29/36)
grubby_prune_debug
193 100% 188.48kB/s 0:00:00 (xfr#7, to-chk=28/36)
lastlog
292,000 100% 139.24MB/s 0:00:00 (xfr#8, to-chk=27/36)
maillog
776 100% 378.91kB/s 0:00:00 (xfr#9, to-chk=26/36)
messages
569,231 100% 25.85MB/s 0:00:00 (xfr#10, to-chk=25/36)
secure
10,280 100% 478.05kB/s 0:00:00 (xfr#11, to-chk=24/36)
spooler
0 100% 0.00kB/s 0:00:00 (xfr#12, to-chk=23/36)
tallylog
0 100% 0.00kB/s 0:00:00 (xfr#13, to-chk=22/36)
vmware-vgauthsvc.log.0
6,009 100% 266.73kB/s 0:00:00 (xfr#14, to-chk=21/36)
vmware-vmsvc.log
7,745 100% 343.79kB/s 0:00:00 (xfr#15, to-chk=20/36)
wtmp
12,288 100% 545.45kB/s 0:00:00 (xfr#16, to-chk=19/36)
yum.log
2,912 100% 129.26kB/s 0:00:00 (xfr#17, to-chk=18/36)
anaconda/
anaconda/X.log
20,654 100% 916.81kB/s 0:00:00 (xfr#18, to-chk=12/36)
anaconda/anaconda.log
25,614 100% 1.11MB/s 0:00:00 (xfr#19, to-chk=11/36)
anaconda/ifcfg.log
11,781 100% 522.95kB/s 0:00:00 (xfr#20, to-chk=10/36)
anaconda/journal.log
1,792,088 100% 39.75MB/s 0:00:00 (xfr#21, to-chk=9/36)
anaconda/ks-script-AZcAyM.log
0 100% 0.00kB/s 0:00:00 (xfr#22, to-chk=8/36)
anaconda/ks-script-sXd5Y_.log
0 100% 0.00kB/s 0:00:00 (xfr#23, to-chk=7/36)
anaconda/packaging.log
109,746 100% 2.23MB/s 0:00:00 (xfr#24, to-chk=6/36)
anaconda/program.log
31,911 100% 663.04kB/s 0:00:00 (xfr#25, to-chk=5/36)
anaconda/storage.log
89,650 100% 1.78MB/s 0:00:00 (xfr#26, to-chk=4/36)
anaconda/syslog
391,974 100% 7.63MB/s 0:00:00 (xfr#27, to-chk=3/36)
audit/
audit/audit.log
468,071 100% 8.93MB/s 0:00:00 (xfr#28, to-chk=2/36)
rhsm/
sa/
sa/sa16
47,536 100% 910.23kB/s 0:00:00 (xfr#29, to-chk=1/36)
tuned/
tuned/tuned.log
5,477 100% 104.88kB/s 0:00:00 (xfr#30, to-chk=0/36)
[root@nfs-31 /opt]#
[root@nfs-31 /opt]#ls
anaconda cron grubby_prune_debug rhsm tallylog wtmp
audit dmesg lastlog sa tuned yum.log
boot.log dmesg.old maillog secure vmware-vgauthsvc.log.0
btmp firewalld messages spooler vmware-vmsvc.log
[root@nfs-31 /opt]#
rsync服务模式(服务端)
需要你去 rsync-41 这台机器上去操作
yum install rsync -y
修改配置文件
cat > /etc/rsyncd.conf << 'EOF'
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = install about rsync
path = /backup
[data]
path = /data
EOF
配置文件解释,注释别写在配置文件里,写笔记上
uid = www # 运行进程的用户
gid = www # 运行进程的用户组
port = 873 # 监听端口
fake super = yes # 无需让 rsync 以 root 身份运行,允许接收文件的完整属性
use chroot = no # 禁锢推送的数据至某个目录, 不允许跳出该目录
max connections = 200 # 最大连接数
timeout = 600 # 超时时间
ignore errors # 忽略错误信息
read only = false # 对备份数据可读写
list = false # 不允许查看模块信息
auth users = rsync_backup # 定义虚拟用户,作为连接认证用户
secrets file = /etc/rsync.passwd # 定义 rsync 服务用户连接认证密码文件路径
[backup] # 定义模块信息
comment = 注释信息 # 模块注释信息
path = /backup # 定义接收备份数据目录
修改配置文件
[root@rsync-41 ~]#cat > /etc/rsyncd.conf << 'EOF'
> uid = www
> gid = www
> port = 873
> fake super = yes
> use chroot = no
> max connections = 200
> timeout = 600
> ignore errors
> read only = false
> list = false
> auth users = rsync_backup
> secrets file = /etc/rsync.passwd
> log file = /var/log/rsyncd.log
> #####################################
> [backup]
> comment = install about rsync
> path = /backup
>
> [data]
> path = /data
> EOF
创建用户及目录 修改权限
[root@rsync-41 ~]#useradd -u 1000 -M -s /sbin/nologin www
[root@rsync-41 ~]#id www
uid=1000(www) gid=1000(www) groups=1000(www)
[root@rsync-41 ~]#mkdir /data
[root@rsync-41 ~]#mkdir /backup
[root@rsync-41 ~]#chown -R www:www /data
[root@rsync-41 ~]#chown -R www:www /backup
[root@rsync-41 ~]#ll -d /data
drwxr-xr-x 2 www www 6 Jan 16 18:57 /data
[root@rsync-41 ~]#ll -d /backup
drwxr-xr-x 2 www www 6 Jan 16 18:58 /backup
[root@rsync-41 ~]#
创建虚拟用户密码
注意和配置文件里的参数对应 /etc/rsyncd.conf
[root@rsync-41 ~]#vim /etc/rsync.passwd
[root@rsync-41 ~]#cat /etc/rsync.passwd
rsync_backup:123123
[root@rsync-41 ~]#
授权,密码文件务必要降低为600权限,否则rsync会报错
[root@rsync-41 ~]#chmod 600 /etc/rsync.passwd
[root@rsync-41 ~]#ll /etc/rsync.passwd
-rw------- 1 root root 20 Jan 16 19:04 /etc/rsync.passwd
[root@rsync-41 ~]#
开机启动,运行rsync服务
[root@rsync-41 ~]#systemctl start rsyncd
[root@rsync-41 ~]#systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@rsync-41 ~]#systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2024-01-16 19:10:31 CST; 39s ago
Main PID: 2094 (rsync)
CGroup: /system.slice/rsyncd.service
└─2094 /usr/bin/rsync --daemon --no-detach
Jan 16 19:10:31 rsync-41 systemd[1]: Started fast remote file copy pro....
Jan 16 19:10:31 rsync-41 systemd[1]: Starting fast remote file copy pr....
Jan 16 19:10:31 rsync-41 rsyncd[2094]: params.c:Parameter() - Ignoring...s
Jan 16 19:10:31 rsync-41 rsyncd[2094]: rsyncd version 3.1.2 starting, ...3
Hint: Some lines were ellipsized, use -l to show in full.
[root@rsync-41 ~]#
检查端口
[root@rsync-41 ~]#netstat -tunlp | grep 2094
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2094/rsync
tcp6 0 0 :::873 :::* LISTEN 2094/rsync
[root@rsync-41 ~]#ps -ef | grep rsyncd
root 2124 1456 0 19:12 pts/0 00:00:00 grep --color=auto rsyncd
[root@rsync-41 ~]#
客户端
1.安装rsync
[root@client-242 /opt]#yum install rsync -y
2.创建密码文件,用于和rsyncd服务端认证,降低权限
[root@client-242 /opt]#echo '123123' > /etc/rsync.pwd
[root@client-242 /opt]#chmod 600 /etc/rsync.pwd
3.方法二,不用文件密码形式,采用环境变量形式,更推荐
[root@client-242 /opt]#export RSYNC_PASSWORD=yuchao666
4.此时可以进行rsync同步数据了
设置客户端密码文件
[root@nfs-31 ~]#echo '123123' > /etc/my_rsync.passwd
[root@nfs-31 ~]#cat /etc/my_rsync.passwd
123123
[root@nfs-31 ~]#chmod 600 /etc/my_rsync.passwd
[root@nfs-31 ~]#
默认无密码文件及变量
[root@nfs-31 ~]#rsync -avzP /var/log/ rsync_backup@rsync-41::backup
Password:
sending incremental file list
./
boot.log
44,377 100% 11.07MB/s 0:00:00 (xfr#1, to-chk=34/36)
btmp
384 100% 375.00kB/s 0:00:00 (xfr#2, to-chk=33/36)
cron
36,601 100% 3.49MB/s 0:00:00 (xfr#3, to-chk=32/36)
dmesg
123,349 100% 9.05MB/s 0:00:00 (xfr#4, to-chk=31/36)
dmesg.old
123,269 100% 7.35MB/s 0:00:00 (xfr#5, to-chk=30/36)
firewalld
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=29/36)
grubby_prune_debug
193 100% 11.09kB/s 0:00:00 (xfr#7, to-chk=28/36)
lastlog
292,000 100% 13.92MB/s 0:00:00 (xfr#8, to-chk=27/36)
maillog
962 100% 46.97kB/s 0:00:00 (xfr#9, to-chk=26/36)
messages
749,381 100% 14.59MB/s 0:00:00 (xfr#10, to-chk=25/36)
secure
12,161 100% 215.93kB/s 0:00:00 (xfr#11, to-chk=24/36)
spooler
0 100% 0.00kB/s 0:00:00 (xfr#12, to-chk=23/36)
tallylog
0 100% 0.00kB/s 0:00:00 (xfr#13, to-chk=22/36)
vmware-vgauthsvc.log.0
7,332 100% 127.86kB/s 0:00:00 (xfr#14, to-chk=21/36)
vmware-vmsvc.log
9,220 100% 130.49kB/s 0:00:00 (xfr#15, to-chk=20/36)
wtmp
13,440 100% 164.06kB/s 0:00:00 (xfr#16, to-chk=19/36)
yum.log
2,912 100% 35.55kB/s 0:00:00 (xfr#17, to-chk=18/36)
anaconda/
anaconda/X.log
20,654 100% 249.01kB/s 0:00:00 (xfr#18, to-chk=12/36)
anaconda/anaconda.log
25,614 100% 308.81kB/s 0:00:00 (xfr#19, to-chk=11/36)
anaconda/ifcfg.log
11,781 100% 138.61kB/s 0:00:00 (xfr#20, to-chk=10/36)
anaconda/journal.log
1,792,088 100% 14.61MB/s 0:00:00 (xfr#21, to-chk=9/36)
anaconda/ks-script-AZcAyM.log
0 100% 0.00kB/s 0:00:00 (xfr#22, to-chk=8/36)
anaconda/ks-script-sXd5Y_.log
0 100% 0.00kB/s 0:00:00 (xfr#23, to-chk=7/36)
anaconda/packaging.log
109,746 100% 885.73kB/s 0:00:00 (xfr#24, to-chk=6/36)
anaconda/program.log
31,911 100% 253.36kB/s 0:00:00 (xfr#25, to-chk=5/36)
anaconda/storage.log
89,650 100% 700.39kB/s 0:00:00 (xfr#26, to-chk=4/36)
anaconda/syslog
391,974 100% 2.79MB/s 0:00:00 (xfr#27, to-chk=3/36)
audit/
audit/audit.log
716,147 100% 3.97MB/s 0:00:00 (xfr#28, to-chk=2/36)
rhsm/
sa/
sa/sa16
88,864 100% 451.99kB/s 0:00:00 (xfr#29, to-chk=1/36)
tuned/
tuned/tuned.log
6,655 100% 32.82kB/s 0:00:00 (xfr#30, to-chk=0/36)
sent 460,353 bytes received 629 bytes 61,464.27 bytes/sec
total size is 4,700,665 speedup is 10.20
[root@nfs-31 ~]#
### 验证
[root@rsync-41 ~]#ls /backup/
anaconda cron grubby_prune_debug rhsm tallylog wtmp
audit dmesg lastlog sa tuned yum.log
boot.log dmesg.old maillog secure vmware-vgauthsvc.log.0
btmp firewalld messages spooler vmware-vmsvc.log
[root@rsync-41 ~]#
非交互式两种方式
1.密码文件
向backup模块推送数据
###错误展示
[root@nfs-31 ~]#rsync -avzP --password-file=/etc/my_rsync.passwd /var/log/ rsync_backup@rsync-41::/backup
ERROR: The remote path must start with a module name not a /
rsync error: error starting client-server protocol (code 5) at main.c(1656) [sender=3.1.2]
###正确写法
[root@nfs-31 ~]#rsync -avzP --password-file=/etc/my_rsync.passwd /var/log/ rsync_backup@rsync-41::backup
sending incremental file list
./
boot.log
44,377 100% 11.07MB/s 0:00:00 (xfr#1, to-chk=34/36)
btmp
384 100% 375.00kB/s 0:00:00 (xfr#2, to-chk=33/36)
cron
37,845 100% 36.09MB/s 0:00:00 (xfr#3, to-chk=32/36)
dmesg
123,349 100% 39.21MB/s 0:00:00 (xfr#4, to-chk=31/36)
dmesg.old
123,269 100% 23.51MB/s 0:00:00 (xfr#5, to-chk=30/36)
firewalld
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=29/36)
grubby_prune_debug
193 100% 37.70kB/s 0:00:00 (xfr#7, to-chk=28/36)
lastlog
292,000 100% 39.78MB/s 0:00:00 (xfr#8, to-chk=27/36)
maillog
962 100% 134.21kB/s 0:00:00 (xfr#9, to-chk=26/36)
messages
751,032 100% 35.81MB/s 0:00:00 (xfr#10, to-chk=25/36)
secure
12,161 100% 593.80kB/s 0:00:00 (xfr#11, to-chk=24/36)
spooler
0 100% 0.00kB/s 0:00:00 (xfr#12, to-chk=23/36)
tallylog
0 100% 0.00kB/s 0:00:00 (xfr#13, to-chk=22/36)
vmware-vgauthsvc.log.0
7,332 100% 358.01kB/s 0:00:00 (xfr#14, to-chk=21/36)
vmware-vmsvc.log
9,220 100% 450.20kB/s 0:00:00 (xfr#15, to-chk=20/36)
wtmp
13,440 100% 656.25kB/s 0:00:00 (xfr#16, to-chk=19/36)
yum.log
2,912 100% 142.19kB/s 0:00:00 (xfr#17, to-chk=18/36)
anaconda/
anaconda/X.log
20,654 100% 960.47kB/s 0:00:00 (xfr#18, to-chk=12/36)
anaconda/anaconda.log
25,614 100% 1.16MB/s 0:00:00 (xfr#19, to-chk=11/36)
anaconda/ifcfg.log
11,781 100% 547.85kB/s 0:00:00 (xfr#20, to-chk=10/36)
anaconda/journal.log
1,792,088 100% 37.15MB/s 0:00:00 (xfr#21, to-chk=9/36)
anaconda/ks-script-AZcAyM.log
0 100% 0.00kB/s 0:00:00 (xfr#22, to-chk=8/36)
anaconda/ks-script-sXd5Y_.log
0 100% 0.00kB/s 0:00:00 (xfr#23, to-chk=7/36)
anaconda/packaging.log
109,746 100% 1.28MB/s 0:00:00 (xfr#24, to-chk=6/36)
anaconda/program.log
31,911 100% 380.04kB/s 0:00:00 (xfr#25, to-chk=5/36)
anaconda/storage.log
89,650 100% 1.02MB/s 0:00:00 (xfr#26, to-chk=4/36)
anaconda/syslog
391,974 100% 4.15MB/s 0:00:00 (xfr#27, to-chk=3/36)
audit/
audit/audit.log
735,192 100% 7.23MB/s 0:00:00 (xfr#28, to-chk=2/36)
rhsm/
sa/
sa/sa16
91,616 100% 903.72kB/s 0:00:00 (xfr#29, to-chk=1/36)
tuned/
tuned/tuned.log
6,655 100% 65.65kB/s 0:00:00 (xfr#30, to-chk=0/36)
sent 461,690 bytes received 621 bytes 924,622.00 bytes/sec
total size is 4,725,357 speedup is 10.22
[root@nfs-31 ~]#
[root@rsync-41 ~]#ls /backup/
anaconda cron grubby_prune_debug rhsm tallylog wtmp
audit dmesg lastlog sa tuned yum.log
boot.log dmesg.old maillog secure vmware-vgauthsvc.log.0
btmp firewalld messages spooler vmware-vmsvc.log
[root@rsync-41 ~]#
### 拉取数据
[root@nfs-31 ~]#rsync -avzP --password-file=/etc/my_rsync.passwd rsync_backup@rsync-41::data /opt/
receiving incremental file list
./
zero.log
209,715,200 100% 62.27MB/s 0:00:03 (xfr#1, to-chk=0/2)
sent 50 bytes received 204,088 bytes 58,325.14 bytes/sec
total size is 209,715,200 speedup is 1,027.32
[root@nfs-31 ~]#ls /opt
zero.log
[root@nfs-31 ~]#
2.生成密码变量
[root@nfs-31 ~]#export RSYNC_PASSWORD='123123'
[root@nfs-31 ~]#rsync -avzP rsync_backup@rsync-41::data /opt/
receiving incremental file list
./
zero.log
209,715,200 100% 173.01MB/s 0:00:01 (xfr#1, to-chk=0/2)
sent 50 bytes received 204,088 bytes 81,655.20 bytes/sec
total size is 209,715,200 speedup is 1,027.32
[root@nfs-31 ~]#ls /opt
zero.log
[root@nfs-31 ~]#
unset RSYNC_PASSWORD 密码失效需要重新验证密码
但愿日子清静抬头遇见的都是柔情
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?