bashunixbackupdisplaybackup-strategies

Script to show how old are all the files in a directory


I created this script few months ago, I've been using it daily to find out which backup files have more than 3 days old or more. it helps me to quickly identify all the files that have 3 days or more of creation.

#!/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

It will display on the CLI the results of ls -l command along with a comment about how old are the files in a more human readable way. For Example:

-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

I use this script daily every morning it helps me to quickly identify if the backups are being done every 3 days no less, the script displays a comment in every file showing how old the file is by using current date (in days) of the files minus the creation date of the file (in days), then if the result is greater than number 3 it will display a comment with the number of days of a file, but the thing is that I have some problems when the real date is near to 29th, 30th or First day of the month, because the scripts shows negative values. For example:

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

Like I said, this only happens when the current date is near to the end or beginning of a month.

I'm not really good with programming or math, so that's the reason I'm looking for help here. I'm pretty sure this task can be done in a much better way, simpler, my code is really messy. Any help will be really appreciate.


Solution

  • You are using the day of a month to perform absolute item differences, but as you can see, that fails when the current day of this month is less than a later day in the previous month. You should instead use a UNIX timestamp, which measures the number of seconds since a day in 1970, to compute the elapsed time since a file was last modified. Also, I recommend using stat, rather than ls, to get this information. (GNU stat assumed; your local implementation may differ.)

    #!/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