【问题标题】:Script Issues with find -> tar/gzipfind -> tar/gzip 的脚本问题
【发布时间】:2017-04-18 13:59:23
【问题描述】:

我目前正在编写一个脚本来存储/备份我们的旧文件,以便我们在服务器上有更多空间。这个脚本将被用作一个 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 处理,最后将它们制成一个 gzip,其名称为“Backup$currentDate.tar.gz”。 然后它应该再次找到文件并删除它们。

但是我确实有一些问题:

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

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

我可以稍后以某种方式使用查找输出(存储变量?),以便最后的删除真的只针对在前一步中找到的那些文件吗?因为如果第三步需要,假设一个小时,最后一步在完成后执行,它可能会删除不早于 90 天但现在的文件,因此它们永远不会备份,但是然后删除(极不可能,但并非不可能)。

如果你还有什么想知道的,欢迎询问^^

最好的问候

【问题讨论】:

  • 你有 GNU Tar 吗?它会为你完成大部分工作。 gnu.org/software/tar/manual
  • 遗憾的是我没有也不允许安装它。
  • 这是 AIX 吗?什么版本? unname -a
  • AIX 7.1(7100-04-01-1543)
  • 没有人解决这个问题吗?

标签: unix find gzip tar


【解决方案1】:

我稍微“改写”了您的原始代码。我没有 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多