LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Backup Script Help..............! (http://www.linuxquestions.org/questions/programming-9/bash-backup-script-help-757432/)

Greenberet88x 09-24-2009 03:06 AM

Bash Backup Script Help..............!
 
Hi, I have a little problem that I canīt seem to find the solution,

I have a simple script that what it does is:

Stop Services
Tar some folders
Log in to an FTP
PUT the Tar Folders on the FTP server (2003)
Remove the previously created tars
Start the Services Again.

But here is the problem, if the Tars, Log in and Put work fine the services do start up ok, but if I get any problem, the script dies, and the services donīt come back up.

So How can I modify this script so that if it fails the Backup procedure, still start the services up anyway, because they are very important "MYSQL
and CVSD")

Here is a Copy of the Script

Code:

#!/bin/bash

cd /root

/etc/init.d/cvsd stop
/etc/init.d/mysql stop

tar czvf source_`date +%y%m%d.tar.gz` /home/source/*
tar czvf mysql_`date +%y%m%d.tar.gz` /var/lib/mysql/*


ftp -inv 192.168.1.2 << EOF
user user1 backup
verbose off
prompt off
binary
put source_`date +%y%m%d.tar.gz`
put mysql_`date +%y%m%d.tar.gz`


bye
EOF

rm source_`date +%y%m%d.tar.gz`
rm mysql_`date +%y%m%d.tar.gz`

/etc/init.d/mysql start
/etc/init.d/cvsd start

Any help will be really appreciated,

Many thanks in advance!...

catkin 09-24-2009 03:45 AM

I do not understand why the service start commands are not being executed. As it stands, if any of the commands fail, execution proceeds through the script executing further commands (arguably not ideal) until the service start commands are executed. Or does the script hang in one of the commands so it doesn't get to the end? Or do you kill the script before it gets to the end.

The solution to the problem is dependent on what is going wrong.

Greenberet88x 09-24-2009 04:20 AM

Quote:

Originally Posted by catkin (Post 3695240)
I do not understand why the service start commands are not being executed. As it stands, if any of the commands fail, execution proceeds through the script executing further commands (arguably not ideal) until the service start commands are executed. Or does the script hang in one of the commands so it doesn't get to the end? Or do you kill the script before it gets to the end.

The solution to the problem is dependent on what is going wrong.

Looks like the service wont start if any problem comes up, lets say, it cannot copy a file because of permissions, or because its been used, or it cannot connect to the FTP server.

Let me also point that this works fine , (when it works fine), it had been doing some backups, but when the error comes, boom no services come back.

Is there anyway to get this script get generate an error log so that I know what the problem is, what do you think it could be?

Thanks for your response!

catkin 09-24-2009 04:43 AM

Difficult to advise without seeing the errors. Of course logging (and error trapping) can be added to the script. For starters you could run it using tee to get output on screen and in log
Code:

./mybackup.sh 2>&1 | tee mybackup.log

Greenberet88x 09-24-2009 05:10 AM

Quote:

Originally Posted by catkin (Post 3695290)
Difficult to advise without seeing the errors. Of course logging (and error trapping) can be added to the script. For starters you could run it using tee to get output on screen and in log
Code:

./mybackup.sh 2>&1 | tee mybackup.log

Great, I will run that command with the backup script and see what it comes out.

thanks a lot for your fast answers.

choogendyk 09-24-2009 07:17 AM

I would also reorder your script so that the starting of services is done after the tar. This is sort of the way people do things with snapshots, and part of the point of snapshots. They reduce the necessary down time window. So, in your case, stop services, tar the directories, start the services, then proceed with copying them to the other server and so on.

The way I do backups is to stop any services that need to be stopped (not all do), take a snapshot, start them up again, then proceed with backing up the snapshot. That gives a service down time of just seconds.

You can also have a separate cron script that starts up after the backup script and runs in a loop. It would check to see if the backup script is still running, and loop if it is. If the backup script is no longer running, then it would check services and launch them if necessary. Service monitoring utilities automate that whole process in a more general sense, but what have to be made aware of your backup procedure.


All times are GMT -5. The time now is 06:47 AM.