Linux Bash脚本(如果文件存在且大于循环)
*注意:我编辑了这个,所以我的最终功能代码如下 好的,我正在编写一个bash脚本,将我们的mysql数据库备份到一个目录中,删除最早的备份(如果有10个),并将备份结果输出到日志中,以便在失败时进一步创建警报。除了if循环输出结果外,一切都很好,再次感谢下面的帮助代码Linux Bash脚本(如果文件存在且大于循环),linux,bash,Linux,Bash,*注意:我编辑了这个,所以我的最终功能代码如下 好的,我正在编写一个bash脚本,将我们的mysql数据库备份到一个目录中,删除最早的备份(如果有10个),并将备份结果输出到日志中,以便在失败时进一步创建警报。除了if循环输出结果外,一切都很好,再次感谢下面的帮助代码 #! /bin/bash #THis creates a variable with the date stamp to add to the filename now=$(date +"%m_%d_%y") #This move
#! /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
要检查文件是否存在且大于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
其他说明:
同时处理两件事:检查文件是否存在以及大小是否大于51200李>find
- 如果文件不存在,我们将stderr重定向到
,以隐藏错误消息/dev/null
- 如果有一个文件同时符合这两个条件,则
将匹配并成功退出,否则将失败退出grep
的最终结果决定了grep
条件if
- 我在关闭
之后移动了fi
,因为这样做相当于减少了重复>/var/log/backups
if
而不使用grep
的更好方法是:
if [[ $(find ehdbkp_$now -type f -size +51200c 2>/dev/null) ]]; then
...
要检查文件是否存在且大于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
其他说明:
同时处理两件事:检查文件是否存在以及大小是否大于51200李>find
- 如果文件不存在,我们将stderr重定向到
,以隐藏错误消息/dev/null
- 如果有一个文件同时符合这两个条件,则
将匹配并成功退出,否则将失败退出grep
的最终结果决定了grep
条件if
- 我在关闭
之后移动了fi
,因为这样做相当于减少了重复>/var/log/backups
if
而不使用grep
的更好方法是:
if [[ $(find ehdbkp_$now -type f -size +51200c 2>/dev/null) ]]; then
...
或者,您可以使用
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
选项
-c%s
告诉stat
以字节为单位返回文件大小。这将同时考虑文件的存在和大于51200的大小。当文件丢失时,stat
将出错,因此我们将错误消息重定向到/dev/null
。逻辑or条件|
将仅在文件丢失时执行,因此比较将使[0-gt 100]
为false。或者,您可以使用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
选项
-c%s
告诉stat
以字节为单位返回文件大小。这将同时考虑文件的存在和大于51200的大小。当文件丢失时,stat
将出错,因此我们将错误消息重定向到/dev/null
。逻辑or条件|
将仅在文件丢失时执行,因此比较将使[0-gt 100]
为假。您所说的“适当大小”是什么意思?我想确保DB文件处于或大于预期值,基本上,我想确保转储不会失败,它不会只是创建一个空文件,或者通过创建一个较小的文件部分失败。现在我将其设置为仅50MB,作为备用,您还可以使用pushd
和popd
而不是后两种cd
s。也就是说,pushd/opt/GroupLink/everythings\u HelpDesk/mysql/bin
。。。运行并转储。。。回声<代码>popd。与你的问题无关,只是我注意到了一点。bash中没有和;它是-a
。在任何语言中都没有“如果循环”这类东西。你说的“适当大小”是什么意思?我想确保DB文件达到或大于预期值,基本上我想确保转储不会失败,而不仅仅是创建一个空文件或通过创建一个较小的文件部分失败。现在我将其设置为仅50MB,作为备用,您还可以使用pushd
和popd
而不是后两种cd
s。也就是说,pushd/opt/GroupLink/everythings\u HelpDesk/mysql/bin
。。。运行并转储。。。回声<代码>popd
。与你的问题无关,只是我注意到了一点。bash中没有和;它是-a
。在任何语言中都没有“if循环”这样的东西。不必生成grep,您也可以说:if[$(find ehdbkp_$now-type f-size+51200c2>/dev/null)]
--如果find的输出不是空的,则成功。不必生成grep,您也可以说:如果[[$(find ehdbkp_$now-type f-size+51200c2>/dev/null)]代码>--如果find的输出不为空,则成功。