用于显示目录

时间:2017-03-06 18:31:46

标签: bash unix backup display backup-strategies

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

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

我对编程或数学不太满意,所以这就是我在这里寻求帮助的原因。我非常确定这项任务可以用更好的方式完成,更简单,我的代码非常混乱。任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:0)

您正在使用一个月中的某一天执行绝对项目差异,但正如您所看到的,当月的当前日期少于中的某一天时,该日期会失败以前的月。您应该使用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

答案 1 :(得分: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