1

I have created the following shell scrip to backup MySQL DB, zip it, and then copy it to an s3 bucket:

#vim /home/ubuntu/backup/mysqlbackup2.sh

#!/bin/bash
## Backup mysql DB, zip it and then copy it to s3 bucket

mysqldump -hhostname -uusername dbName -p'p@ssW0rd' > db.sql

if [ $? -le 1 ] 
then
    # zip the file and copy it s3 bucket
    sudo gzip -9 db.sql
    s3cmd put db.sql.gz s://mys3bucket/
else 
    echo "Fail to backup MySQL DB" >> "backup.log"
fi

It does everything fine and the backup is copied to s3 bucket. But I cannot understand the output of shell script:

enter image description here

I understand the password warning but why does it show: '[' 0 -le 1 ']'? Is there anything wrong in my if condition?

dessert
  • 40,956
Hooman Bahreini
  • 518
  • 1
  • 8
  • 24

1 Answers1

3

Your script:

mysqldump -hhostname -uusername dbName -p'p@ssW0rd' > db.sql

if [ $? -lt 1 ] #<-- if the script exit status value is less than or equal 1, which means it succeeded then execute the if block
then
    # zip the file and copy it s3 bucket
    gzip -9 db.sql #<-- create a zipped folder from the mysql backup file
    s3cmd put db.sql.gz s://mys3bucket/ #<-- Amazon http request PUT command
else 
    echo "Fail to backup MySQL DB" >> "backup.log" #<-- if the backup failed let us know by out putting this message into the backup.log file
fi

'[' 0 -le 1 ']': A zero here means the the command to backup succeeded, and reads "less than or equal to". And yes those lines with a "+" are showing you the various steps in the execution process and that is because you used the -x in the option in executing the bash script.

Also as noted by desert the sudo and the "-le" option should be removed and changed to "-lt" respectively.

George Udosen
  • 37,534