0

几个月前我创建了这个脚本,我每天都在使用它来找出哪些备份文件有超过 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

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

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

4

2 回答 2

0

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

#!/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
于 2017-03-06T19:23:02.327 回答
0

在@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
于 2017-03-07T20:17:36.143 回答