rsync命令是一个远程数据同步工具,可经过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不一样部分,而不是每次都整份传送,所以速度至关快。 rsync是一个功能很是强大的工具,其命令也有不少功能特点选项,咱们下面就对它的选项一一进行分析说明。python
[root@xuexi-001 ~]# mkdir test [root@xuexi-001 test]# vi 1.txt [root@xuexi-001 test]# cat 1.txt 111 222 333 [root@xuexi-001 test]# touch xuexi.txt [root@xuexi-001 test]# ln -s xuexi.txt xuexi.sorft [root@xuexi-001 test]# ls 1.txt xuexi.sorft xuexi.txt [root@xuexi-001 test]# echo "aaa" > xuexi.txt [root@xuexi-001 test]# cat xuexi.txt aaa
[root@xuexi-001 test]# rsync -av /root/test/ /tmp/test2 sending incremental file list created directory /tmp/test2 ./ 1.txt xuexi.sorft -> xuexi.txt xuexi.txt sent 241 bytes received 93 bytes 668.00 bytes/sec total size is 31 speedup is 0.09
[root@xuexi-001 test]# rm -rf /tmp/test2/* [root@xuexi-001 test]# rsync -avL /root/test/ /tmp/test2 sending incremental file list ./ 1.txt xuexi.sorft // 此时这边同步的是一个文件并不是时软链接文件。 xuexi.txt sent 267 bytes received 76 bytes 686.00 bytes/sec total size is 26 speedup is 0.08 [root@xuexi-001 test]# ls -l /tmp/test2/ 总用量 12 -rw-r--r-- 1 root root 18 6月 18 23:19 1.txt -rw-r--r-- 1 root root 4 6月 18 23:21 xuexi.sorft -rw-r--r-- 1 root root 4 6月 18 23:21 xuexi.txt
[root@xuexi-001 test]# touch /tmp/test2/new.txt [root@xuexi-001 test]# rsync -av --delete /root/test/ /tmp/test2/ sending incremental file list deleting new.txt ./ 1.txt xuexi.sorft -> xuexi.txt xuexi.txt sent 241 bytes received 71 bytes 624.00 bytes/sec total size is 31 speedup is 0.10
[root@xuexi-001 ~]# ls -l /root/test 总用量 8 -rw-r--r-- 1 root root 18 6月 18 23:19 1.txt lrwxrwxrwx 1 root root 9 6月 18 23:20 xuexi.sorft -> xuexi.txt -rw-r--r-- 1 root root 4 6月 18 23:21 xuexi.txt [root@xuexi-001 ~]# rsync -av --exclude "*.txt" /root/test/ /tmp/test2/ sending incremental file list ./ xuexi.sorft -> xuexi.txt sent 86 bytes received 22 bytes 216.00 bytes/sec total size is 9 speedup is 0.08 [root@xuexi-001 ~]# ls -l /tmp/test2/ 总用量 0 lrwxrwxrwx 1 root root 9 6月 18 23:20 xuexi.sorft -> xuexi.txt // 由于软链接的源文件没有同步过来,因此这边显示的文件为错误的软链接。
[root@xuexi-001 ~]# rsync -avP /root/test/ /tmp/test2/ sending incremental file list ./ 1.txt 18 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/4) xuexi.sorft -> xuexi.txt xuexi.txt 4 100% 3.91kB/s 0:00:00 (xfr#2, to-chk=0/4) sent 241 bytes received 60 bytes 602.00 bytes/sec total size is 31 speedup is 0.10
[root@xuexi-001 ~]# vi /tmp/test2/1.txt 111 aaa bbb ccc 222 333 [root@xuexi-001 ~]# cat !$ cat /tmp/test2/1.txt 111 aaa bbb ccc 222 333 [root@xuexi-001 ~]# cat /root/test/1.txt 111 222 333 [root@xuexi-001 ~]# rsync -avPu /root/test/ /tmp/test2/ sending incremental file list ./ sent 130 bytes received 19 bytes 298.00 bytes/sec total size is 31 speedup is 0.21 [root@xuexi-001 ~]# cat /tmp/test2/1.txt 111 aaa bbb ccc 222 333 [root@xuexi-001 ~]# cat /root/test/1.txt 111 222 333
192.168.5.130机器算法
[root@xuexi-001 ~]# rsync -av /root/test/ 192.168.5.132:/root/test2/ root@192.168.5.132's password: sending incremental file list created directory /root/test2 ./ 1.txt xuexi.sorft -> xuexi.txt xuexi.txt sent 241 bytes received 94 bytes 95.71 bytes/sec total size is 31 speedup is 0.09
192.168.5.132机器安全
[root@localhost ~]# ls anaconda-ks.cfg test1 test2 [root@localhost ~]# ls -l test2 总用量 8 -rw-r--r-- 1 root root 18 6月 18 23:19 1.txt lrwxrwxrwx 1 root root 9 6月 18 23:20 xuexi.sorft -> xuexi.txt -rw-r--r-- 1 root root 4 6月 18 23:21 xuexi.txt
192.168.5.130机器服务器
[root@xuexi-001 ~]# rsync -av -e "ssh -p 22" /root/test/ 192.168.5.132:/root/test3/ root@192.168.5.132's password: sending incremental file list created directory /root/test3 ./ 1.txt xuexi.sorft -> xuexi.txt xuexi.txt sent 241 bytes received 94 bytes 74.44 bytes/sec total size is 31 speedup is 0.09
192.168.5.132机器ssh
[root@localhost ~]# ls -l test3 总用量 8 -rw-r--r-- 1 root root 18 6月 18 23:19 1.txt lrwxrwxrwx 1 root root 9 6月 18 23:20 xuexi.sorft -> xuexi.txt -rw-r--r-- 1 root root 4 6月 18 23:21 xuexi.txt
port=873tcp
log file=/var/log/rsync.log工具
pid file=/var/run/rsyncd.pid测试
address=192.168.133.130ui
[test]日志
path=/root/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.133.132
1.将配置文件的样例粘贴到配置文件/etc/rsync.conf中
[root@xuexi-001 ~]# vi /etc/rsyncd.conf # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area port=873 log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid address=192.168.5.130 [test] path=/tmp/rsync use chroot=true max connections=4 read only=no list=true uid=root gid=root #auth users=test #secrets file=/etc/rsyncd.passwd "/etc/rsyncd.conf" 35L, 721C 34,1 85%
2.启动rsync 服务 3.检查rsync服务是否启动 4.检查873端口 5.建立模块test的路径目录 `````mkdir /tmp/rsync
[root@xuexi-001 ~]# rsync --daemon [root@xuexi-001 ~]# ps aux |grep rsync root 2074 0.0 0.0 114696 540 ? Ss 22:21 0:00 rsync --daemon root 2584 0.0 0.0 112676 984 pts/0 R+ 23:45 0:00 grep --color=auto rsync [root@xuexi-001 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 919/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1119/master tcp 0 0 192.168.5.130:873 0.0.0.0:* LISTEN 2074/rsync tcp6 0 0 :::22 :::* LISTEN 919/sshd tcp6 0 0 ::1:25 :::* LISTEN 1119/master [root@xuexi-001 ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area port=873 log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid address=192.168.5.130 [test] path=/tmp/rsync use chroot=true max connections=4 read only=no list=true uid=root gid=root #auth users=test #secrets file=/etc/rsyncd.passwd hosts allow=192.168.5.132 [root@xuexi-001 ~]# mkdir /tmp/rsync
1.将B机器上的一个文件同步到A机器上
将B机器上的root目录下面的test3目录 同步到A机器上的/tmp/rsync目录下并更名为test110
[root@localhost ~]# rsync -avP /root/test3/ 192.168.5.130::test/test110 sending incremental file list created directory /test110 ./ 1.txt 18 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/4) xuexi.sorft -> xuexi.txt xuexi.txt 4 100% 3.91kB/s 0:00:00 (xfr#2, to-chk=0/4) sent 242 bytes received 91 bytes 31.71 bytes/sec total size is 31 speedup is 0.09
2.查看A机器上的文件
[root@xuexi-001 ~]# ls /tmp/rsync/ test110
3.将A机器上的文件同步到B机器上
[root@localhost ~]# rsync -avP 192.168.5.130::test/test110 /tmp/test111 receiving incremental file list created directory /tmp/test111 test110/ test110/1.txt 18 100% 17.58kB/s 0:00:00 (xfr#1, to-chk=2/4) test110/xuexi.sorft -> xuexi.txt test110/xuexi.txt 4 100% 3.91kB/s 0:00:00 (xfr#2, to-chk=0/4) sent 69 bytes received 260 bytes 31.33 bytes/sec total size is 31 speedup is 0.09 [root@localhost ~]# ls /tmp/ test111
在同步过程当中能够指定端口 使用--port 873
[root@localhost ~]# rsync -avP --port 873 192.168.5.130::test/test110 /tmp/test111
列出模块名,在服务器上的配置文件/etc/rsync.conf中将list=true 改成false 则不列出模块名
[root@localhost ~]# rsync --port=873 192.168.5.130:: test
[root@xuexi-001 ~]# vi /etc/rsyncd.conf # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area port=873 log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid address=192.168.5.130 [test] path=/tmp/rsync use chroot=true max connections=4 read only=no list=true uid=root gid=root auth users=test·····使用的用户名 secrets file=/etc/rsyncd.passwd ····密码文件名 "/etc/rsyncd.conf" 35L, 719C [root@xuexi-001 ~]# vi /etc/rsyncd.passwd test:111111 [root@xuexi-001 ~]# chmod 600 /etc/rsyncd.passwd ·····须要设置文件的权限 600
B机器上测试同步效果
[root@localhost ~]# rsync -avP /root/test3/ test@192.168.5.130::test/test110 //·····这时候就须要输入用户名 test@192.168.5.130··· Password: ····输入密码 sending incremental file list sent 124 bytes received 12 bytes 10.07 bytes/sec total size is 31 speedup is 0.23
在客户端B机器上定义一个密码文件 vi /etc/rsync_pass.txt
而后在同步的时候就不须要输入密码了,须要加 --password-file=/etc/rsync_pass.txt
[root@localhost ~]# vi /etc/rsync_pass.txt ······将密码文件写入 只写密码 111111 [root@localhost ~]# chmod 600 /etc/rsync_pass.txt [root@localhost ~]# rsync -avP /root/test3/ --password-file=/etc/rsync_pass.txt test@192.168.5.130::test/test110 sending incremental file list sent 124 bytes received 12 bytes 12.95 bytes/sec total size is 31 speedup is 0.23