[英]Backup files listed in tar archive before unpacking for simple rollback
Before unpacking a compressed tar file, what is the best way to backup the files that will be overwritten? 在解压缩压缩的tar文件之前,什么是备份将被覆盖的文件的最佳方法?
For example, given the input archive files.tar.gz
containing: 例如,给定输入的归档文件
files.tar.gz
包含:
path/to/fileA.txt
path/to/fileB.txt
path/to/fileC.txt
and currently on the filesystem we have: 当前在文件系统上,我们有:
path/to/fileA.txt
path/to/fileC.txt
Since fileA.txt and fileC.txt will be overwritten, we would like to have files-backup.tar.gz
which contains only those two original files. 由于fileA.txt和fileC.txt将被覆盖,因此我们希望使用
files-backup.tar.gz
,其中仅包含这两个原始文件。
Currently I'm doing this manually to have a crude rollback mechanism if things don't work out. 当前,如果事情无法解决,我将手动执行此操作以具有粗略的回滚机制。 With more than a handful of files in several locations this can become a pain.
如果在几个位置有几个文件,这可能会很麻烦。
Would love to script it up or learn a new hidden parameter for the tar command. 很想编写脚本或学习tar命令的新隐藏参数。
UPDATE : The best so far I've come up with is: 更新 :到目前为止,我想出的最好的方法是:
tar ztf files.tar.gz | xargs tar zcvf /tmp/file-backup.tar.gz
What you had is a good start, but one thing to be aware of is that xargs will process stdin lines in groups. 您的开始是一个好的开始,但是要知道的一件事是xargs将成组地处理stdin行。 (That's its whole purpose in life afterall: to take an unlimited number of input lines and execute a command multiple times, each time with a subset of input lines as command line arguments.) When
files.tar.gz
has a huge number of files, xargs will do tar zcvf /tmp/file-backup.tar.gz
on a subset of them at a time! (毕竟,这是其一生的全部目的:取不限数量的输入行并多次执行命令,每次以输入行的子集作为命令行参数。)当
files.tar.gz
有大量文件时,xargs将一次对其中的一个子集执行tar zcvf /tmp/file-backup.tar.gz
! The resulting /tmp/file-backup.tar.gz
will contain only the last subset of files! 生成的
/tmp/file-backup.tar.gz
将仅包含文件的最后一个子集!
I will assume that since you specified paths as path/.../...
that all paths will be in relative. 我假设由于您将路径指定为
path/.../...
,因此所有路径都是相对的。
Here's a better solution (see "notes" below on use of tar ...|while read...
): 这是一个更好的解决方案(请参阅下面的
tar ...|while read...
“注释” tar ...|while read...
):
(tar ztf files.tar.gz|while read f; do if [ -f "$f" -o -h "$f" ]; then echo "$f" ; fi; done)|tar Tcfz - /tmp/`date '+%Y%m%d_%H%M%S'`.file-backup.tar.gz
The tar
command produces a file named /tmp/yyyymmdd_hhmmss.file-backup.tar.gz
containing the list of files supplied on its stdin, which is the list of files and symbolic links in files.tar.gz
. tar
命令生成一个名为/tmp/yyyymmdd_hhmmss.file-backup.tar.gz
的文件,其中包含其stdin上提供的文件列表,这是files.tar.gz
中的文件和符号链接的files.tar.gz
。
Finally, you might want to consider a couple of alternatives that might make hosuekeeping easier: 最后,您可能需要考虑一些替代方法,这些方法可以使居家管理更容易:
/tmp/yyyymmdd_hhmmss.file-backup.tar.gz
files first: /tmp/yyyymmdd_hhmmss.file-backup.tar.gz
文件: (TAG=`date '+%Y%m%d_%H%M%S'` ; tar ztf files.tar.gz|while read f; do if [ -f "$f" -o -h "$f" ]; then mv "$f" "$f.$TAG" ; fi; done)
NOTE: the tar ztf ...|while read f
ensures that filenames containing whitespace(s) won't cause a problem. 注意:
tar ztf ...|while read f
可以确保包含空格的文件名不会引起问题。 The simpler 更简单
for f in `tar ztf files.tar.gz`; do...
won't work when there are files in files.tar.gz
with whitespace(s) in their name. 当
files.tar.gz
有文件files.tar.gz
为空格的文件时,将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.