第8章:Shell腳本歸檔與壓縮
第8章:Shell腳本歸檔與壓縮
定期備份不可小視,我們可以通過shell腳本來實現備份自動化。其中數據備份一般要使用到歸檔與壓縮,歸檔與壓縮對於系統管理員或普通用戶來說是比較常用的工具,有許多不同的壓縮格式,要結合不同的使用方法才可以獲得最佳的壓縮效果。還有文件加密也是一種保護數據常用的方法,文件加密之前通常都要先進行歸檔和壓縮。
8.1、tar 歸檔
tar命令主要用來歸檔文件,可以用tar將多個文件和文件夾保存為單個文件,同時還能保留所有文件的屬性、權限等。
範例1:創建tar歸檔
[[email protected] home]# tar --help | grep ‘\-f\,‘
-f, --file=ARCHIVE 使用歸檔文件或 ARCHIVE 設備
[[email protected] home]# tar --help | grep "\-c\," #查看系統tar幫助 –c選項的說明
-c, --create 創建一個新歸檔
[[email protected] home]# for((i=1;i<4;i++));do ifconfig > font$i.txt;done #使用for循環創建3個測試文本
[[email protected]
font1.txt font2.txt font3.txt
[[email protected] home]# tar -cf font.tar font1.txt font2.txt font3.txt
範例2:查看歸檔及追加歸檔文件
[[email protected] home]# tar --help | grep ‘\-v\,‘
-v, --verbose 詳細地列出處理的文件
[[email protected] home]# tar --help | grep ‘\-r\,‘
-r, --append 追加文件至歸檔結尾
[[email protected] home]# tar -tf font.tar #查看歸檔文件
font1.txt
font2.txt
font3.txt
[[email protected] home]# tar -rvf font.tar font4.txt
font4.txt
[[email protected] home]# tar -tf font.tar
font1.txt
font2.txt
font3.txt
font4.txt
[[email protected] home]# tar -tvf font.tar #查看更加詳細的歸檔文件
-rw-r--r-- root/root 10240 2017-04-29 18:05 font1.txt
-rw-r--r-- root/root 884 2017-04-29 18:03 font2.txt
-rw-r--r-- root/root 884 2017-04-29 18:03 font3.txt
-rw-r--r-- root/root 884 2017-04-29 18:14 font4.txt
範例3:從歸檔文件中提取文本或文件夾
[[email protected] home]# tar --help | grep ‘\-x\,‘
-x, --extract, --get 從歸檔中解出文件
[[email protected] home]# tar --help | grep ‘\-C\,‘
-C, --directory=DIR 改變至目錄 DIR
[[email protected] home]# tar -xf font.tar
[[email protected] home]# ls
font1.txt font2.txt font3.txt font4.txt font.tar lost+found test.sh
[[email protected] home]# tar -xf font.tar -C /mnt
[[email protected] home]# ls /mnt/
font1.txt font2.txt font3.txt font4.txt
[[email protected] home]# tar -tvf font.tar
-rw-r--r-- root/root 884 2017-04-29 18:27 font1.txt
-rw-r--r-- root/root 884 2017-04-29 18:27 font2.txt
-rw-r--r-- root/root 884 2017-04-29 18:27 font3.txt
-rw-r--r-- root/root 884 2017-04-29 18:27 font4.txt
[[email protected] home]# tar -xvf font.tar font2.txt
font2.txt
範例4:兩個tar歸檔文件進行合並
[[email protected] home]# tar --help | grep "\-A\,"
-A, --catenate, --concatenate 追加 tar 文件至歸檔
[[email protected] home]# tar -cf test01.tar font1.txt font2.txt
[[email protected] home]# tar -cf test02.tar font3.txt font4.txt
[[email protected] home]# tar -Af test01.tar test02.tar #把test02.tar文件合並到test01.tar
範例5:從歸檔文件中刪除文件
[[email protected] home]# tar --help | grep "\-delete"
--delete 從歸檔(非磁帶!)中刪除
[[email protected] home]# tar -tf test01.tar
font1.txt
font2.txt
font3.txt
font4.txt
[[email protected] home]# tar --delete --file test01.tar font2.txt
[[email protected] home]# tar -tf test01.tar
font1.txt
font3.txt
font4.txt
範例6:tar 命令把文件進行歸檔和壓縮,-z對.gz壓縮格式,-j對bz2壓縮格式
[[email protected] home]# tar -zcvf file.tar.gz font1.txt font3.txt font4.txt
font1.txt
font3.txt
font4.txt
[[email protected] home]# ls -l | head -n 3
-rw-r--r--. 1 root root 589 4月 29 22:59 file.tar.gz
-rw-r--r--. 1 root root 45 4月 29 22:56 font1.txt
範例7:tar命令解壓歸檔文件
[[email protected] home]# tar -zxvf file.tar.gz
font1.txt
font3.txt
font4.txt
8.2、gzip壓縮與gunzip解壓
gzip只能夠壓縮單個文件,而無法對目錄和多個文件進行歸檔,因些需要tar進行歸檔後再使用gzip進行壓縮,生成壓縮格式後綴為.gz
範例1:gzip默認會刪除原文件生成一個壓縮文件test01.tar.gz
[[email protected] home]# gzip font1.txt
[[email protected] home]# ls -l
-rw-r--r--. 1 root root 401 4月 29 18:27 font1.txt.gz
-rw-r--r--. 1 root root 884 4月 29 18:27 font3.txt
範例2:gunzip解壓會刪除原壓縮文件解壓出文件
[[email protected] home]# gunzip font1.txt.gz
[[email protected] home]# ls -l font1.txt
-rw-r--r--. 1 root root 884 4月 29 18:27 font1.txt
範例3:gzip -l 選項查看壓縮文件屬性信息
[[email protected] home]# gzip -l font1.txt.gz
compressed uncompressed ratio uncompressed_name
401 884 58.7% font1.txt
範例4:將font4.txt內容輸入,gzip打包輸出重寫向到test.gz
[[email protected] home]# cat font4.txt | gzip -c > test.gz
範例5:先使用tar進行歸檔再進行gzip壓縮
[[email protected] home]# tar -cvf file.tar font3.txt font4.txt
font3.txt
font4.txt
[[email protected] home]# gzip file.tar
[[email protected] home]# ls -l file.tar.gz
-rw-r--r--. 1 root root 515 4月 29 23:35 file.tar.gz
範例6:先gzip進行解壓.gz再使用tar解壓tar
[[email protected] home]# ls -l file.tar.gz
-rw-r--r--. 1 root root 515 4月 29 23:35 file.tar.gz
[[email protected] home]# gunzip file.tar.gz
[[email protected] home]# ls -l file.tar
-rw-r--r--. 1 root root 10240 4月 29 23:35 file.tar
[[email protected] home]# tar -xvf file.tar
font3.txt
font4.txt
8.3、bzip2壓縮與bunzip2解壓
Bunzip2是另一種與bzip類似的壓縮技術,壓縮格式後綴.bz2
範例1:bzip2默認會刪除原文件生成一個壓縮文件font3.txt.bz2
[[email protected] home]# bzip2 font3.txt
[[email protected] home]# ls -l font3.txt.bz2
-rw-r--r--. 1 root root 462 4月 29 18:27 font3.txt.bz2
範例2:bunzip2解壓會刪除原壓縮文件解壓出文件
[[email protected] home]# bunzip2 font3.txt.bz2
[[email protected] home]# ls -l font3.txt
-rw-r--r--. 1 root root 884 4月 29 18:27 font3.txt
範例3:將font3.txt內容輸入,bzip2打包輸出重寫向到test.bz2
[[email protected] home]# cat font3.txt | bzip2 -c > test.bz2
範例4:先使用tar進行歸檔再進行bzip2壓縮
[[email protected] home]# tar -cvf file.tar font3.txt
[[email protected] home]# ls -l file.tar
-rw-r--r--. 1 root root 10240 4月 30 00:16 file.tar
[[email protected] home]# bzip2 file.tar
[[email protected] home]# ls -l file.tar.bz2
-rw-r--r--. 1 root root 545 4月 30 00:16 file.tar.bz2
範例5:先bzip2進行解壓.bz2再使用tar解壓tar
[[email protected] home]# ls -l file.tar.bz2
-rw-r--r--. 1 root root 545 4月 30 00:16 file.tar.bz2
[[email protected] home]# bunzip2 file.tar.bz2
[[email protected] home]# ls -l file.tar
-rw-r--r--. 1 root root 10240 4月 30 00:16 file.tar
[[email protected] home]# tar -xvf file.tar
font3.txt
8.4、zip歸檔和壓縮
zip作為一種流行的壓縮格式,在gzip或bzip2使用沒那麽廣泛,但在因特網上的文件通常都采用這種格式。
範例1:zip壓縮不會刪除原文件, -r 選項對目錄和文件進行遞歸壓縮。
[[email protected] home]# zip file.zip font3.txt
adding: font3.txt (deflated 58%)
[[email protected] home]# zip -r file.zip file/
adding: file/ (stored 0%)
adding: file/file.zip (stored 0%)
adding: file/abc/ (stored 0%)
adding: file/abc/file.zip (stored 0%)
[[email protected] home]# ls -l file.zip
-rw-r--r--. 1 root root 1704 4月 30 00:46 file.zip
範例2:unzip從zip文件中提取內容,完成之後不會刪除zip壓縮
[[email protected] home]#unzip file.zip
Archive: file.zip
creating: file/
extracting: file/file.zip
creating: file/abc/
extracting: file/abc/file.zip
範例3: -u 選項更新壓縮歸檔文件內容, -d 刪除壓縮歸檔文件中的刪除內容
[[email protected] home]# zip file.zip -u font4.txt #更新
adding: font4.txt (deflated 58%)
[[email protected] mnt]# zip -d file.zip /file/abc/file.zip #刪除指定壓縮歸檔文件目錄下的文件
deleting: file/abc/file.zip
8.5、加密工具與散列
範例1:gpg加密和解密文本,它采用密鑰簽名技術保護文件內容。
[[email protected] home]# gpg -c font4.txt #提示輸入加密密碼
[[email protected] home]# ls -l font4.txt.gpg
-rw-r--r--. 1 root root 429 4月 30 01:36 font4.txt.gpg
[[email protected] home]# gpg font4.txt.gpg #輸入解密密碼進行解密
gpg: 3DES 加密過的數據
can‘t connect to `/root/.gnupg/S.gpg-agent‘: 沒有那個文件或目錄
agpg: 以 1 個密碼加密
gpg: 警告:報文未受到完整的保護
範例2:base64是一組類似編碼方案,它通過將ASCII字符轉換成以64為基數的形式來用ASCII字符串描述二進制數據。Base64可以用來對編碼和解碼base64字符串。
[[email protected] home]# cat file.txt
abc123
abc123
abc123
[[email protected] home]# base64 file.txt > outputfile.txt #加密生成outputfile.txt加密文件
[[email protected] home]# cat outputfile.txt
YWJjMTIzCmFiYzEyMwphYmMxMjMK
[[email protected] home]# base64 -d outputfile.txt > jiemifile.txt #解密outputfile.txt生成jimifile.txt解密文件
[[email protected] home]# cat jiemifile.txt
abc123
abc123
abc123
8.6、raync備份系統快照
rsync 可以對位於不同位置的文件和目錄進行備份,它可以借助差異計算以及壓縮技術來最小化數據傳輸量。rsync還還支持網絡數據傳輸,rsync還會比較源端和目的端的文件,只有文件有更新時才進行復制。
範例1:將源目錄復制到目的端 , -a 選項歸檔, -v 顯示細節信息或進度
標註:路徑格式而言,如果/bin末尾使用/,rsync會將/bin/目錄中的所有文件復制至目的端。如果/bin沒有使用 / ,rsync會將/bin目錄本身復制至目的端。
[[email protected] home]# rsync -av /bin/ /home/file #將/bin/目錄中的文件復制到/home/file目錄下
[[email protected] home]# rsync -av /bin /home/ #將/bin目錄復制到/home/目錄下
[[email protected] home]# rsync –av /bin/ [email protected]:/home/file #將/bin/目錄復制到遠程主機/home/file目錄下
範例2: --exclude 排除不在歸檔範圍內的部分文件
[[email protected] home]# rsync -av /bin /home/ --exclude “*.txt”
8.7、dd命令克隆磁盤
dd命令是同硬盤和分區打交道時,我們可能需要創建所有分區的副本或備份,而不僅僅是復制內容(不僅是各個硬盤分區,而且還包括引誌記錄、分區表等信息)。這時,我們就可以使用dd命令,它能夠用於克隆任何類型的磁盤,如果硬盤、閃存、CD、DVD等。
dd格式如下:
dd if=source of=target bs=block_size count=count
其中:
if 代表輸入文件或輸入設備路徑
of 代表目標文件或目標設備路徑
bs 代表塊大小(通常以2的冪數形式給出,如果512 、1024、2048等)。Count是需要復制的塊數。
需要復制的字節總數=塊大小 * count
通過指定count,我們可以限制從輸入文件復制至目標的字節數。如果不指定count,dd命令會對輸入文件進行復制,直到遇見文件結束標誌(EOF)為止。
範例1:備份sda1分區,使用備份恢復分區。
[[email protected] home]# df -h
文件系統 容量 已用 可用 已用%% 掛載點
/dev/sda2 97G 4.5G 87G 5% /
tmpfs 996M 112K 996M 1% /dev/shm
/dev/sda1 485M 30M 430M 7% /boot
/dev/sda3 58G 188M 55G 1% /home
/dev/sr0 4.0G 4.0G 0 100% /media/CentOS_6.0_Final
[[email protected] home]# dd if=/dev/sda1 of=/home/sda1_boot.img #備份分區
記錄了1024000+0 的讀入
記錄了1024000+0 的寫出
524288000字節(524 MB)已復制,35.5123 秒,14.8 MB/秒
[[email protected] home]# ls -l sda1_boot.img
-rw-r--r--. 1 root root 524288000 5月 3 17:31 sda1_boot.img
[[email protected] home]# dd if=/home/sda1_boot.img of=/dev/sda1 #恢復分區
記錄了1024000+0 的讀入
記錄了1024000+0 的寫出
524288000字節(524 MB)已復制,9.14704 秒,57.3 MB/秒
範例2:永久刪除一個分區中所有數據,使用dd向該分區中寫入0傎,/dev/zero是一個字符設備,它總是會返回字符 ’\0’
[[email protected] home]# dd if=/dev/zero of=/dev/sda1
範例3:在容量相同的硬盤間進行克隆,/dev/sda是原數據端,/dev/sdb目的數據端。
[[email protected] home]# dd if=/dev/sda of=/dev/sdb
[[email protected] home]# dd if=/dev/cdrom of=cdrom.iso #制作CD ROM的鏡像(ISO文件)
第8章:Shell腳本歸檔與壓縮