LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 12-03-2009, 12:10 PM   #1
elliot01
Member
 
Registered: Jun 2009
Location: UK
Distribution: CentOS / RedHat
Posts: 89

Rep: Reputation: 16
Removing a mounted drive folder safely


Greetings,

I have a nightly scheduled script which creates a $vhostname directory under /mnt, mounts a windows box pointing to /mnt/$vhostname then rsyncs (pulls) some files/folders from the mount point.

After this, I want to unmount the drive and remove the /mnt/$vhostname directory.

Problem is that removing the /mnt/$vhostname directory sometimes ends up deleting all the files on the source!

Example of script (stripped out lots of superfluous stuff):

Code:
vHOSTNAME='testhost'
vRSYNCFLAGS='-ahvz --inplace'

/bin/mkdir -p /mnt/${vHOSTNAME}
/usr/bin/rsync ${vRSYNCFLAGS} /mnt/${vHOSTNAME}/KAVSHARE /backups3/${vHOSTNAME}/

/bin/umount -l /mnt/${vHOSTNAME}
/bin/rm -Rf /mnt/${vHOSTNAME}
It seems that my umount line isn't disconnecting the mount, leaving the following line to literally delete all directories, recursively.

Is there a way of safely just deleting the directory (ie: mount link) without linux accidentally trashing the whole source?

Thanks in advance.
 
Old 12-03-2009, 12:25 PM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
If you use rmdir instead of the brutish rm -Rf, then it'll fail if there are files in the directory. At least that way you won't wipe your source.
 
Old 12-03-2009, 02:17 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by elliot01 View Post
Problem is that removing the /mnt/$vhostname directory sometimes ends up deleting all the files on the source!
Check the return code of "umount" before trying to remove the directory:
Code:
LOGFILE="/var/log/whatever.log"
vHOSTNAME='testhost'
vRSYNCFLAGS='-ahvz --inplace'

/bin/mkdir -p /mnt/${vHOSTNAME}
/usr/bin/rsync ${vRSYNCFLAGS} /mnt/${vHOSTNAME}/KAVSHARE /backups3/${vHOSTNAME}/

/bin/umount -l /mnt/${vHOSTNAME}
RETCODE=$?
if [ RETCODE != 0 ]; then
    echo "ERROR: Could not unmount /mnt/${vHOSTNAME}: $RETCODE" >> $LOGFILE
    exit 1
fi
/bin/rm -Rf /mnt/${vHOSTNAME}
You could even put in a loop to try to unmount the share several times before giving up.
 
Old 12-04-2009, 11:48 AM   #4
elliot01
Member
 
Registered: Jun 2009
Location: UK
Distribution: CentOS / RedHat
Posts: 89

Original Poster
Rep: Reputation: 16
Awesome replies, thanks guys!
 
  


Reply



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
Things one can safely delete from /tmp folder glore2002 Slackware 24 09-17-2009 05:09 PM
How do I remove safely a desktop folder in SuSe Linux 10.1? sfzab SUSE / openSUSE 1 06-21-2007 11:13 AM
On removing safely Suse Linux 9.3 sfzab SUSE / openSUSE 4 04-16-2006 08:30 PM
Removing safely Suse Linux 9.3 sfzab SUSE / openSUSE 4 04-16-2006 06:59 PM
Safely removing ext hdd nanoprobe Linux - Hardware 1 07-24-2005 07:34 PM

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

All times are GMT -5. The time now is 03:20 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