焦油的目录,但不充分绝对路径存储在存档 [英] Tar a directory, but don't store full absolute paths in the archive

查看:194
本文介绍了焦油的目录,但不充分绝对路径存储在存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个备份的shell脚本的一部分,下面的命令:

 焦油-cjf site1.bz2在/ var / WWW / SITE1 /

当我列出存档的内容,我得到:

 焦油-tf site1.bz2
无功/网络/ SITE1 / style.css文件
无功/网络/ SITE1 / index.html的
无功/网络/ SITE1 / page2.html
无功/网络/ SITE1 / page3.html
无功/网络/ SITE1 /图像/ img1.png
无功/网络/ SITE1 /图像/ img2.png
无功/网络/ SITE1 /子目录/ index.html的

不过,我想删除的部分 /无功/网络/ site1的从存档中的目录和文件名,以简化提取,避免无用的不断目录结构。从来不知道,万一我会在网页数据没有在 /无功/网络存储的地方提取备份了网站。

对于上面的例子,我想有:

 焦油-tf site1.bz2
style.css文件
的index.html
page2.html
page3.html
图片/ img1.png
图片/ img2.png
子目录/ index.html的

所以,当我解压,文件将在当前目录提取,我不需要事后移动提取文件,因此该子目录结构是preserved。

有关于焦油和backuping在计算器,并在网络上的其他地方已经有很多的问题,但大多要求放弃整个子目录结构(压扁),或者只是添加或删除初始/的名字(我不知道这是什么解压时恰好发生变化),但没有更多的。

在看了一些在这里和那里发现以及手动的解决方案,我想:

 焦油-cjf site1.bz2 -C。在/ var / WWW / SITE1 /
焦油-cjf site1.bz2 -C /在/ var / WWW / SITE1 /
焦油-cjf site1.bz2 -C在/ var / WWW / site1的/在/ var / WWW / SITE1 /
焦油-cjf site1.bz2 --strip成分= 3 /无功/网络/ SITE1 /

但他们没有工作,我想要的方式。有些一筹莫展,有些人再也不会归档子目录。

这是由一个cron推出了备份的shell脚本里面,所以我不知道还有哪个用户运行它,什么是路径和当前目录,所以总是写绝对路径所需的一切,将preFER不改变当前目录,以避免在脚本进一步打破东西(因为它不仅备份的网站,但也数据库,则发送所有到FTP等)

如何实现这一点?

让我只是误会如何-C工作的选项?


解决方案

 焦油-cjf site1.tar.bz2 -C在/ var / WWW / site1的。

在上面的例子中,焦油会变为目录 /无功/网络/ site1的在做它的事之前,因为该选项 -C的/ var / WWW / site1的给出。

男人焦油

 其他选项  -C,--directory DIR
       切换到目录DIR

I have the following command in the part of a backup shell script:

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. Never know, in case I would extract backuped websites in a place where web data weren't stored under /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.

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.)

How to achieve this?

Have I just misunderstood how the option -C works?

解决方案

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

In the above example, tar will change to directory /var/www/site1 before doing its thing because the option -C /var/www/site1 was given.

From man tar:

OTHER OPTIONS

  -C, --directory DIR
       change to directory DIR

这篇关于焦油的目录,但不充分绝对路径存储在存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆