Bash - backup script - need help with my if-then-else :D
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
[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
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.
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.
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.
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).
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.