BashScripts.org
http://bashscripts.org/forum/

Need help checking each stages. If not OK quit the script
http://bashscripts.org/forum/viewtopic.php?f=8&t=1810
Page 1 of 1

Author:  Roastbeef70 [ Wed Apr 16, 2014 5:49 am ]
Post subject:  Need help checking each stages. If not OK quit the script

Hello,

With the script below, I would like to check the return code after each 5 stages and maintain like a $Continue boolean variable.

If all went well, it is safe to delete de content of the backups folder, then to delete the zip archive file.

Can someone please help me out?

I'm new to bash...

Thanks a lot.


#! /bin/sh

# -----------------------------------------------------------
# 1. Backup the SQL databases with mysqldump
# -----------------------------------------------------------

DATE=`date '+%Y.%m.%d'`
FOLDER='/home/backups'
MYSQLUSER='root'
MYSQLPASSWD='andrew4president'
DATABASES=`mysql -u $MYSQLUSER -p$MYSQLPASSWD -e 'SHOW DATABASES' | sed '1,2d'`

for i in $DATABASES; do
echo "Dumping $i... database" in $FOLDER
mysqldump -u $MYSQLUSER -p$MYSQLPASSWD $i > $FOLDER/$i.sql
done

echo "`echo $DATABASES | wc -w` Databases were backed up in $FOLDER"

# ----------------------------------------------------------
# 2. Compress the databases in one zip file
# ----------------------------------------------------------

echo 'thojul' | sudo zip -r /home/databases.zip /home/backups

# ----------------------------------------------------------
# 3. Send the zip file over FTP to a distant file server
# ----------------------------------------------------------

HOST='10.192.120.250'
USER='admin'
PASSWD='glouglou4ever'
FILE='/home/databases.zip'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE /share/AtoM/databases-$DATE.zip
quit

END_SCRIPT

# ---------------------------------------------------------
# 4. Delete the mysqldump folder content
# ---------------------------------------------------------

# remove content of folder Backups

# ---------------------------------------------------------
# 5. Delete the zip file
# ---------------------------------------------------------

# delete zip file

Author:  DarthWavy [ Thu Apr 17, 2014 5:05 am ]
Post subject:  Re: Need help checking each stages. If not OK quit the scrip

Code:
command1
continue=$? # $? is exit status of previous command, 0 is success
command2
continue=$(($continue || $?)) # ORs the previous status with the new one from command2
...
if [ $continue == 0 ]; then
  echo all commands exited normally
fi


Hope that helps.
DW

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com