Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 查找时出现的脚本问题->;焦油/gzip_Unix_Find_Gzip_Tar - Fatal编程技术网

Unix 查找时出现的脚本问题->;焦油/gzip

Unix 查找时出现的脚本问题->;焦油/gzip,unix,find,gzip,tar,Unix,Find,Gzip,Tar,我目前正在编写一个脚本,用于存储/备份旧文件,以便在服务器上有更多的空间。此脚本将用作每周备份内容的cronjob。我的脚本当前如下所示: #!/bin/bash currentDate=$(date '+%Y%m%d%T' | sed -e 's/://g') find /Directory1/ -type f -mtime +90 | xargs tar cvf - | gzip > /Directory2/Backup$currentDate.tar.gz find /Direct

我目前正在编写一个脚本,用于存储/备份旧文件,以便在服务器上有更多的空间。此脚本将用作每周备份内容的cronjob。我的脚本当前如下所示:

#!/bin/bash
currentDate=$(date '+%Y%m%d%T' | sed -e 's/://g') 
find /Directory1/ -type f -mtime +90 | xargs tar cvf - | gzip > /Directory2/Backup$currentDate.tar.gz
find /Directory1/ -type f -mtime +90 -exec rm {} \;
脚本首先将当前日期+时间戳(不带“:”)保存为变量。之后,它搜索超过90天的文件,对它们进行tar处理,最后将它们生成一个名为“Backup$currentDate.tar.gz”的gzip。 然后它应该再次找到文件并删除它们

不过,我在这里确实有一些问题:

目录1由多个目录组成。它确实找到了文件并创建了gz文件,但尽管某些文件压缩正确(例如/DirName1/DirName2/DirName3/file),其他文件直接显示在“root”目录中。这里可能有什么问题

有没有办法告诉脚本,如果找到文件,只创建gz文件?因为目前,我们得到了gz文件,即使找不到任何东西,也会导致空目录

我是否可以稍后使用find输出(store变量?),以便最后的remove实际上只针对之前步骤中找到的文件?因为如果第三步需要一个小时,最后一步在完成后执行,它可能会删除文件,这些文件以前不超过90天,但现在已经存在,因此它们永远不会备份,但会被删除(非常不可能,但并非不可能)

如果你还有什么需要知道的,尽管问吧^^

致以最诚挚的问候

我已经“重新表述”了您的原始代码。我没有AIX机器来测试任何东西,所以不要剪切和粘贴它。使用此代码,您应该能够解决您的问题。也就是说:

  • 它会记录打算对哪些文件进行操作(
    $BFILES
  • 此记录可用于检查空的tar文件
  • 此记录可用于查看您的发现产生“有趣”输出的原因。当我发现xargs击中了一个空格字符时,我不会感到惊讶
  • 此记录可用于准确删除存档的文件
当我还是个孩子的时候,我和xargs发生了一次严重的事故,从那以后我就避免了。也许有一个安全的版本

#!/bin/bash

# I don't have an AIX machine to test this, so exit immediately until
# someone can proof this code.
exit 1

currentDate=$(date '+%Y%m%d%T' | sed -e 's/://g') 

BFILES=/tmp/Backup$currentDate.files

find /Directory1 -type f -mtime +90 -print > $BFILES
# Here is the time to proofread the file list, $BFILES

# The AIX page I read lists the '-L' option to take filenames from an
# input file.  I've found xargs to be sketchy unless you are very
# careful about quoting.

#tar -c -v -L $BFILES -f - | gzip -9 > /Directory2/Backup$currentDate.tar.gz

# I've found xargs to be sketchy unless you are very careful about
# quoting.  I would rather loop over the input file one well quoted
# line at a time rather than use the faster, less safe xargs.  But
# here it is.

#xargs rm < $BFILES
#/bin/bash
#我没有AIX机器来测试这一点,所以请立即退出,直到
#有人能证明这个密码。
出口1
currentDate=$(日期'+%Y%m%d%T'| sed-e's/://g')
BFILES=/tmp/Backup$currentDate.files
查找/Directory1-键入f-mtime+90-打印>$b文件
#现在是校对文件列表的时候了,$b文件
#我阅读的AIX页面列出了“-L”选项,用于从
#输入文件。我发现xargs是粗略的,除非你非常
#小心引用。
#tar-c-v-L$BFILES-f-| gzip-9>/Directory2/Backup$currentDate.tar.gz
#我发现xargs是粗略的,除非你非常小心
#引用。我宁愿在输入文件上循环一个引用得很好的文件
#一次一行,而不是使用更快、更不安全的xargs。但是
#给你。
#xargs rm<$b文件

您有GNU Tar吗?它会帮你完成大部分工作。遗憾的是,我没有,也不允许安装它。这是AIX吗?什么版本?uname-aAIX 7.1(7100-04-01-1543)是否没有解决方案?