Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
We are doing TAR backup through cron job.
We do TAR backup on our backup servers exited on the network.
At the start of script we mounted to the backup server and script perform the TAR backup. (see below)
Sometimes we have noticed that mounting is not available or due to any other reason TAR backup has been created on the /root directory of the source server.
So I have added umount at the beginning of the script.
So script is like that:
{umount /mnt/backup
mount 16.01.91.6:/backup/data /mnt/backup
cd /mnt/backup/prtlprd6
rm prtlprd6.bk*
today=$(date '+%m%d%y')
tar -cvf prtlprd6.bk_"$today".tar /opt/oracle/db
/home/oracle/TARStatus > /home/oracle/TARStatus.log
mail -s "TAR Backup for prtlprd6 has been done" Mail@us.mail. < /home/oracle/TARStatus.log}
But still some times mount is failed on two servers and TAR backup went to /root. We have 16 servers to do backup.All other 14 servers are fine.
I would like to put the logic that if mount fails it will create backup under different FS not under the /root FS.
OR do not perform backup, just send the message such as mount is not available.
Any help will be appreciated.
umount /mnt/backup
mount 16.01.91.6:/backup/data /mnt/backup
if [ -d /mnt/backup/prtlprd6 ]; then
# The preferred directory exists, so use it
cd /mnt/backup/prtlprd6
else
# Go to an alternate location to store the backup
cd /other/backup/location
if
rm prtlprd6.bk*
today=$(date '+%m%d%y')
tar -cvf prtlprd6.bk_"$today".tar /opt/oracle/db /home/oracle/TARStatus > /home/oracle/TARStatus.log
mail -s "TAR Backup for prtlprd6 has been done" Mail@us.mail. < /home/oracle/TARStatus.log
If instead of go to seperate directory, I want only message, please correct it below:
umount /mnt/backup
mount 16.01.91.6:/backup/data /mnt/backup
if [ -d /mnt/backup/prtlprd6 ]; then
# The preferred directory exists, so use it
cd /mnt/backup/prtlprd6
else
# send message
mount is not available, try agin later
if
rm prtlprd6.bk*
today=$(date '+%m%d%y')
tar -cvf prtlprd6.bk_"$today".tar /opt/oracle/db /home/oracle/TARStatus > /home/oracle/TARStatus.log
mail -s "TAR Backup for prtlprd6 has been done" Mail@us.mail. < /home/oracle/TARStatus.log
if [ -d /mnt/backup/prtlprd6 ]; then
# The preferred directory exists, so use it
cd /mnt/backup/prtlprd6
else
# send message
echo Mount is not available, try agin later | mutt -x -s "Backup Failed" your@email.address
exit 1
if
The "exit" command will terminate the script at that point with a return code of 1. You might not need this, and I only put it there out of habit. If you just said "exit", or even "exit 0" then this would generate a return code of zero which indicates "success" in most operations. However, if you're not going to test the return code anywhere, then it really doesn't matter how you exit the script.
The "echo" and "mutt" commands above the exit will send an email to you, but it relies on you having the mutt package installed. Your system may have other ways to send messages. You should use a similar command to your "mail -s" command at the end of the script if you're more familiar with that.
Error:
mount: 10.00.00.8:/bkup already mounted or /mnt/upgdp busy
mount: according to mtab, 10.00.00.8:/bkup is already mounted on /mnt/upgdp
/home/dn/scrontar.sh: line 19: syntax error: unexpected end of file
Script:
umount /mnt/backup
mount 10.00.00.8:/bkup /mnt/backup
if [ -d /mnt/upgdp/prtltst2 ]; then
# The preferred directory exists, so use it
cd /mnt/upgdp/prtltst2
else
# send message
echo Mount is not available, try agin later | mutt -x -s "Backup Failed" mail@mail.gov
exit 1
if
cd /mnt/upgdp/prtltst2
rm prtltst2.bk*
today=$(date '+%m%d%y')
tar -czvf prtltst2.bk_"$today".tar /opt/IBM/WebSphere
/home/dn/ TARStatus > /home/dn/TARStatus.log
mail -s "TAR Backup for prtltst2 has been done" mail@mail.gov < /home/dn/TARStatus.log
After the "exit 1" command the next line should be fi, not if. The backwards "if" - i.e. "fi" - is used to terminate the "if" clause that started several lines above.
Also, your script mounts the remote filesystem on /mnt/backup, but then tests for a directory called /mnt/upgdp/prtltst2. The mount should be to /mnt/upgdp or the test should check for /mnt/backup/prtltst2 (and the following "cd" as well). As it is now, you're not testing whether the mounted filesystem contains the directory you're after.
That's because your "if" statement isn't terminated. Fix that and you're finished.
Syntax for if tests:-
Code:
Example 1:-
if [ some test ]
then
do something
fi
Example 2:-
if [ some test ]
then
do something
else
do something else
fi
There is also an elif statement, but no need to worry about that now.
You can, for brevity, write the first two lines like this:-
Code:
if [ some test ]; then
But you always need to terminate with fi.
It is also a good idea to indent the blocks inside the if/then/else sections. This helps you to read and debug the script - nothing else. It's good practice to get in to.
Last edited by blacky_5251; 03-24-2010 at 04:59 PM.
umount /mnt/upgdp
mount 9.0.0.0:/bkup /mnt/upgdp
if [ -d /mnt/upgdp/dev ]; then
# The preferred directory exists, so use it
cd /mnt/upgdp/dev2
else
echo Mount is not available, try agin later | mutt -x -s "Backup Failed" Mail@mail.com
exit 1
fi
cd /mnt/upgdp/dev2
rm dev2.bk*
today=$(date '+%m%d%y')
tar -czvf dev2.bk_"$today".tar.tgz /opt/IBM/WebSphere
/home/dn/TARStatus > /home/dn/TARStatus.log
mail -s "TAR Backup for dev2 has been done" Mail@mail.com< /home/dn/TARStatus.log
I don't think it is correct, not quite. The directory being tested still isn't the directory that you "cd" into. Here is a section from your latest script:-
Code:
if [ -d /mnt/upgdp/dev ]; then
# The preferred directory exists, so use it
cd /mnt/upgdp/dev2
else
echo Mount is not available, try agin later | mutt -x -s "Backup Failed" Mail@mail.com
exit 1
fi
cd /mnt/upgdp/dev2
Either the directory inside the test "[ -d /mnt/upgdp/dev ]" should be "[ -d /mnt/upgdp/dev2 ]" OR the directory that you cd to "cd /mnt/upgdp/dev2" should be "cd /mnt/upgdp/dev". Fix one or the other, but they should be the same - shouldn't they?
Also, the "cd" statement at the end of the "if/then/else/fi" code is not needed. If the test passes, then the first "cd" will put you where you need to be.
if [ -d /mnt/upgdp/dev2 ]; then
# The preferred directory exists, so use it
cd /mnt/upgdp/dev2
else
echo Mount is not available, try agin later | mutt -x -s "Backup Failed" Mail@mail.com
exit 1
fi
cd /mnt/upgdp/dev2
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.