如何按创建日期和文件大小比较不同的文件?
How can I compare different files by creation date and file size?
我想查看最近 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
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)
只需将 ~/testing/* 更改为 path/to/your/files*
记得把 *
注意:
此版本的脚本假定您 运行 它在所需的日期。例如(date-command 和最新的文件条目匹配)
# 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
注意:
这个版本的脚本不做任何假设,只是抓取目录接受的最后两个条目。这模拟了 "today" 和 "yesterday" 而不需要你在最后一天 运行 脚本。
# 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
我想查看最近 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
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)
只需将 ~/testing/* 更改为 path/to/your/files* 记得把 *
注意: 此版本的脚本假定您 运行 它在所需的日期。例如(date-command 和最新的文件条目匹配)
# 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
注意: 这个版本的脚本不做任何假设,只是抓取目录接受的最后两个条目。这模拟了 "today" 和 "yesterday" 而不需要你在最后一天 运行 脚本。
# 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