JTuman
JTuman

Reputation: 76

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. Everything works great except the if loop to output the results, thanks again for the help guys code is below!

#! /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

Upvotes: 4

Views: 4663

Answers (2)

janos
janos

Reputation: 124646

To check if the file exists and larger than 51200 bytes you could rewrite your if like this:

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:

  • The find takes care two things at once: checks if file exists and size is greater than 51200.
  • We redirect stderr to /dev/null to hide the error message if the file doesn't exist.
  • If there was a file matching both conditions, then grep will match and exit with success, otherwise it will exit with failure
  • The final outcome of the grep is what decides the if condition
  • I moved the >> /var/log/backups after the closing fi, as it's equivalent this way and less duplication.

Btw if is NOT a loop, it's a conditional.

UPDATE

As @glennjackman pointed out, a better way to write the if, without grep:

if [[ $(find ehdbkp_$now -type f -size +51200c 2>/dev/null) ]]; then
...

Upvotes: 4

alvits
alvits

Reputation: 6758

Alternatively, you could use stat instead of 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. 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. The logical or condition || will get executed only when the file is missing thus the comparison will make [ 0 -gt 100 ] false.

Upvotes: 5

Related Questions