【问题标题】:Script to show how old are all the files in a directory显示目录中所有文件的历史的脚本
【发布时间】:2017-03-06 18:31:45
【问题描述】:

几个月前我创建了这个脚本,我每天都在使用它来找出哪些备份文件已经超过 3 天或更长时间。它可以帮助我快速识别创建时间为 3 天或更长时间的所有文件。

#!/bin/bash
#Backup
time=$(date +%d)        #Current time in days
a=3                     #Number of the past days
b=0                     #No need to verify it because it has a backup from today
        echo
        ls -l | tail -n +2 | while read result;
        do
        echo $result | awk -vC0='\033[0;0m' -vC1='\033[0;32m' -vC2='\033[0;31m' -vC3='\033[0;33m' \
        '{printf "%+10s %+1s %-5s %+4s %+4s %+3s %+2s %5s %-20s \n", $1,$2,$3,$4,$5,$6," " C1 $7 C0," " $8," " $9}'
        actual=$(echo $result | awk '{ print $7 }')
        partition=$(echo $result | awk '{ print $9 }')
        rest=$(($time-$actual))
if [[ $rest -le $a && $rest -ne $b ]]; then
        echo -e "\t The Backup for \e[33m$partition\e[0m was done \e[33m$rest\e[0m days ago"
fi
        done

它将在 CLI 上显示 ls -l 命令的结果,并以更易于阅读的方式对文件的年龄进行注释。例如:

-rw-r--r-- 1 root  root 98756181 Mar 7 23:59  server005.Mon.tgz
         The Backup for server005.Mon.tgz was done 3 days ago
-rw-r--r-- 1 root  root 23663925 Mar  3  18:00  server006.Fri.tgz
         The Backup for server006.Fri.tgz was done 3 days ago
-rw-r--r-- 1 root  root 23663925 Mar 3 18:00  server009.Mon.tgz
         The Backup for server009.Mon.tgz was done 3 days ago

我每天早上都使用这个脚本,它可以帮助我快速确定备份是否每隔 3 天进行一次,脚本会在每个文件中显示一条注释,通过使用当前日期(以天为单位)显示文件的年龄文件的数量减去文件的创建日期(以天为单位),然后如果结果大于数字 3,它将显示带有文件天数的注释,但问题是当真正的日期接近每月的 29 日、30 日或第一天,因为脚本显示负值。例如:

The Backup for backupserver001.Thu.tgz was done -11 days ago
The Backup for backupserver002.Wed.tgz was done -10 days ago
The Backup for backupserver003.Mon.tgz was done -21 days ago

就像我说的,这只发生在当前日期接近月底或月初时。

我不太擅长编程或数学,所以这就是我在这里寻求帮助的原因。我很确定这项任务可以以更好的方式完成,更简单,我的代码真的很乱。任何帮助将不胜感激。

【问题讨论】:

  • timeactual 都是一个月中的几天;当actual 出现在上一个 月,但不到一个完整月前,您会得到rest 的负值。

标签: bash unix backup display backup-strategies


【解决方案1】:

您正在使用一个月中的某一天来执行绝对项目差异,但正如您所见,当 this 月的当前日期小于 中的较晚日期时,这将失败上个月。您应该改为使用 UNIX 时间戳(测量自 1970 年一天以来的秒数)来计算自上次修改文件以来经过的时间。另外,我建议使用stat 而不是ls 来获取此信息。 (假定为 GNU stat;您的本地实现可能会有所不同。)

#!/bin/bash
#Backup
now=$(date +%s)        #Current time in seconds since Jan 1 1970
a=3                     #Number of the past days
b=0                     #No need to verify it because it has a backup from today
echo
for f in *; do      
  actual=$(stat -c '%Y')
  rest=$(( (now - actual) / 84600 ))
  if (( rest < a && rest != b )); then
    printf '\t The Backup for \033[33m%s\033[0m was done \033[m%d\033[0m days ago\n' "$f" "$rest"
done

【讨论】:

  • 我很久以前就尝试过使用 stat,但它让我无处可去。我收到一个我不明白的错误。你能帮我解决这个问题吗:stat:无法读取“%Y”的文件系统信息:没有这样的文件或目录。我修复了一些东西,例如 /home/admin/Documents/* 中的 f;并在完成之前添加 fi。
  • 变量“now”存储了什么?
  • 对不起,我把我的stats 弄糊涂了;我为 BSD stat 发布了一个示例。我还打算用now 替换time,但没有完全这样做。现在应该解决这两个问题。
  • 非常感谢您的帮助。我要加强我的剧本,我学到了一些东西。
【解决方案2】:

在@chepner 帮助我之后,我能够增强脚本,现在它以更好的方式执行任务。

#!/bin/bash

#Backup

time=$(date +%s)        #Current time in seconds since Jan 1 1970
a=3                     #Number of the past days

echo
    printf '\t \t \033[32mBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \033[0m \n \n'
for f in /home/admin/Documents/*;
do
  actual=$(stat -c '%Y' $f )
  normal=$(stat -c '%y' $f )
  rest=$(( (time - actual) / 84600 ))
  if (( rest > a )); then
    printf '\t Backup for \033[33m%-40s\033[0m was done \033[1;31m%d\033[0m days ago. \t Created Date: \033[0m%-30s \n' "$f" "$rest" "$normal"
 else
    printf '\t Backup for \033[33m%-40s\033[0m was done \033[1;32m%d\033[0m days ago. \t Created Date: \033[0m%-30s \n' "$f" "$rest" "$normal"
fi
done
echo

输出更有条理和清晰,更容易识别任何问题:

                 BACKUPS        BACKUPS         BACKUPS         BACKUPS         BACKUPS         BACKUPS         BACKUPS         BACKUPS

         Backup for /home/admin/Documents/backup_full.sh     was done 36 days ago.       Created Date: 2017-01-30 15:26:46.217390547 -0500
         Backup for /home/admin/Documents/backup.sh          was done 0 days ago.        Created Date: 2017-03-07 15:07:17.066182193 -0500
         Backup for /home/admin/Documents/English.xlsx       was done 36 days ago.       Created Date: 2017-01-30 15:25:37.592965894 -0500
         Backup for /home/admin/Documents/MySQL.docx         was done 36 days ago.       Created Date: 2017-01-30 15:25:37.604965969 -0500
         Backup for /home/admin/Documents/accounts.txt        was done 36 days ago.       Created Date: 2017-01-30 15:25:37.628966117 -0500
         Backup for /home/admin/Documents/SGID.docx          was done 36 days ago.       Created Date: 2017-01-30 15:25:37.604965969 -0500
         Backup for /home/admin/Documents/sticky_bit.docx    was done 36 days ago.       Created Date: 2017-01-30 15:25:37.616966043 -0500
         Backup for /home/admin/Documents/SUID.docx          was done 36 days ago.       Created Date: 2017-01-30 15:25:37.576965795 -0500
         Backup for /home/admin/Documents/test.sh            was done 1 days ago.        Created Date: 2017-03-06 12:23:46.872273977 -0500

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 2015-05-20
    • 2017-09-10
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 2021-01-06
    • 2015-03-05
    相关资源
    最近更新 更多