0

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.

  • `time` and `actual` are both days of the month; you get a negative value for `rest` when `actual` occurs in the *previous* month, but less than one full month ago. – chepner Mar 06 '17 at 18:46

2 Answers2

0

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I tried with stat long time ago, but it got me nowhere. Im receiving an error that I dont understand. Can you help me out with this one: stat: cannot read file system information for ‘%Y’: No such file or directory. I fixed few thing such as for f in /home/admin/Documents/*; and added the fi before the done. – Hamilton Jimenez Vasquez Mar 06 '17 at 20:25
  • also what does the variable "now" stores? – Hamilton Jimenez Vasquez Mar 06 '17 at 20:35
  • Sorry, I got my `stat`s confused; I posted an example for BSD `stat`. I also meant to replace `time` with `now`, but did not do so completely. Both problems should be fixed now. – chepner Mar 06 '17 at 20:55
  • Thank you so much for the help. I was to enhance my script and I learned a few things. – Hamilton Jimenez Vasquez Mar 07 '17 at 20:10
0

After @chepner helped me, I was able to enhanced the script, now it performs the task in a much better way.

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

The output is more organize and clear, making easier to identify any issue:

                 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