[英]Bash Script if a file exists and larger than loop
*Note i edited this so my final functioning code is below *请注意,我对此进行了编辑,因此我的最终功能代码如下
Ok so I'm writing a bash script to backup our mysql database to a directory, delete the oldest backup if 10 exist, and output the results of the backup to a log so I can further create alerts if it fails. 好的,所以我正在编写一个bash脚本以将我们的mysql数据库备份到目录,如果存在10个旧备份,则删除最旧的备份,然后将备份结果输出到日志中,以便在失败时进一步创建警报。 Everything works great except the if loop to output the results, thanks again for the help guys code is below! 除了if循环输出结果外,其他所有东西都运行良好,再次感谢下面的帮助人员代码!
#! /bin/bash
#THis creates a variable with the date stamp to add to the filename
now=$(date +"%m_%d_%y")
#This moves the bash shell to the directory of the backups
cd /dbbkp/backups/
#Counts the number of files in the direstory with the *.sql extension and deletes the oldest once 10 is reached.
[[ $(ls -ltr *.sql | wc -l) -gt 10 ]] && rm $(ls -ltr *.sql | awk 'NR==1{print $NF}')
#Moves the bash shell to the mysql bin directory to run the backup script
cd /opt/GroupLink/everything_HelpDesk/mysql/bin/
#command to run and dump the mysql db to the directory
./mysqldump -u root -p dbname > /dbbkp/backups/ehdbkp_$now.sql --protocol=socket --socket=/tmp/GLmysql.sock --password=password
#Echo the results to the log file
#Change back to the directory you created the backup in
cd /dbbkp/backups/
#If loop to check if the backup is proper size and if it exists
if find ehdbkp_$now.sql -type f -size +51200c 2>/dev/null | grep -q .; then
echo "The backup has run successfully" >> /var/log/backups
else
echo "The backup was unsuccessful" >> /var/log/backups
fi
Alternatively, you could use stat
instead of find
. 另外,您可以使用stat
代替find
。
if [ $(stat -c %s ehdbkp_$now 2>/dev/null || echo 0) -gt 51200 ]; then
echo "The backup has run successfully"
else
echo "The backup was unsuccessful"
fi >> /var/log/backups
Option -c %s
tells stat
to return the size of file in bytes. 选项-c %s
告诉stat
返回字节的文件大小。 This will take care of both the presence of file and size greater than 51200. When the file is missing, stat
will err out, thus we redirect error message to /dev/null
. 这将照顾到文件的存在和大于51200的大小。当文件丢失时, stat
将出错,因此我们将错误消息重定向到/dev/null
。 The logical or condition ||
逻辑或条件||
will get executed only when the file is missing thus the comparison will make [ 0 -gt 100 ]
false. 仅当文件丢失时才会执行,因此比较将使[ 0 -gt 100 ]
假。
To check if the file exists and larger than 51200 bytes you could rewrite your if
like this: 要检查文件是否存在和大于51200个字节你可以重写你的if
是这样的:
if find ehdbkp_$now -type f -size +51200c 2>/dev/null | grep -q .; then
echo "The backup has run successfully"
else
echo "The backup has was unsuccessful"
fi >> /var/log/backups
Other notes: 其他说明:
find
takes care two things at once: checks if file exists and size is greater than 51200. find
一次要注意两件事:检查文件是否存在且大小是否大于51200。 /dev/null
to hide the error message if the file doesn't exist. 如果文件不存在,我们会将stderr重定向到/dev/null
以隐藏错误消息。 grep
will match and exit with success, otherwise it will exit with failure 如果存在同时符合这两个条件的文件,则grep
将匹配并成功退出,否则将失败退出 grep
is what decides the if
condition grep
的最终结果是决定if
条件的条件 >> /var/log/backups
after the closing fi
, as it's equivalent this way and less duplication. 我在关闭fi
后移动了>> /var/log/backups
,因为这样等效,减少了重复。 Btw if
is NOT a loop, it's a conditional. 顺便说一句, if
不是循环,那是有条件的。
UPDATE 更新
As @glennjackman pointed out, a better way to write the if
, without grep
: 正如@glennjackman所指出的,一种更好的方式来编写if
而不使用grep
:
if [[ $(find ehdbkp_$now -type f -size +51200c 2>/dev/null) ]]; then
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.