Search code examples
bashdatabase-backups

How can I compare different files by creation date and file size?


I want to check the backup files from last 2 days and compare if the size of current file (today) is greater than another (yesterday) with different backup file names.

I have 10 Server with MYSQL and everyday create each server an backup from database and send to backup server.

By some server there is more than one database, therefore server create different backup files in same day and send to backup server. i need to be sure if a backup was made today and if the file (today) is larger than old file (yesterday)?

55M 25. Mai 03:45 /home/backup/server1/SW1_20190525.sql.gz

48M 25. Mai 03:45 /home/backup/server1/SW2_20190525.sql.gz

39M 25. Mai 03:45 /home/backup/server1/SW3_20190525.sql.gz

35M 25. Mai 03:45 /home/backup/server1/SW4_20190525.sql.gz

42M 25. Mai 03:45 /home/backup/server1/SW5_20190525.sql.gz

57M 26. Mai 03:45 /home/backup/server1/SW1_20190526.sql.gz

51M 26. Mai 03:45 /home/backup/server1/SW2_20190526.sql.gz

20K 26. Mai 03:45 /home/backup/server1/SW3_20190526.sql.gz

45M 26. Mai 03:45 /home/backup/server1/SW4_20190526.sql.gz

48M 26. Mai 03:45 /home/backup/server1/SW5_20190526.sql.gz

i expect to check:

SW1_20190526.sql.gz > SW1_20190525.sql.gz

SW2_20190526.sql.gz > SW2_20190525.sql.gz

SW3_20190526.sql.gz > SW3_20190525.sql.gz

SW4_20190526.sql.gz > SW4_20190525.sql.gz

SW5_20190526.sql.gz > SW5_20190525.sql.gz

when OK say 0 OK

when not say 1 Error (for example => SW3_20190526.sql.gz)


Solution

  • Just change the ~/testing/* to path/to/your/files* remember to put the *

    NOTE: This version of the script assumes that you run it on the desired day. For example (date-command and the latest file entry match)

    # Set directory to loop through
    dir=~/testing/*
    
    # Todays date used to grep for today's file
    today=$(date +%Y%m%d)
    
    # Yesterday's date used to grep for yesterday's file
    yesterday=$(date -d "yesterday" '+%Y%m%d')
    
    # Start point to loop through each SW? files
    num=1
    
    # Start looping through directory
    for file in $dir; do
    
        # Get today's file by date
        file_td=$(ls SW[$num]* | grep $today)
    
        # Get yesterday's file by date 
        file_yd=$(ls SW[$num]* | grep $yesterday)
    
        # Get size of today's file
        size_td=$(stat -c %s $file_td)
    
        # Get size of yesterday's file
        size_yd=$(stat -c %s $file_yd)
    
        # Test if file size is equal or greater than
        if (($size_td >= $size_yd)); then
            echo "$file_td is equal or bigger that $file_yd"
        else
            echo "$file_yd is bigger $file_td"
        fi
    
        # Limit SW? to 5 so its doesn't attempt the script on none existing file
        # You change the 5 to how ever many SW's there are in the directory
        ((num++)) && [[ $num > 5 ]] && exit
    
    done
    

    NOTE: This version of the script doesn't assume anything and just grabs the last two entries the directory accepted. This simulates "today" and "yesterday" with out needing you to run the script on the last day.

    # File Directory to loop through
    dir=~/testing/*
    
    # Starting point for file names SW1 - SW?
    num=1
    
    # Begin looping through directory
    for file in $dir; do
    
        # Set the latest file in directory SW? to today
        file_td=$(ls SW[$num]* | sort -u -r | sed '1q;d')        
    
        # Set the second latest file in directory SW? to yesterday
        file_yd=$(ls SW[$num]* | sort -u -r | sed '2q;d')
    
        # Get size of today's file
        size_td=$(stat -c %s $file_td)
    
        # Get size of yesterday's file
        size_yd=$(stat -c %s $file_yd)
    
         # Test if file size is equal or greater than
        if (($size_td >= $size_yd)); then
            echo "$file_td is equal or bigger that $file_yd"
        else
            echo "$file_yd is bigger $file_td"
        fi
    
        # Limit SW? to 5 so its doesn't attempt the script on none existing files
        # You change the 5 to how ever many SW's there are in the directory 
        ((num++)) && [[ $num > 5 ]] && exit
    
    done