如何按创建日期和文件大小比较不同的文件?

时间:2019-05-27 19:10:47

标签: bash database-backups

我想检查最近2天的备份文件,并比较 当前文件(今天)的大小大于另一个文件(昨天)的大小, 不同的备份文件名。

我有10台使用MySQL的服务器,每天都从数据库创建每台服务器的备份,然后发送到备份服务器。

由于某些服务器上有多个数据库,因此服务器 在同一天创建不同的备份文件,然后发送到备份服务器。一世 需要确定是否今天进行了备份以及文件(今天)是否为 比旧文件大(昨天)?

  

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

     

我希望检查:

     

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

     

确定时说0,确定

     

何时不说1错误(例如=> SW3_20190526.sql.gz)

1 个答案:

答案 0 :(得分:0)

只需将〜/ testing / *更改为path / to / your / files * 记得把*

注意: 此版本的脚本假定您在所需的日期运行它。例如(日期命令和最新文件条目匹配)

# 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

注意: 这个版本的脚本不做任何假设,只是获取目录接受的最后两个条目。这样可以模拟“今天”和“昨天”,而无需您在最后一天运行脚本。

# 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