Linux - NewbieThis 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
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.
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!
Last edited by wjevans_7d1@yahoo.co; 05-01-2007 at 03:00 PM.
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.
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.
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.
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 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.