tar 一個目錄,但不要在存檔中存儲完整的絕對路徑 - Tar a directory, but don't store full absolute paths in the archive

問題:

I have the following command in the part of a backup shell script:我在備份 shell 腳本的一部分中有以下命令:

tar -cjf site1.bz2 /var/www/site1/

When I list the contents of the archive, I get:當我列出檔案的內容時,我得到:

tar -tf site1.bz2
var/www/site1/style.css
var/www/site1/index.html
var/www/site1/page2.html
var/www/site1/page3.html
var/www/site1/images/img1.png
var/www/site1/images/img2.png
var/www/site1/subdir/index.html

But I would like to remove the part /var/www/site1 from directory and file names within the archive, in order to simplify extraction and avoid useless constant directory structure.但是我想從存檔中的目錄和文件名中刪除部分/var/www/site1 ,以簡化提取並避免無用的恆定目錄結構。 Never know, in case I would extract backuped websites in a place where web data weren't stored under /var/www .永遠不知道,以防萬一我將備份的網站提取到 Web 數據未存儲在/var/www

For the example above, I would like to have :對於上面的例子,我想要:

tar -tf site1.bz2
style.css
index.html
page2.html
page3.html
images/img1.png
images/img2.png
subdir/index.html

So, that when I extract, files are extracted in the current directory and I don't need to move extracted files afterwards, and so that sub-directory structures is preserved.因此,當我提取文件時,文件將提取到當前目錄中,之後我不需要移動提取的文件,從而保留子目錄結構。

There are already many questions about tar and backuping in stackoverflow and at other places on the web, but most of them ask for dropping the entire sub-directory structure (flattening), or just add or remove the initial / in the names (I don't know what it changes exactly when extracting), but no more.stackoverflow和網絡上的其他地方已經有很多關於 tar 和備份的問題,但大多數都要求刪除整個子目錄結構(扁平化),或者只是添加或刪除名稱中的首字母 /(我不不知道它在提取時究竟發生了什麼變化),但僅此而已。

After having read some of the solutions found here and there as well as the manual, I tried :在閱讀了這裏和那裏的一些解決方案以及手冊後,我嘗試了:

tar -cjf site1.bz2 -C . /var/www/site1/
tar -cjf site1.bz2 -C / /var/www/site1/
tar -cjf site1.bz2 -C /var/www/site1/ /var/www/site1/
tar -cjf site1.bz2 --strip-components=3 /var/www/site1/

But none of them worked the way I want.但是他們都沒有按照我想要的方式工作。 Some do nothing, some others don't archive sub-directories anymore.有些什麼都不做,有些則不再存檔子目錄。

It's inside a backup shell script launched by a Cron, so I don't know well, which user runs it, what is the path and the current directory, so always writing absolute path is required for everything, and would prefer not changing current directory to avoid breaking something further in the script (because it doesn't only backup websites, but also databases, then send all that to FTP etc.)它在一個 Cron 啓動的備份 shell 腳本中,所以我不太清楚,哪個用戶運行它,路徑和當前目錄是什麼,所以總是寫絕對路徑是所有東西的必需品,並且寧願不改變當前目錄避免在腳本中進一步破壞某些內容(因爲它不僅備份網站,還備份數據庫,然後將所有內容發送到 FTP 等)

How to achieve this?如何實現這一目標?

Have I just misunderstood how the option -C works?我是不是誤解了選項 -C 的工作原理?


解決方案:

參考一: https://en.stackoom.com/question/1GNw3
參考二: https://stackoom.com/question/1GNw3
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章