linux文件同步工具- rsync

rsync 文件同步工具

rsync命令是一個遠程數據同步工具,可經過LAN/WAN快速同步多臺主機間的文件。rsync使用所謂的「rsync算法」來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不一樣部分,而不是每次都整份傳送,所以速度至關快。 rsync是一個功能很是強大的工具,其命令也有不少功能特點選項,咱們下面就對它的選項一一進行分析說明。python

格式

  • rsync [OPTION]... SRC DEST
  • rsync [OPTION]... SRC [USER@]host:DEST
  • rsync [OPTION]... [USER@]HOST:SRC DEST
  • rsync [OPTION]... [USER@]HOST::SRC DEST
  • rsync [OPTION]... SRC [USER@]HOST::DEST
  • rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

選項

  • -a 包含-rtplgoD
  • -r 同步目錄時要加上,相似cp時的-r選項
  • -v 同步時顯示一些信息,讓咱們知道同步的過程
  • -l 保留軟鏈接
  • -L 加上該選項後,同步軟鏈接時會把源文件給同步
  • -p 保持文件的權限屬性
  • -o 保持文件的屬主
  • -g 保持文件的屬組
  • -D 保持設備文件的信息
  • -t 保持文件的時間屬性
  • --delete 刪除SEDT中SRC沒有的文件
  • --exclude過濾指定文件,如--exclude "logs"會把文件名包含logs的文件或者目錄過濾掉,不一樣步
  • -P 顯示同步過程,好比速率,比-v更加詳細
  • -u 加上該選項後,若是DEST中的文件比SRC新,則不一樣步
  • -z 傳輸時壓縮

舉例說明

  • 建立目錄test
  • 在目錄test中建立文件1.txt>內容111,222,33三、 軟鏈接文件xuexi.sorft
[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
  • 將test目錄 同步到/tmp/下並更名test2
[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
  • 加上L 後同步時 選項a 裏面包含的小寫l做用將失效
[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
  • 在/tmp/test2/目錄中新增一個文件new.txt ,在同步的時候加上 --delete 刪除test2中test沒有的文件
[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
  • 在同步目錄test2時,不想同步包含「.txt」的文件時可使用 --exclude過濾指定文件
[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 // 由於軟鏈接的源文件沒有同步過來,因此這邊顯示的文件爲錯誤的軟鏈接。
  • -P 顯示同步過程,好比速率,比-v更加詳細
[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
  • 若是目標test2目錄的文件內容發生改變,不想被源文件同步時覆蓋須要加上-u選項: 加上該選項後,若是test2目錄中的文件比test新,則不一樣步
[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

rsync 經過ssh方式同步

  • rsync -av /root/test/ 192.168.5.132:/root/test2

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
  • rsync -av -e "ssh -p 22" /root/test/ 192.168.5.132:/root/test3/······ 指定端口同步

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
  • rsync 經過服務的方式同步
  • 要編輯配置文件/etc/rsyncd.conf
  • 啓動服務rsync --daemon
  • 格式:rsync -av /root/test/ 192.168.5.130::module/dir/

  • rsyncd.conf樣例

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


  • rsyncd.conf配置文件詳解
  • port:指定在哪一個端口啓動rsyncd服務,默認是873端口。
  • log file:指定日誌文件。
  • pid file:指定pid文件,這個文件的做用涉及服務的啓動、中止等進程管理操做。
  • address:指定啓動rsyncd服務的IP。假如你的機器有多個IP,就能夠指定由其中一個啓動rsyncd服務,若是不指定該參數,默認是在所有IP上啓動。
  • []:指定模塊名,裏面內容自定義。
  • path:指定數據存放的路徑。
  • use chroot true|false:表示在傳輸文件前首先chroot到path參數所指定的目錄下。這樣作的緣由是實現額外的安全防禦,但缺點是須要以roots權限,而且不能備份指向外部的符號鏈接所指向的目錄文件。默認狀況下chroot值爲true,若是你的數據當中有軟鏈接文件,阿銘建議你設置成false。

將機器B的文件同步到A機器上面(不使用密碼的狀況下)

A機器

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

B機器

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

在同步的過程當中須要將firewalld服務關閉。

systemctl stop firewalld

在同步過程當中能夠指定端口 使用--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
相關文章
相關標籤/搜索