查找的脚本问题 -> tar/gzip

Script Issues with find -> tar/gzip

我目前正在编写一个脚本,store/backup 我们的旧文件,以便我们的服务器上有更多 space。这个脚本将用作 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 天的文件,对它们进行 tars,最后将它们制成 gzip,名称为 "Backup$currentDate.tar.gz"。 然后它应该再次找到文件并删除它们。

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

Directory1 由多个目录组成。它确实找到了文件并创建了 gz 文件,但是虽然一些文件被正确压缩(例如 /DirName1/DirName2/DirName3/File),但其他文件直接出现在 "root" 目录中。这可能是什么问题?

有没有办法告诉脚本,如果找到文件,只创建 gz 文件?因为目前,我们得到的是 gz 文件,即使什么也找不到,导致空目录。

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

如果还有什么想知道的,尽管问^^

此致

我 "rephrased" 了解了您的原始代码。我没有 AIX 机器来测试任何东西,所以不要剪切和粘贴它。使用此代码,您应该能够解决您的问题。即:

  • 它会记录要操作的文件 ($BFILES)。
  • 此记录可用于检查空的 tar 个文件。
  • 此记录可用于查看您的查找产生 "funny" 输出的原因。发现 xargs 命中 space 字符我不会感到惊讶。
  • 这条记录可以用来准确删除存档的文件。

小时候,我曾因 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