Results 1 to 6 of 6

Thread: bash script to check drive mounted

  1. #1
    Join Date
    Aug 2008
    Beans
    10

    bash script to check drive mounted

    Hi guys,

    I have a basic bash script which I use to rsync one external drive to another external drive. I'm concerned that my script may delete everything from my destination drive if the source drive hasn't mounted properly.

    How can I include a condition to check whether my external hard-drive is mounted?

    Code:
    #!/bin/bash
    
    # List files to wake up drives
    cd /media/external
    ls
    cd /media/mirror
    ls
    
    # Synchronize external hard drive to mirror hard drive
    sudo rsync -av --progress --delete --exclude .Trash* /media/external/ /media/mirror/
    Both drives mounted (mount command):
    Code:
    /dev/sdc1 on /media/external type vfat (rw,uid=1000,gid=100,utf8,dmask=027,fmask=137)
    /dev/sdd1 on /media/mirror type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096)
    Could I use awk to grab the third column and compare that to a string or something?

    I'm running 9.04 x86 server edition.

    Thanks!!

  2. #2
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: bash script to check drive mounted

    Quote Originally Posted by bfrosty View Post
    ...How can I include a condition to check whether my external hard-drive is mounted?...

    I don't know how to do what you want with the mount command, but I found this a while back, and it worked for me:

    Code:
    if grep '/media/disk' /etc/mtab > /dev/null 2>&1 ; then 
    	echo mounted
    else 
    	echo "not mounted"
    fi
    You have to change '/media/disk' to whatever is appropriate.
    Last edited by kaibob; May 27th, 2009 at 05:46 AM.

  3. #3
    Join Date
    May 2006
    Location
    Bologna, Italy
    Beans
    32

    Re: bash script to check drive mounted

    I'd suggest
    Code:
    grep -q ...
    instead of
    Code:
    grep ... > /dev/null 2>&1
    And the salad is frightful!

  4. #4
    Join Date
    Aug 2008
    Beans
    10

    Re: bash script to check drive mounted

    Thanks a lot guys. Here's the current script, which works well:

    Code:
    #!/bin/bash
    
    # If both external drives are mounted
    if ((grep -q '/media/external' /etc/mtab) && (grep -q 'media/mirror' /etc/mtab)); then
    
        # List files to wake up drives
        cd /media/external
        ls
        cd /media/mirror
        ls
    
        # Syncronize external drive to mirror drive
        sudo rsync -av --progress --delete --exclude .Trash* /media/external/ /media/mirror/
    
    else
        exit 1
    fi

  5. #5
    Join Date
    Nov 2011
    Beans
    6

    Re: bash script to check drive mounted

    I had the same issue, so I've solved it just creating a file in the external drive and checking that is there

    [[ -f /media/external/mounted ]] && rsync -rtu --delete /media/external /media/mirror

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: bash script to check drive mounted

    Please don't bump old threads.

    Thread closed.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •