管理壓縮的tar歸檔
創(chuàng)建備份和通過網(wǎng)絡(luò)傳輸數(shù)據(jù)時矗漾,歸檔和壓縮非常有用。用來創(chuàng)建和使用備份存檔的其中一個最早也是最常見的命令是tar命令
- tar包的歸檔和壓縮
-- 1访惜、整合分散的數(shù)據(jù)
-- 2共螺、減小空間的占用 - Linux中常見的壓縮格式:
-- .gz:gzip的壓縮格式,壓縮速度快
-- .bz2:bzip2的壓縮格式膜蠢,不快不小
-- .xz:xz的壓縮格式,壓縮最慢莉兰,壓縮比例高 - 打包格式:
tar [option] /路徑/壓縮包名字 [option] 被壓縮的數(shù)據(jù)源
- 解包格式:
tar [option] /路徑/壓縮包名字 [option] 被解包的路徑
- tar命令的常用選項
-c #創(chuàng)建歸檔挑围,打包
-x #釋放歸檔,解包
-f #指定歸檔文件名稱
-z糖荒、-j杉辙、-J #調(diào)用.gz、.bz2捶朵、.xz壓縮格式的工具進行處理
-t #顯示歸檔中的文件清單
-C #指定釋放路徑
-v #顯示指令執(zhí)行過程
- 創(chuàng)建壓縮存檔案例
[root@serverb ~]# tar -jcf /home/user/log.tar.bz2 /var/log/
tar: Removing leading `/' from member names
[root@serverb ~]# tar -Jcf /home/user/log.tar.xz /var/log/
tar: Removing leading `/' from member names
[root@serverb ~]# tar -zcf /home/user/log.tar.gz /var/log/
[root@serverb user]# ls -lh
total 1.6M
-rw-r--r--. 1 root root 468K Mar 31 21:34 log.tar.bz2
-rw-r--r--. 1 root root 700K Mar 31 21:31 log.tar.gz
-rw-r--r--. 1 root root 378K Mar 31 21:34 log.tar.xz
#如果出現(xiàn)以下報錯蜘矢,是系統(tǒng)缺少bzip2軟件包,需要安裝
[root@serverb ~]# tar -jcf log.tar.bz2 /var/log/
tar: Removing leading `/' from member names
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
[root@serverb ~]# yum install bzip2
#查看歸檔
[root@serverb user]# tar -tf log.tar.gz
var/log/
var/log/tallylog
var/log/grubby_prune_debug
var/log/lastlog
var/log/wtmp
var/log/tuned/
var/log/tuned/tuned.log
var/log/audit/
var/log/audit/audit.log
var/log/qemu-ga/
... ...
- 提取壓縮存檔解包
#不用-C選項指定解壓路徑综看,默認解壓在當前路徑
[root@serverb ~]# tar -zxf /home/user/log.tar.gz -C /tmp/
[root@serverb log]# tar -jxf /home/user/log.tar.bz2 -C /tmp/
[root@serverb tmp]# tar -Jxf /home/user/log.tar.xz -C /tmp/
在系統(tǒng)之間安全的傳輸文件
- 使用secure copy傳輸文件scp命令
運行此命令時品腹,scp客戶端將使用基于密鑰的身份驗證或以提示輸入密碼的方式向遠程SSH服務(wù)器進行身份驗證,就像ssh一樣
[root@serverb ~]# scp -r /tmp/var/log/ root@192.168.2.100:/root/
The authenticity of host '192.168.2.100 (192.168.2.100)' can't be established.
ECDSA key fingerprint is SHA256:etAhB4GlCD3RMEkDMjBb3Kui1ULKK1ZeV03TRf/4pP8.
ECDSA key fingerprint is MD5:af:a3:a9:cb:8d:87:2d:74:eb:0e:b2:a8:aa:57:90:ad.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.100' (ECDSA) to the list of known hosts.
root@192.168.2.100's password:
tallylog 100% 63KB 7.3MB/s 00:00
grubby_prune_debug 100% 193 330.3KB/s 00:00
lastlog 100% 286KB 15.8MB/s 00:00
wtmp 100% 60KB 17.5MB/s 00:00
tuned.log 100% 13KB 10.8MB/s 00:00
... ...
- 使用安全文件傳輸程序傳輸文件
要以交互式從ssh服務(wù)器上傳或下載文件红碑,請使用安全文件傳輸程序sftp舞吭。sftp命令的會話使用安全身份驗證機制,并將數(shù)據(jù)加密后再與ssh服務(wù)器來回傳輸
#連接遠程ssh
[root@serverb ~]# sftp root@192.168.2.100
root@192.168.2.100's password:
Connected to 192.168.2.100.
sftp>lcd /tmp/var/
sftp> lpwd
Local working directory: /tmp/var
sftp> lls
log
sftp> mkdir /root/bak/
sftp> cd /root/bak/
sftp> pwd
Remote working directory: /root/bak
sftp> put -r log/
sftp> cd log/
sftp> ls -l
在系統(tǒng)間安全地同步文件
- 使用rsync同步文件和目錄
rsync命令是在系統(tǒng)之間安全復(fù)制文件的另一種方式。此工具采用的算法可通過僅同步已更改的文件部分來將復(fù)制的數(shù)據(jù)量最小化羡鸥。它與scp的區(qū)別在于蔑穴,如果兩個服務(wù)器間的兩個文件或目錄相似,rsync將僅復(fù)制文件系統(tǒng)間的差異部分兄春,而scp仍復(fù)制所有內(nèi)容 - rsync的常用選項
-n #用于空執(zhí)行澎剥,空執(zhí)行是對執(zhí)行命令時所發(fā)生的情況模擬,在正常運行之前使用-n選項
-a #啟用存檔模式赶舆,這樣可實現(xiàn)遞歸復(fù)制并開啟很多有用的選項哑姚,以保留文件的大部分特征(遞歸方式同步整個目錄樹、同步符號鏈接芜茵、保留權(quán)限叙量、保留時間戳、保留組所有權(quán)九串、保留文件所有者绞佩、同步設(shè)備文件)。\
不會保留硬鏈接猪钮,會增加同步時間品山,想要保留硬鏈接-H選項
-v #可提供詳細的輸出。對于故障排查和查看實時進度非常有用
- 可以使用rsync命令將本地文件或目錄的內(nèi)容和遠程計算機上的文件或目錄進行同步烤低。也可以同步兩個本地文件或目錄的內(nèi)容肘交。
#兩個本地文件同步,在源文件后面不加/會在目標文件下創(chuàng)建一個和源文件同名的子目錄然后將文件內(nèi)容一并同步
[root@serverb ~]# rsync -av /root/test /root/testdir
sending incremental file list
test/
test/file1.txt
test/file10.txt
test/file2.txt
test/file3.txt
test/file4.txt
test/file5.txt
test/file6.txt
test/file7.txt
test/file8.txt
test/file9.txt
sent 617 bytes received 210 bytes 1,654.00 bytes/sec
total size is 0 speedup is 0.00
[root@serverb ~]# cd test
[root@serverb test]# cd ../testdir/
[root@serverb testdir]# ls
test
[root@serverb testdir]# cd test/
[root@serverb test]# ls
file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt
[root@serverb test]# pwd
/root/testdir/test
#如果在源文件后面加上/扑馁,就只同步源文件內(nèi)的內(nèi)容到新目錄
[root@serverb ~]# rsync -av /root/test/ /root/testdir
sending incremental file list
./
file1.txt
file10.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
sent 604 bytes received 209 bytes 1,626.00 bytes/sec
total size is 0 speedup is 0.00
[root@serverb ~]# cd /root/testdir
[root@serverb testdir]# ls
file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt
- 如果在源文件新加一些內(nèi)容涯呻,rsync只會同步新加的內(nèi)容
[root@serverb ~]# rsync -av /root/test/ /root/testdir/
sending incremental file list
./
file10.txt
file/
file/heihei
sent 357 bytes received 65 bytes 844.00 bytes/sec
total size is 0 speedup is 0.00
#查看
[root@serverb ~]# cd testdir/
[root@serverb testdir]# ls
file file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt
[root@serverb testdir]# cd file
[root@serverb file]# ls
heihei
- 兩臺服務(wù)器之間使用rsync同步
#從本地同步到遠程目錄
[root@serverb ~]# rsync -av /root/testdir root@192.168.2.100:/tmp
root@192.168.2.100's password:
sending incremental file list
testdir/
testdir/file1.txt
testdir/file10.txt
testdir/file2.txt
testdir/file3.txt
testdir/file4.txt
testdir/file5.txt
testdir/file6.txt
testdir/file7.txt
testdir/file8.txt
testdir/file9.txt
testdir/file/
testdir/file/heihei
sent 723 bytes received 237 bytes 384.00 bytes/sec
total size is 0 speedup is 0.00
[root@myhost ~]# ls /tmp/testdir/
file file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt
#也可以從遠程目錄同步到本地
[root@myhost ~]# cd /tmp/testdir/
[root@myhost testdir]# touch newfile.txt #遠程目錄創(chuàng)建新內(nèi)容
[root@serverb ~]# rsync -av root@192.168.2.100:/tmp/testdir/ /root/testdir/
root@192.168.2.100's password:
receiving incremental file list
./
newfile.txt
sent 47 bytes received 339 bytes 110.29 bytes/sec
total size is 0 speedup is 0.00
[root@serverb ~]# cd /root/testdir/
[root@serverb testdir]# ls
file file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt newfile.txt