歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

tar命令使用及tar實現全備份和增量備份

tar命令使用及tar實現全備份和增量備份   首先弄清楚何為全備、增量備份、差異備份。簡而言之,可以這樣理解: 全備:對所有的文件做一次備份。 增量備份:本次和上一次的的差異。 差異備份:在全備的基礎上做備份。 一 tar命令 [plain]  #創建測試文件夾和文件   [root@serv01 web]# cd /data   [root@serv01 data]# ls   [root@serv01 data]# mkdir /work   [root@serv01 data]# mkdir /work/upload   [root@serv01 data]# cd /work/upload/   [root@serv01 upload]# touchaa0{1,2,3,4,5}.txt   [root@serv01 upload]# ls   aa01.txt aa02.txt  aa03.txt  aa04.txt aa05.txt       #創建壓縮包   [root@serv01 data]# tar -cvf upload01.tar/work/upload/   tar: Removing leading `/' from member names   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   #查看壓縮包裡面包含的文件   [root@serv01 data]# tar -tf upload01.tar   work/upload/   work/upload/aa03.txt   work/upload/aa01.txt   work/upload/aa04.txt   work/upload/aa05.txt   work/upload/aa02.txt   #刪除掉數據,模擬數據丟失   [root@serv01 data]# rm -rf /work   #解壓   [root@serv01 data]# tar -xvf upload01.tar-C /   work/upload/   work/upload/aa03.txt   work/upload/aa01.txt   work/upload/aa04.txt   work/upload/aa05.txt   work/upload/aa02.txt   [root@serv01 data]# ls /work   upload   [root@serv01 data]# ls   upload01.tar   #壓縮時包含文件路徑   [root@serv01 data]# tar -cPvf upload02.tar/work/upload/   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   #解壓時就不需要指定文件路徑了   [root@serv01 data]# tar -xPvf upload02.tar   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   [root@serv01 data]# ls /work/   upload            #不一定需要f參數,可以使用重定向解決   [root@serv01 data]# tar -cv upload03.tar/work/upload/   tar: upload03.tar: Cannot stat: No suchfile or directory   tar: Removing leading `/' from member names   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   work/upload/00007550000000000000000000000000121766767060120445ustar  rootrootwork/upload/aa03.txt00006440000000000000000000000000121766767060133170ustar rootrootwork/upload/aa01.txt00006440000000000000000000000000121766767060133150ustar  rootrootwork/upload/aa04.txt00006440000000000000000000000000121766767060133200ustar  rootrootwork/upload/aa05.txt00006440000000000000000000000000121766767060133210ustar rootrootwork/upload/aa02.txt00006440000000000000000000000000121766767060133160ustar  rootroottar: Exiting with failurestatus due to previous errors   #指定路徑,重定向到upload03.tar   [root@serv01 data]# tar -cv /work/upload/> upload03.tar   tar: Removing leading `/' from member names   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   #刪除目錄   [root@serv01 data]# rm -rf /work/   #解壓,指定輸入源   [root@serv01 data]# tar -xv -C / <upload03.tar   work/upload/   work/upload/aa03.txt   work/upload/aa01.txt   work/upload/aa04.txt   work/upload/aa05.txt   work/upload/aa02.txt   #查看   [root@serv01 data]# ls /work/   upload   [root@serv01 data]# tar -tf upload03.tar   work/upload/   work/upload/aa03.txt   work/upload/aa01.txt   work/upload/aa04.txt   work/upload/aa05.txt   work/upload/aa02.txt   #測試路徑加的不一樣的效果       [root@serv01 data]# cd /work/   [root@serv01 work]# tar -cf upload04.tarupload/   [root@serv01 work]# tar -tf upload04.tar   upload/   upload/aa03.txt   upload/aa01.txt   upload/aa04.txt   upload/aa05.txt   upload/aa02.txt       #會解壓到根下面的upload目錄下   [root@serv01 work]# tar -xv -C / <upload04.tar   upload/   upload/aa03.txt   upload/aa01.txt   upload/aa04.txt   upload/aa05.txt   upload/aa02.txt       [root@serv01 work]# ls /upload/   aa01.txt aa02.txt  aa03.txt  aa04.txt aa05.txt   #創建aa06.txt文件   [root@serv01 upload]# touch aa06.txt   [root@serv01 upload]# cd /data   [root@serv01 data]# tar -tf upload02.tar   tar: Removing leading `/' from member names   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   #增加aa06.txt到upload02.tar中   [root@serv01 data]# tar -Pvf upload02.tar-r /work/upload/aa06.txt   /work/upload/aa06.txt   [root@serv01 data]# tar -tf upload02.tar   tar: Removing leading `/' from member names   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   /work/upload/aa06.txt       #演示文件修改後,更新壓縮包   #編輯文件   [root@serv01 data]# vim/work/upload/aa01.txt   #更新   [root@serv01 data]# tar -uPvf upload02.tar/work/upload/   /work/upload/aa01.txt   [root@serv01 data]# tar -tf upload02.tar   tar: Removing leading `/' from member names   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   /work/upload/aa06.txt   /work/upload/aa01.txt       #刪除文件,然後恢復   [root@serv01 data]# rm -rf /work/   [root@serv01 data]# ls /work/*   ls: cannot access /work/*: No such file or directory   [root@serv01 data]# tar -xPvf upload02.tar   /work/upload/   /work/upload/aa03.txt   /work/upload/aa01.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa02.txt   /work/upload/aa06.txt   /work/upload/aa01.txt   #查看剛才更新的文件,發現沒任何問題   [root@serv01 data]# cat/work/upload/aa01.txt   this is aa01.txt       #找到etc下面以conf結尾的文件,然後打包   [root@serv01 data]# find /etc/ -name"*.conf" | xargs tar -Pcvf test01.tar   #找到etc下面以conf結尾的文件,但不包含yum.conf,然後打包   [root@serv01 data]# find /etc/ -name"*.conf" ! -name "yum.conf" | xargs tar -Pcvf test02.tar   [root@serv01 data]# find /etc/ -name"*.conf" ! -name yum.conf | xargs tar -Pcvf test02.tar       #指定文件,然後備份   #將配置文件寫到file1   [root@serv01 data]# find /etc -name"*.conf" > file1   #查看文件,可以看到所有的配置文件列表都已經存在了(注意是文件路徑)   [root@serv01 data]# vim file1   #創建conf01.tar,然後T指定文件   [root@serv01 data]# tar -cPvf conf01.tar -Tfile1   [root@serv01 data]# tar -tf conf01.tar   [root@serv01 data]# ls -h conf01.tar -l   -rw-r--r—. 1 root root 250K Aug  2 18:18 conf01.tar     二 tar命令實現——全備和增量備份 [plain]  [root@serv01 data]# ls /work/upload/   aa01.txt aa02.txt  aa03.txt  aa04.txt aa05.txt  aa06.txt   [root@serv01 data]# rm -rf upload01.tar   #這是全備,無法實現增量備份功能   [root@serv01 data]# tar -cPvf upload01.tar/work/upload/   #g指定標志文件   [root@serv01 data]# tar -g flag -cPvfupload01.tar /work/upload/   tar: /work/upload: Directory is new   /work/upload/   /work/upload/aa01.txt   /work/upload/aa02.txt   /work/upload/aa03.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa06.txt   [root@serv01 data]# vim flag   #新建文件   [root@serv01 data]# touch /work/upload/aa07.txt   #更改文件   [root@serv01 data]# vim/work/upload/aa02.txt   [root@serv01 data]# tar -g flag -cPvfupload02.tar /work/upload/   /work/upload/   /work/upload/aa02.txt   /work/upload/aa07.txt   [root@serv01 data]# tar -tPf upload02.tar   /work/upload/   /work/upload/aa02.txt   /work/upload/aa07.txt       [root@serv01 data]# touch/work/upload/aa08.txt   [root@serv01 data]# rm -rf/work/upload/aa01.txt   [root@serv01 data]# ls /work/upload/   aa02.txt aa03.txt  aa04.txt  aa05.txt aa06.txt  aa07.txt  aa08.txt   [root@serv01 data]# tar -g flag -cPvfupload03.tar /work/upload/   /work/upload/   /work/upload/aa08.txt       #恢復全備,逐次恢復   #數據丟失   [root@serv01 data]# rm -rf /work/*   [root@serv01 data]# ls /work/   #先恢復全備   [root@serv01 data]# tar -g flag -xPvfupload01.tar   /work/upload/   /work/upload/aa01.txt   /work/upload/aa02.txt   /work/upload/aa03.txt   /work/upload/aa04.txt   /work/upload/aa05.txt   /work/upload/aa06.txt   #可以看到數據回來了   [root@serv01 data]# ls /work/upload/   aa01.txt aa02.txt  aa03.txt  aa04.txt aa05.txt  aa06.txt   #查看aa02.txt文件,發現內容沒有   [root@serv01 data]# cat/work/upload/aa02.txt   #恢復文件,從增量備份upload02.tar文件中來   [root@serv01 data]# tar -g flag -xPvfupload02.tar   /work/upload/   /work/upload/aa02.txt   /work/upload/aa07.txt   #查看aa02.txt文件,內容回來了   [root@serv01 data]# cat/work/upload/aa02.txt   hello world   #恢復文件,從增量備份upload03.tar文件中來   [root@serv01 data]# tar -g flag -xPvfupload03.tar   /work/upload/   tar: Deleting `/work/upload/aa01.txt'   /work/upload/aa08.txt   [root@serv01 data]# ls /work/upload/   aa02.txt aa03.txt  aa04.txt  aa05.txt aa06.txt  aa07.txt  aa08.txt    
Copyright © Linux教程網 All Rights Reserved