Linux下的數據備份工具rsync

[toc]ssh

10.28 rsync工具介紹工具

10.29/10.30 rsync經常使用選項測試

10.31 rsync經過ssh同步.net

1、rsync工具介紹

Linux系統下有不少數據備份工具,經常使用的是rsync,從字面意思理解爲remote sync(遠程同步)。rsync不只能夠遠程同步數據(相似於scp),並且能夠本地同步數據(相似於cp),但不一樣於cp或者scp的一點是,它不會覆蓋之前的數據(若是數據已經存在),而是先判斷已經存在的數據和新數據的差別,只有數據不一樣時纔會把不相同的部分覆蓋。code

  • 若是沒有rsync命令,請使用yum install -y rsync安裝. 舉例:
[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd

sent 2465 bytes  received 31 bytes  4992.00 bytes/sec
total size is 2391  speedup is 0.96

上例中把/etc/passwd同步到/tmp/目錄下,並更名爲1.txt,若是要改爲遠程複製,數據備份的格式是: 用戶名@IP:path,好比192.168.188.128:/root/.具體用法以下:ci

[root@localhost ~]# rsync -av /etc/passwd root@192.168.72.133:/tmp/1.txt
The authenticity of host '192.168.72.133 (192.168.72.133)' can't be established.
ECDSA key fingerprint is 54:1a:44:33:2e:df:c2:58:41:cf:f3:d2:e3:69:87:b7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.72.133' (ECDSA) to the list of known hosts.
root@192.168.72.133's password: 
sending incremental file list
passwd

sent 2465 bytes  received 253 bytes  175.35 bytes/sec
total size is 2391  speedup is 0.88
  • 這裏要求遠程的機器也必須安裝有rsync,須要驗證密碼是由於這裏沒有作兩機互聯.

rsync是一個功能很是強大的工具,其命令也有不少功能特點選項。rem

1.命令語法格式:
  • [ ] rsync [OPTION]... SRC DEST
  • [ ] rsync [OPTION]... SRC [USER@]host:DEST // 第一例不加user@host,默認的就是root
  • [ ] rsync [OPTION]... [USER@]HOST:SRC DEST//從遠程目錄同步數據到本地
  • [ ] rsync [OPTION]... [USER@]HOST::SRC DEST
  • [ ] rsync [OPTION]... SRC [USER@]HOST::DEST
2.rsync經常使用選項
  • [ ] -a :包含-rtplgoD,a選項後面能夠跟--no-OPTION這個表示關閉-rlptgoD中的某一個例如 -a--no-l 等同於-rptgoD
  • [ ] -r :同步目錄時要加上,相似cp時的-r選項
  • [ ] -v :同步時顯示-些信息,讓咱們知道同步的過程
  • [ ] -l :保留軟鏈接
  • [ ] -L :加上該選項後,同步軟鏈接時會把源文件給同步
  • [ ] -p :保持文件的權限屬性
  • [ ] -o :保持文件的屬主
  • [ ] -g :保持文件的屬組
  • [ ] -D :保持設備文件信息
  • [ ] -t :保持文件的時間屬性
  • [ ] --delete :刪除DEST中SRC沒有的文件
  • [ ] --exclude :過濾指定文件,如--exclude 「logs」會把文件名含logs的文件或者目錄過濾掉,不一樣步
  • [ ] -P :顯示同步過程,好比速率,比-v更加詳細
  • [ ] -u :加上該選項後,若是DEST中的文件比SRC新,則不一樣步
  • [ ] -z :傳輸時壓縮

上述選項筆記多,經常使用的有-a,-v,-z,--delete和--exclude.get

3.測試rsync的選項 爲了更換的測試,須要新建目錄和文件同步

[root@localhost ~]# ls
anaconda-ks.cfg  grep          httpd-2.4.29.tar.gz   [root@localhost  split_dir
awk              httpd-2.4.29  initial-setup-ks.cfg  sed              xaa
## ls 下沒有合適的目錄和文件,只能從新建
[root@localhost ~]# mkdir rsync

[root@localhost ~]# cd rsync
[root@localhost rsync]# mkdir 111
[root@localhost rsync]# touch 1 2 3 /root/123.txt
[root@localhost rsync]# ln -s /root/123.txt ./134.txt //創建軟連接
[root@localhost rsync]# ls -l
總用量 0
-rw-r--r-- 1 root root  0 1月  31 23:22 1

3.1 -a選項用法it

mark

mark

3.2 使用-L的用法

[root@localhost ~]# rsync -avL ./rsync/ ./test2/
sending incremental file list
111/
111/123.txt

sent 158 bytes  received 35 bytes  386.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls -l test2/
總用量 0
-rw-r--r-- 1 root root  0 1月  31 23:22 1
drwxr-xr-x 2 root root 21 1月  31 23:50 111
-rw-r--r-- 1 root root  0 1月  31 23:22 134.txt
-rw-r--r-- 1 root root  0 1月  31 23:22 2
-rw-r--r-- 1 root root  0 1月  31 23:22 3
-rw-r--r-- 1 root root  0 1月  31 23:22 321.txt

3.3 使用delete選項

[root@localhost ~]# rsync -avL --delete ./rsync/ ./test2/
sending incremental file list

sent 116 bytes  received 13 bytes  258.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls
123.txt  556.txt          awk   httpd-2.4.29         initial-setup-ks.cfg  rsync  split_dir  xaa
321.txt  anaconda-ks.cfg  grep  httpd-2.4.29.tar.gz  [root@localhost       sed    test2

3.4 使用--exclude選項

[root@localhost ~]# mkdir test1
[root@localhost ~]# touch test1/4
[root@localhost ~]# rsync -a --exclude="4" test1/ test2/
[root@localhost ~]# ls test1
4

3、經過ssh方式同步,rsync應用

第一種方法:把文件推出去

在以前介紹的rsync的5種命令格式中,第二種和第三種(一個冒號)就屬於經過ssh的方式,備份數據,這種方式其實就是讓用戶登入到遠程機器,而後執行rsync的任務

[root@localhost ~]# rsync -av test2/ 192.168.72.133:/tmp/xavi/
root@192.168.72.133's password: 
sending incremental file list
./
1
134.txt
2
3
321.txt
111/
111/123.txt

sent 356 bytes  received 133 bytes  88.91 bytes/sec
total size is 0  speedup is 0.00

上述就是前面介紹的第二種方式了,經過ssh複製的數據,須要輸入另外以太極的root帳戶的密碼。

固然也能夠用第三種方式複製,以下所示:

1.命令語法格式:

  • [ ] rsync [OPTION]... SRC DEST
  • [ ] rsync [OPTION]... SRC [USER@]host:DEST // 第一例不加user@host,默認的就是root
  • [ ] rsync [OPTION]... [USER@]HOST:SRC DEST//從遠程目錄同步數據到本地
  • [ ] rsync [OPTION]... [USER@]HOST::SRC DEST
  • [ ] rsync [OPTION]... SRC [USER@]HOST::DEST
  • 固然也能夠第三種方式複製,以下所示(相似於把遠程那邊的文件拉過來)
[root@localhost ~]# rsync -avP 192.168.72.133:/tmp/xavi/ /tmp/test1
root@192.168.72.133's password: 
receiving incremental file list
created directory /tmp/test1
./
1
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=6/8)
134.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=5/8)
2
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=4/8)
3
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=3/8)
321.txt
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=2/8)
111/
111/123.txt
           0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=0/8)

sent 132 bytes  received 361 bytes  89.64 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# rsync -avP 192.168.72.133:/tmp/xavi/ /tmp/test1
root@192.168.72.133's password: 
receiving incremental file list
created directory /tmp/test1
./
1
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=6/8)
134.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=5/8)
2
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=4/8)
3
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=3/8)
321.txt
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=2/8)
111/
111/123.txt
           0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=0/8)

sent 132 bytes  received 361 bytes  89.64 bytes/sec
total size is 0  speedup is 0.00
  • 制定端口的鏈接 -e "ssh -p 22"
[root@localhost ~]# rsync -avP -e "ssh -p 22" /etc/passwd  192.168.72.133:/tmp/xavi.txt
root@192.168.72.133's password: 
sending incremental file list
passwd
        2391 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 2465 bytes  received 31 bytes  713.14 bytes/sec
total size is 2391  speedup is 0.96
  • ssh切換到遠程用戶端 ssh -p 22 IP地址 mark
相關文章
相關標籤/搜索