1. 程式人生 > >Linux的rsync命令和歸檔、壓縮

Linux的rsync命令和歸檔、壓縮

一、Linux的rsync命令

1.rsync與scp的區別

     scp是複製和rsync是同步,rsync和scp在資料夾均不存在時,執行時間相差不大,但是資料夾存在的情況下差異很大。因為scp是複製:若目的地檔案不存在則新建,若存在則暴力覆蓋(改變屬性)。rsync則是同步,比較兩邊檔案是否相同,相同則保留原始檔,若存在差異就直接更新,而且rsync遠端拷貝可以附帶軟連結/硬連結。(引數-l 保留軟連結,-H 保留硬連結)。

    視情況來選擇rsync或scp,若是同步作用則rsync會快一些,若是複製作用時兩者均可(目的地無檔案

)。

rsync  [引數]  filename  [email protected]:/dir

具體引數含義:    
             -r    ##同步目錄
             -l    ##不忽略連結
             -p    ##不忽略檔案許可權
             -t    ##不忽檔案時間戳
             -g    ##不忽檔案所有組
             -o    ##不忽檔案所有人
             -D    ##不忽略裝置檔案

2.建立實驗環境

[[email protected]
~]# hostnamectl set-hostname node1 ##虛擬機器Desktop更名 [[email protected] ~]# hostnamectl set-hostname node2 ##虛擬機器Sercer更名
[[email protected] ~]cd /mnt                           ##搭建實驗環境移動目錄至/mnt
[[email protected] mnt]# touch westos{1..5}
[[email protected] mnt]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 15 11:07 westos1
-rw-r--r--. 1 root root 0 Oct 15 11:07 westos2
-rw-r--r--. 1 root root 0 Oct 15 11:07 westos3
-rw-r--r--. 1 root root 0 Oct 15 11:07 westos4
-rw-r--r--. 1 root root 0 Oct 15 11:07 westos5
[
[email protected]
mnt]# chown student.student * [[email protected] mnt]# ll total 0 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos1 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos2 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos3 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos4 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos5

[[email protected] mnt]# chmod 777 westos*
[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos1
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos2
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos3
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos4
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos5
[[email protected] mnt]# ln -s westos1 file1
[[email protected] mnt]# ll
total 0
lrwxrwxrwx. 1 root    root    7 Oct 15 11:12 file1 -> westos1
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos1
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos2
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos3
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos4
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos5

2.練習實驗開始

注意 :時刻注意rysnc傳輸過去後在node2中的屬性變化,分辨引數的含義

[[email protected] mnt]# rsync -r /mnt/* [email protected]:/mnt/
skipping non-regular file "file1"             ##跳過非普通檔案fiel快捷方式

[[email protected] ~]# cd /mnt                          ##位移到目錄/mnt下方便查詢
[[email protected] mnt]# ll
total 0
-rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos1
-rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos2
-rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos3
-rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos4
-rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos5

[[email protected] ~]# rsync -rp /mnt/* [email protected]:/mnt/
skipping non-regular file "file"

[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos1
-rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos2
-rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos3
-rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos4
-rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos5

[[email protected] ~]#rsync -rpo /mnt/* [email protected]:/mnt/
skipping non-regular file "file"

[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos1
-rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos2
-rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos3
-rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos4
-rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos5

[[email protected] ~]# rsync -rpog /mnt/* [email protected]:/mnt/
skipping non-regular file "file"

[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos1
-rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos2
-rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos3
-rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos4
-rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos5

[[email protected] ~]#rsync -rpogt /mnt/* [email protected]:/mnt/
skipping non-regular file "file"

[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos1
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos2
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos3
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos4
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos5

[[email protected] ~]# rsync -rpogtl /mnt/* [email protected]:/mnt/

[[email protected] mnt]# ll
total 0
lrwxrwxrwx. 1 root    root    7 Oct 15 11:12 file1 -> westos1
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos1
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos2
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos3
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos4
-rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos5

rysnc的練習測試到此結束

 

 

二、檔案的歸檔及壓縮

1.檔案歸檔的定義及作用

檔案歸檔,就是把多個檔案/目錄變成一個歸檔檔案

歸檔的作用:加快檔案傳輸速度

2.tar命令

tar  +  引數             ##tar命令的主旨是為了加快檔案的傳輸
引數含義:
            c		##建立
            f		##指定歸檔檔名稱
            t		##顯示歸檔檔案中的內容
            r		##向歸檔檔案中新增檔案
            --get       ##取出單個檔案
            --delete	##刪除單個檔案
            x		##取出歸檔檔案中的所有內容
            -C		##指定解檔目錄
            -z		##gz格式壓縮
            -j		##bz2格式壓縮
            -J		##xz格式壓縮

3.tar命令的練習

[[email protected] Desktop]# tar cf etc.tar /etc/ /mnt    ##tar歸檔 c建立一個歸檔 f指定歸檔檔名稱
tar: Removing leading `/' from member names       
[[email protected] Desktop]# tar tf etc.tar               ##t檢視歸檔檔案內容

[[email protected] Desktop]# tar -rf etc.tar file         ##r向歸檔檔案中新增檔案

 [[email protected] Desktop]# tar -xf etc.tar              ##取出歸檔中所有內容

 

[[email protected] Desktop]# tar -f etc.tar --get file           ##get 取出歸檔檔案中單個檔案
[[email protected] Desktop]# tar -f etc.tar --get mnt

[[email protected] Desktop]# tar -f etc.tar --delete mnt            ##delete刪除單個檔案


 

[[email protected] Desktop]# tar -xf etc.tar -C /mnt                 ##指定解檔目錄

[[email protected] Desktop]# tar zcf etc.tar.gz .    ##歸檔檔案壓縮為gz格式
tar: .: file changed as we read it
[[email protected] Desktop]# du -sh etc.tar.gz       ##檢視壓縮檔案大小
8.4M	etc.tar.gz
[[email protected] Desktop]# ls                      ##檢視目錄為解壓處理環境
etc.tar  etc.tar.gz
[[email protected] Desktop]# rm -fr etc.tar
[[email protected] Desktop]# ls
etc.tar.gz
[[email protected] Desktop]# tar zxf etc.tar.gz .    ##格式解壓 
[[email protected] Desktop]# ls
etc.tar  etc.tar.gz

 

[[email protected] Desktop]# tar jcf etc.tar.bz2 .     ##將歸檔檔案壓縮成bz2格式
tar: .: file changed as we read it
[[email protected] Desktop]# du -sh etc.tar.bz2        ##檢視檔案bz2壓縮格式大小
7.0M	etc.tar.bz2  
[[email protected] Desktop]# ls                        ##檢視環境為解壓清理環境
etc.tar  etc.tar.bz2
[[email protected] Desktop]# rm -fr etc.tar
[[email protected] Desktop]# ls
etc.tar.bz2
[[email protected] Desktop]# tar jxf etc.tar.bz2 .     ##解壓bz2格式
[[email protected] Desktop]# ls
etc.tar  etc.tar.bz2

[[email protected] Desktop]# tar Jcf etc.tar.xz .
tar: .: file changed as we read it
[[email protected] Desktop]# du -sh etc.tar.xz 
7.5M	etc.tar.xz
[[email protected] Desktop]# ls
etc.tar  etc.tar.xz
[[email protected] Desktop]# rm -fr etc.tar
[[email protected] Desktop]# ls
etc.tar.xz
[[email protected] Desktop]# tar Jxf etc.tar.xz .
[[email protected] Desktop]# ls
etc.tar  etc.tar.xz

 

4.檔案壓縮

注意:同樣大小的檔案,不同的格式壓縮大小不同且壓縮的越小,需要的時間越長。

gz 格式:

[[email protected] Desktop]# gzip etc.tar            ##壓縮成gz格式 
[[email protected] Desktop]# du -sh etc.tar.gz       ##檢視壓縮大小
8.4M	etc.tar.gz
[[email protected] Desktop]# gunzip etc.tar          ##解壓gz格式壓縮包

bz2 格式:

[[email protected] Desktop]# bzip2 etc.tar         ##壓縮成bz2格式
[[email protected] Desktop]# du -sh etc.tar.bz2    ##檢視bz2壓縮大小
7.0M	etc.tar.bz2
[[email protected] Desktop]# bunzip2 etc.tar.bz2   ##解壓bz2格式壓縮包

xz 格式:

[[email protected] Desktop]# xz etc.tar          ##壓縮成xz格式
[[email protected] Desktop]# du -sh etc.tar.xz   ##檢視xz壓縮檔案大小
7.6M	etc.tar.xz
[[email protected] Desktop]# unxz etc.tar.xz     ##解壓並解檔xz格式壓縮包