LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-30-2009, 08:44 AM   #1
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Rep: Reputation: 15
Backup Mount USB script


Hello,

I wish to create a script that will mount a USB drive and then copy a file to it.

At the moment I backup onto a tape drive and have a script which does:

tar cvf /dev/st0 /directory

How can I do something similar but with a USB drive.

Obviously I can mount the area, do I then need to tar up the file, then move it to the relevant mount area. Is that the best process to follow?

so in psudeo:

mount /dev/sdb2 /mnt/backup

cd /

tar cvf backup.tar directory

mv tar to /mnt/backup

umount /dev/sdb2
 
Old 06-30-2009, 09:08 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,337

Rep: Reputation: 362Reputation: 362Reputation: 362Reputation: 362
Hi

Your script looks fine. I changed it a little:

mount /dev/sdb2 /mnt/backup
cd /
tar cvf /mnt/backup/backup.tar directory
umount /dev/sdb2

No need to tar first, and then move the tar file. So I changed it so that the tar file is written directly on the USB disk.
 
Old 07-01-2009, 03:34 AM   #3
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Guttorm View Post
Hi

Your script looks fine. I changed it a little:

mount /dev/sdb2 /mnt/backup
cd /
tar cvf /mnt/backup/backup.tar directory
umount /dev/sdb2

No need to tar first, and then move the tar file. So I changed it so that the tar file is written directly on the USB disk.
Thanks for the suggestion, I have made the change to my script at it works fine now.

One more question though, I catch if the tar fails by putting it in an if statement:

Code:
if [ $? = 0 ]

Then blah blah
How can I do something similar for if the USB fails to mount, so if I run the 'mount /dev/sdb2 /mnt/backup' command and it fails? Cos if it does then I want it to quit the script and send an email etc, which is what happens in the if statement if the tar fails.
 
Old 07-01-2009, 10:01 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,337

Rep: Reputation: 362Reputation: 362Reputation: 362Reputation: 362
Hi again

You mean, after tar:

Code:
if [ $? != 0 ] ; then
  echo tar failed, giving up.
  exit
fi
Then the same just right after the mount command:
Code:
if [ $? != 0 ] ; then
  echo Mounting failed, giving up.
  exit
fi
 
Old 07-01-2009, 04:59 PM   #5
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
Just as a quick suggestion. You might want to use:
"exit 1" and "exit 2"
Then, if you can fully automate it later, the return code can clue you to the error and you won't have to scan the script for what command failed.

Just a suggestion.
 
Old 07-02-2009, 03:28 AM   #6
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nuwen52 View Post
Just as a quick suggestion. You might want to use:
"exit 1" and "exit 2"
Then, if you can fully automate it later, the return code can clue you to the error and you won't have to scan the script for what command failed.

Just a suggestion.
Thank you for both suggestions, they worked great.

Thanks again.
 
Old 07-02-2009, 05:31 AM   #7
riaankdp
LQ Newbie
 
Registered: Jul 2009
Posts: 1

Rep: Reputation: 0
Good Day

I am using the same method would i be able to put something in the script file if there's any error it would send me a mail to my email adress?

Regards
Riaan
 
Old 07-02-2009, 10:29 AM   #8
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
Quote:
Originally Posted by riaankdp View Post
Good Day

I am using the same method would i be able to put something in the script file if there's any error it would send me a mail to my email adress?

Regards
Riaan
If you are using on a machine that can send e-mail directly, then you could always script mailx (or mail) to send the e-mail. If you are using a remote smtp server (through an ISP), there's a program called sendEmail that can be scripted for this purpose.
 
  


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 help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 08:29 PM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 11:13 AM
need mount script for backup purposes npereira Linux - Newbie 2 05-03-2008 10:21 PM
Bash Script to Detect USB drive mount status nutthick Programming 6 02-02-2005 09:17 AM
mount usb module then mount usb hard drive guanyu Linux - Hardware 1 10-08-2003 12:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration