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... |
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! |
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. |
First, your script should have something like
Code:
#/bin/sh Second, try this at the command line: Code:
cat /etc/mtab 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'. |
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. |
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. |
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. |
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 |
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.
|
All times are GMT -5. The time now is 08:35 PM. |