LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-10-2009, 09:12 AM   #1
fruitwerks
Member
 
Registered: Apr 2009
Posts: 74

Rep: Reputation: 15
[SOLVED] Bash - backup script - need help with my if-then-else :D


This is my first 'complex' (lol - I know it is super basic) bash script - as you can see it runs a backup. Anyone see any issues that may arise? I'm kinda worried about the nested statements, never dealt with them before.

Here is the rundown; lamzor is an XP host, it holds a sparse ext3 disk. /backup mounts the samba share, then we setup the loop and run the backup, but only if the host is up. I know I have a somewhat redundant part, but that is to help me troubleshoot if something hinky pops up. The sleep statements are in there to resolve some CIFS errors - probably not needed but they appear to help in this testing phase.

Host lamzor is not always on / fails to wake up so I have to check before we start anything. I noticed when this host is down (hibernates after 2 hours), my cacti disk graphs (for the local machine) fail due to a timeout - bad mount related issues on my part. Hopefully this upgraded script will be able to not bork cacti

Code:
#!/bin/sh

if ! ping -c 1 -w 5 lamzor &>/dev/null ; then
        logger "Host lamzor is down unable to run backup!"
        exit 1

mount /backup

if [ ! -d /backup/mounted ]; then
        logger "alpha_backup not availible but host is up! - exiting"
else
        losetup /dev/loop0 /backup/alpha_backup
        mount /dev/loop0 /mnt/backup/
        rsync / /mnt/backup/ -v -a -l --delete --exclude-from '/root/backup_exclude'
        sleep 120
        umount /dev/loop0
        sleep 60
        losetup -d /dev/loop0
        logger "alpha_backup script completed"
fi
fi

sleep 120
umount /backup
Darn! I ran this, but it doesn't get past the ping part... everything worked separately. Guess my structure is bad?

Last edited by fruitwerks; 04-10-2009 at 09:40 AM. Reason: solved
 
Old 04-10-2009, 09:20 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
You're missing [ brackets ] around that statement, first of all.. Fix that and see what happens..
 
Old 04-10-2009, 09:25 AM   #3
fruitwerks
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 15
see, I'm so stupid!
fixed to;

Code:
if [ ! ping -c 1 -w 5 lamzor &>/dev/null ]; then
but we hang at the ping part - I'm getting the feeling I don't need a nested statement since I have an exit?
 
Old 04-10-2009, 09:34 AM   #4
sleddog
Member
 
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182

Rep: Reputation: 35
Quote:
Originally Posted by fruitwerks View Post
but we hang at the ping part - I'm getting the feeling I don't need a nested statement since I have an exit?
That sounds right.

Code:
if [ ! ping -c 1 -w 5 lamzor &>/dev/null ]; then
  # exit if unreachable.
  logger "Host lamzor is down unable to run backup!"
  exit 1
fi

... otherwise continue with backup actions.
 
Old 04-10-2009, 09:39 AM   #5
fruitwerks
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 15
Thanks! I removed the nesting - appears to be working properly. I will leave the host off tonight and see what happens. If anyone has improvements on any of the methods used I would appreciate it.
 
Old 04-10-2009, 09:41 AM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
If you haven't yet, give the bash MAN page a few good reads. It is very comprehensive and shows many ways of doing lots of common things with bash.
Code:
bash# man bash
Cheers!
Sasha
 
Old 04-10-2009, 09:44 AM   #7
fruitwerks
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 15
I have looked at it many times
I don't remember anything I read. I need to do it or have pictures.. lol, that is how I remember.
 
Old 04-10-2009, 09:51 AM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by fruitwerks View Post
I have looked at it many times
I don't remember anything I read. I need to do it or have pictures.. lol, that is how I remember.
WHen I first started bash, I felt the exact same way, and it all looked pretty foreign. It helps a lot to have (two monitors ) a man page open for reference, while working on whatever it is you're working on. After a while, you'll pick up on when something isn't right.

Took me seems like forever to figure out where the trailing semi-colon went on such things as IF/THENs and FOR/DOs.. Semicolons were my nemesis :/ lol.

Something that's really handy too is to just read through other scripts on your system; the system scripts, firewall scripts, anything at all, you can learn endlessly different ways of doing stuff, and see how easy it can be sometimes to do something another way.

Best of luck,
Sasha
 
Old 04-10-2009, 04:04 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
Quote:
Took me seems like forever to figure out where the trailing semi-colon went on such things as IF/THENs and FOR/DOs
The help command can be useful for this:
Code:
~$ help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
...
Just remember that a newline replaces a semicolon.

Also, there should NOT be square brackets around the ping command:
Code:
~$ if [ ! ping -c 1 -w 5 NO-SUCH-HOST ] ; then echo fail ; fi ; echo otherwise continue
bash: [: too many arguments
otherwise continue

I don't quite see what those calls sleep are for.
 
Old 04-10-2009, 04:06 PM   #10
fruitwerks
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 15
The ping fails without the brackets on my host and the sleep calls were just for testing, they have been removed.
 
Old 04-10-2009, 07:03 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
In what way does it fail? If you ping a non-existent host does the script exit? I don't know of any shell square brackets are required around the conditional command of the if. The '[' is another name for the test command, which does not take a command line to run, so the test will always fail if you run it that way (that is, your script will never detect when lamzor is unreachable).
 
Old 04-11-2009, 11:38 AM   #12
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Is it me, or is there an else statement missing?

Code:
#!/bin/sh

if [ ! ping -c 1 -w 5 lamzor &>/dev/null ] ; then
        logger "Host lamzor is down unable to run backup!"
        exit 1

else
mount /backup

if [ ! -d /backup/mounted ]; then
        logger "alpha_backup not availible but host is up! - exiting"
else
        losetup /dev/loop0 /backup/alpha_backup
        mount /dev/loop0 /mnt/backup/
        rsync / /mnt/backup/ -v -a -l --delete --exclude-from '/root/backup_exclude'
        sleep 120
        umount /dev/loop0
        sleep 60
        losetup -d /dev/loop0
        logger "alpha_backup script completed"
fi
fi

sleep 120
umount /backup
 
Old 04-11-2009, 01:34 PM   #13
sleddog
Member
 
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182

Rep: Reputation: 35
Quote:
Originally Posted by Disillusionist View Post
Is it me, or is there an else statement missing?
It's you Think about it....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script for database backup #cousin# Programming 8 08-29-2008 06:00 AM
bash backup script kristof_v Programming 4 07-24-2007 07:33 PM
Need help with a bash script that does a backup jamtech Programming 8 06-07-2007 11:36 PM
Need help with my backup script written in bash! guy12345 Programming 14 05-25-2007 05:09 AM
Backup script in bash gauge73 Programming 13 10-17-2005 06:25 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration