LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-02-2009, 06:42 PM   #1
fizgig10
Member
 
Registered: Nov 2004
Location: San Diego
Distribution: Mepis w/ 2.6.9 kernel, KDE 3.3
Posts: 36

Rep: Reputation: 15
Backup script that senses portable hard drive...


I have a cron job that backs up my computer from one internal hard drive to another internal hard drive every night.

I wanted to also backup to a portable hard drive if it happens to be plugged in when that script is run.

I found a udev tutorial that makes the portable hard drive appear as /dev/500gb-USB-drive using its serial number as an identification to do it. That works great.

So, I figured if that device exists, go ahead and run the extra backup step. Here's the relevant part of the backup script:

Quote:
ls /dev/500*
if [ $? -eq 0 ]
then
mount /dev/500gb-USB-Drive
echo "********* Start USB Backup" >> rsync-log
echo "Result of ls/dev/500*" >> rsync-log
ls /dev/500* >> rsync-log
rsync -av /media/500gb /mnt/500gb-usb/ >> rsync-log
else
echo "********* USB Drive not sensed" >> rsync-log
fi
The idea is that the ls command should exit with a "1" if it doesn't see anything that starts with /dev/500. If it does see something that starts with that string then it exits with "0" and so the if statement runs (the drive is then mounted and rsynced to)

This script usually works. Every now and then (perhaps every 5th night) it doesn't though and it decides to try to mount the drive anyway (which fails) and then runs the rsync to the /mnt/500gb-usb folder. This results with the unmounted folder filling up because the root partition isn't that big.

Can anyone tell me why that if statement would still sometimes execute even if the portable drive isn't plugged in?

If not, is there a better way to do what I'm trying to do? (Only rsync if the drive is plugged in and mounted.)

In thinking this over, perhaps I can pipe the output to the mount command to grep and search for /mnt/500gb-usb. I think I'll try that next unless someone else has a better idea here.

Thanks!
 
Old 02-02-2009, 07:08 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,333

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
I suggest that you check the mount return code and not bother with the first ls command. The mount return codes are listed in:

man mount

Also at the end of the script I suggest that you umount /media/500gb.

I don't think that the second ls command serves any function. If you do need it then you should list the mount point, not the device name.

------------------
Steve Stites
 
Old 02-02-2009, 07:15 PM   #3
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
When it comes to coding in bash I'm not very promising. Still, I had a couple of ideas that might help.
Code:
if [ -e /dev/500gb-USB-drive ]; then whatever
If you are using ext2 or ext3 on the disk drive then you could search for the lost+found directory in the mount point. Otherwise you could put a file in the top of the disk's directory structure and look for that file before running rsync.
 
Old 02-02-2009, 10:47 PM   #4
fizgig10
Member
 
Registered: Nov 2004
Location: San Diego
Distribution: Mepis w/ 2.6.9 kernel, KDE 3.3
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jailbait View Post
I suggest that you check the mount return code and not bother with the first ls command. The mount return codes are listed in:

man mount

Also at the end of the script I suggest that you umount /media/500gb.

I don't think that the second ls command serves any function. If you do need it then you should list the mount point, not the device name.

------------------
Steve Stites
That makes a lot of sense. Didn't think that the mount command would return an exit code but of course it does.

My script now tries to mount the drive and looks as the exit code. If it's "0" it continues the USB backup and eventually unmounts it.

Hopefully no more misfires.
 
Old 02-02-2009, 11:43 PM   #5
fizgig10
Member
 
Registered: Nov 2004
Location: San Diego
Distribution: Mepis w/ 2.6.9 kernel, KDE 3.3
Posts: 36

Original Poster
Rep: Reputation: 15
So here's my final script:

Quote:
#!/bin/bash
echo "*********** Start Rsync Log" > rsync-log
rsync -av /media/500gb /media/500gb-backup >> rsync-log

#Try to mount the usb drive and send all errors and messages to /dev/null
#if mount passes "0" which means success, the if branch will run
if ( mount /mnt/500gb-usb > /dev/null 2> /dev/null)
then
echo "********* Start USB Backup" >> rsync-log
rsync -av /media/500gb /mnt/500gb-usb/ >> rsync-log
else
echo "********* USB Drive not sensed" >> rsync-log
fi

echo "*********** Start file space left" >> rsync-log
df -h | egrep '(sdc1|media|-usb)' >> rsync-log
mailx -s "Backup Report" myemail@gmail.com < rsync-log
rm rsync-log
umount /mnt/500gb-usb
 
  


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
need a portable linux distro to bring on portable usb hard drive... pippo17 Linux - Distributions 3 10-20-2008 06:41 AM
LXer: What's the right filesystem for your portable backup drive? LXer Syndicated Linux News 0 04-17-2008 03:10 AM
Linux on a portable hard drive Kratman Linux - Newbie 1 01-07-2008 02:28 AM
Portable hard drive linux papatrpt89 Linux - Hardware 1 12-13-2005 11:34 AM
Portable hard drive siawash Linux - Hardware 4 11-14-2004 05:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:30 AM.

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