LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-01-2007, 12:49 PM   #1
Zylar
LQ Newbie
 
Registered: May 2007
Distribution: CentOS/RHEL
Posts: 11

Rep: Reputation: 0
Bash scripting help needed


Greetings all.

I have a centos4.4 box with a simple samba server that I'm using for backups-to-file of 2 2003 servers. I would like to be able to plug in an external usb drive, have it copy the previous nights backup to the drive, then unmount the drive. The external drive only has the capacity to store 1 set from each server.

5-day rotation, monday-friday. All backups are stored in /backupsvr/<first 3 letters of the day of week>/svr<X>.bkf (X = 1 or 2, depending on which servers set). Usb drive mounts as /media/usbdisk.

I guess the first question is can a script be triggered by a drive being mounted?

Other than that I have psuedo-code that I've done in my head, but have had trouble implementing:

-Plug-in drive (trigger script)
-use the `date` command to determine day of week
-if $date=mon, cp (-f to overwrite? needed?) /backupsvr/fri/*.bkf /media/usbdisk/, umount drive
-if $date=tue, cp /backupsvr/mon/*.bkf /media/usbdisk/, ...etc.
</end of psuedo-code>

Unfortunately I'm swamped this week, and am being hounded about the off-site backup. I wouldn't ask unless I really needed the help, I am usually pretty good at figuring this stuff out, and I figure this might take 30 seconds for someone more familiar with bash scripting.

-Z

Edit: Ahh, a thought. If it's not possible to trigger a script by a mount, maybe just a cron job that runs every 5 minutes, 8am-5pm, to see if /media/usbdisk (or /dev/sda1) is mounted, and if so, trigger the cp script. The linux box pretty much just sits idle during working hours anyway. Arg, what happens 5 minutes into the cp though...

Last edited by Zylar; 05-01-2007 at 01:30 PM.
 
Old 05-01-2007, 02:57 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Better yet, have this script start up on boot and run all the time. A loop detects whether the drive is mounted. If it's not, sleep 300 seconds and go back to the top of the loop. If it's mounted, then do your stuff.

Then check every minute or so for the drive to become unmounted. When it is, go to the top of the loop and wait for the drive to become mounted again.

Have fun writing the script!

Last edited by wjevans_7d1@yahoo.co; 05-01-2007 at 03:00 PM.
 
Old 05-01-2007, 04:38 PM   #3
Zylar
LQ Newbie
 
Registered: May 2007
Distribution: CentOS/RHEL
Posts: 11

Original Poster
Rep: Reputation: 0
Great idea, thanks.

Using this so far:

day='date +%u' # 'date +%u' returns the number of the day of the week, mon=1, etc.
case $day in
1) cp .../fri/*.bks /<usbdisk>, umount... ;;
2) ... etc.
esac

Now just to figure out how to start it at boot, detect the mount, and go back to the top if the disk isn't mounted. I did post this in the right sub-forum, I'm a complete newbie with this stuff, haha.

And of course, I'm off the clock now, just considering this a learning experience. Wife isn't too happy, but oh well.
 
Old 05-02-2007, 05:59 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
First, your script should have something like

Code:
#/bin/sh
as the very first line, to identify that it's a shell script. It's good form, and it never hurts.

Second, try this at the command line:

Code:
cat /etc/mtab
Try it once when the volume isn't mounted and once when it is. That should help you with recognizing whether it's mounted.

Third, you have a lot of learning experiences ahead. Don't be thinking, "Wife can deal with this because it's only temporary." It's not. Wife knows this already.

So make a special effort to keep wife happy.

Just sayin'.

Last edited by wjevans_7d1@yahoo.co; 05-02-2007 at 06:02 AM.
 
Old 05-02-2007, 07:27 AM   #5
Zylar
LQ Newbie
 
Registered: May 2007
Distribution: CentOS/RHEL
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks again!

Here's what I got so far, not tested or working, partially psuedo still:

#!/bin/bash
x=1
do while $x=1
touch /media/usbdisk/exist
if $?=0 then
day=`date +%u`
case "$day" in
1)...(cp -u fri to usb, umount)
esac
fi
sleep 120
loop

Edit: Hmm, lost my formatting. Oh well. And I only spent about an hour on it last night, wife is a great woman, she understood, and I spent the rest of the night taking care of her.

Last edited by Zylar; 05-02-2007 at 08:43 AM.
 
Old 05-02-2007, 07:54 AM   #6
Zylar
LQ Newbie
 
Registered: May 2007
Distribution: CentOS/RHEL
Posts: 11

Original Poster
Rep: Reputation: 0
Hmm, slight problem.

When I plug the usbdisk into my centos4.4 workstation, it's automatically mounted. Formatted w/ vfat.

When I plug it into the other centos4.4 box, it doesn't automatically mount. Hmm...

Edit: Arg this is getting confusing.

svr fstab:/dev/sda1 /media/usbdisk vfat pamconsole,noatime,sync,exec,noauto,managed 0 0

ws fstab:/dev/sda1 /media/usbdisk vfat pamconsole,noatime,sync,exec,noauto,managed 0 0

Edit2: It never shows up in the svrs mtab. I can mount it manually, but it won't automount. Maybe it's just because of different h/w. Gah, don't have the time to mess with it. All the h/w is at the office so I can't test after hours. Oh well.

Last edited by Zylar; 05-02-2007 at 09:07 AM.
 
Old 05-02-2007, 09:20 AM   #7
Zylar
LQ Newbie
 
Registered: May 2007
Distribution: CentOS/RHEL
Posts: 11

Original Poster
Rep: Reputation: 0
Ok, has something to do with Gnome/X/?. If I drop back to the commandline on the ws, same behavior.

Edit: Ok, I'm just going to replace the 'touch' with a mount. Can still use the $? to see if it worked.

Last edited by Zylar; 05-02-2007 at 09:27 AM.
 
Old 05-02-2007, 12:25 PM   #8
Zylar
LQ Newbie
 
Registered: May 2007
Distribution: CentOS/RHEL
Posts: 11

Original Poster
Rep: Reputation: 0
Ok here it is. Probably bass-ackwards, mutilated, and ugly, but most of it seems to work. Haven't had a chance to test the 'donetoday / sleep 10h' part, but oh well. Launching it using '/etc/rc.local'. I might eventually redirect the cat/grep line to null, it outputs to the screen, but it's headless anyways.


Code:
#bin/sh
x="1"
donetoday="0"
 while [ "$x"="1" ]
 do
  if [ "$donetoday" = "1" ] ; then sleep 10h && donetoday="0" ; else
   cat /etc/fstab | grep sda1
   attached=`echo $?`
    if [ "$attached" = "0" ] ; then
     mount -t vfat /dev/sda1 /media/usbdisk
     day=`date +%u`
      case "$day" in
       1) cp -u /backupsvr/fri/*.bkf /media/usbdisk/ && umount /dev/sda1 && donetoday="1" ;;
       2) cp -u /backupsvr/mon/*.bkf /media/usbdisk/ && umount /dev/sda1 && donetoday="1" ;;
       3) cp -u /backupsvr/tue/*.bkf /media/usbdisk/ && umount /dev/sda1 && donetoday="1" ;;
       4) cp -u /backupsvr/wed/*.bkf /media/usbdisk/ && umount /dev/sda1 && donetoday="1" ;;
       5) cp -u /backupsvr/thu/*.bkf /media/usbdisk/ && umount /dev/sda1 && donetoday="1" ;;
      esac
    fi
  fi
sleep 120
done
Know what? Now that I look at my above post, I probably should of posted this in programming.
 
Old 05-03-2007, 08:32 AM   #9
Zylar
LQ Newbie
 
Registered: May 2007
Distribution: CentOS/RHEL
Posts: 11

Original Poster
Rep: Reputation: 0
Well, didn't work, and it's kind of a 'duh' moment. External drive was formatted fat32 (mount -t vfat ...), 4gb filesize limit. Now the decision of formatting it as ext3 or ntfs, but I'd like to be able to restore from the usb drive without a linux box needed. Maybe a small ntfs partition with a 'ext4win' type driver, or setting up ntfs rw on the backupsvr.
 
  


Reply

Tags
bash, script



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
Scripting Help needed - IP Enumeration AnonyMooseUK Programming 11 10-22-2006 10:17 AM
Bash Scripting newb.. Advice needed. trey85stang Linux - General 5 09-28-2006 12:05 PM
shell scripting - help needed jonhewer Linux - Newbie 54 09-09-2005 02:58 PM
bash scripting --- some help needed rajsharma Linux - Software 1 09-09-2005 02:49 AM
Scripting help needed. stonelee Linux - Software 2 09-29-2003 09:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:59 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration