Register
It is currently Fri Jul 25, 2014 9:57 am

Need help checking each stages. If not OK quit the script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Apr 16, 2014 5:49 am   

Joined: Mon Apr 07, 2014 5:48 am
Posts: 2
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


Top
 Profile  
 PostPosted: Thu Apr 17, 2014 5:05 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
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


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP