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