Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Backup script to check if a path is mounted

  1. #1
    Join Date
    Jul 2009
    Location
    South Africa
    Beans
    168
    Distro
    Ubuntu

    Backup script to check if a path is mounted

    I'm writing a script to rsync some directories to external hdd for backup.

    My external hdd gets automatically mounted to /media/backup1

    My script then backs up predefined directories to /media/backup1.

    I have added this script to cron to run once every day.

    The problem is that in the case where the drive is not plugged in and the script runs, it backs up to my local hard drive, and since it is more than 70% full, it fills it up by duplicating that 70% onto itself.

    I have taken the script further, to test whether /media/backup1 is mounted. If it is, the backup will run. If it is not, it will bail out.

    I'm using the mountpoint program to test for mounts.

    My script so far:
    Code:
    #!/bin/bash
    if [[ `mountpoint /media/backup1` ]]; then
           echo "filesystem mounted"
    
    # The backup function. Commented out for testing.
    #       rsync --verbose --progress -a /var /media/backup1
    #      rsync -a /var /media/backup1/rsync
    
    else
            echo "backup1 not mounted"
    fi
    If /media/backup1 is mounted, and I run the script, it reports "filesystem mounted".

    If /media/backup1 is NOT mounted, and I run the script, it still reports "filesystem mounted".

    When /media/backup1 is mounted and I run:
    Code:
    $mountpoint /media/backup1
    /media/backup1 is a mountpoint
    $
    When /media/backup1 is NOT mounted and I run:
    Code:
    $mountpoint /media/backup1
    /media/backup1 is not a mountpoint
    $
    Clearly, "$mountpoint /media/backup1" works at the command line as expected, but when being executed in the script, it should return 1 when mounted or 0 when not, resulting in an appropriate message, but it doesn't. What am I doing wrong?

  2. #2
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Backup script to check if a path is mounted

    Hiya,

    I think it's echoing that it's mounted because of the way you set it up. It's running mountpoint successfully, instead of checking for a mountpoint and then returning the exit code depending on the result. See the modified script at the bottom of this post.

    Here's what one of my backup scripts look like (it's nasty but it works):

    Code:
    #!/bin/bash
    
    ###
    ### full.sh
    ### Script to do a full backup of RAID5 Array
    ### Created by CharlesA
    ### Tested 10/04/2010
    ### Updated 10/04/2010
    ###
    
    BIN=/bin/
    USR=/usr/bin/
    ARRAYPATH=/array/
    FULLPATH=/full/
    HOME=/home/charles/
    ERROR=${HOME}logs/full_backup_error.log
    DATE=$(${BIN}date +%D)
    LOG=$(${BIN}date +%m%d%Y)
    
    # Check if $ARRAYPATH is mounted and, if so, mount full backup drive
    if ${BIN}mountpoint -q $ARRAYPATH
    then
      { ${BIN}mount -t ext4 UUID=755a6096-908d-4ddd-b7b6-5725c43717f5 $FULLPATH; } 2> $ERROR || { echo "Mounting of backup drive failed"; exit; }
    else
      { echo "RAID Array not mounted!"; exit; }
    fi
    
    NEWFULLDIR=$FULLPATH
    FULLLOG=${FULLPATH}log/${LOG}.txt
    
    # rync
    if ${BIN}mountpoint -q $FULLPATH
    then
      { ${USR}rsync --archive --itemize-changes --delete --log-file $FULLLOG $ARRAYPATH --exclude lost+found --exclude log --exclude full.txt $NEWFULLDIR && echo "Full Backup Completed on $DATE" > ${FULLPATH}full.txt  && ${BIN}umount $FULLPATH; } 2> $ERROR || { echo "Full backup failed."; exit; }
    fi
    # eof
    I modified your script like so:

    Code:
    #!/bin/bash
    if mountpoint -q /media/backup1
      then
        echo "filesystem mounted"
    
    # The backup function. Commented out for testing.
    #       rsync --verbose --progress -a /var /media/backup1
    #      rsync -a /var /media/backup1/rsync
    
      else
        echo "backup1 not mounted"
    fi
    EDIT 2:

    Here's a script that'll work.

    Code:
    #!/bin/bash
    
    if [[ $(mountpoint -q /media) ]]
      then
        echo mounted
      else
        echo unmounted
    fi
    Last edited by CharlesA; March 6th, 2011 at 06:29 PM. Reason: added modified script and colorized changed bits.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  3. #3
    Join Date
    Jul 2009
    Location
    South Africa
    Beans
    168
    Distro
    Ubuntu

    Talking Re: Backup script to check if a path is mounted

    Awesome, that seems to be all I needed. I was convinced the if statement was where the fault was.

    After a little cleanup, this is what it looks like:

    Code:
    #!/bin/bash
    if mountpoint -q /media/backup1
      then
           echo "filesystem mounted"
           rsync -a /var /media/backup1/rsync
    
    else
           echo "filesystem not mounted"
    fi
    Two questions if I may:


    1. is my rsync statement sufficient? Should I add enything else?
    2. This is getting placed in a cron job. How do I go about sending an e-mail to root and another e-mail address to notify that the backup has been completed or failed?

    Thank you once again, the backup is now working

  4. #4
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Backup script to check if a path is mounted

    In my rsync scripts I tell it to create a log as well as itemized changes (and delete files that aren't there any longer).

    Are you just backing up the entire /var/ folder or do you want it to be an exact copy?

    In order to send an email you'd have to install a MTA (Mail Transfer Agent - either Postfix or Exim) and write the script so that it'll send an email to root or something else if the backup finishes or fails.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  5. #5
    Join Date
    Jul 2009
    Location
    South Africa
    Beans
    168
    Distro
    Ubuntu

    Smile Re: Backup script to check if a path is mounted

    Well I'm trying to do just a backup. Under /var I have folders being shared, users mail and faxes. Everything is being backed up to two hard drives (Backup1 and Backup2).

    I basically don't want the backup drives to be an EXACT copy, as I still want to give the users someplace to go to retrieve an accidentally deleted or lost file.

    As for mail, this server is an internet gateway/firewall, mailserver, and fax server, so my MTA is set up and working. I was just asking as I'm not sure how to go about implementing e-mail notifications.

  6. #6
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Backup script to check if a path is mounted

    Ah ok.

    Your rsync command looks fine then.

    The way I have mine set up is that it'll email me if the backup fails, and not do anything if the backup was successful.

    Your rsync statement would look something like this:

    Code:
    rsync -a /var /media/backup1/rsync || echo "Backup Failed for $(date)| mail -s "Backup Failed" someemail@host
    The "||" tells bash to run that command should the previous command exit with anything that is not 0.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  7. #7
    Join Date
    Jul 2009
    Location
    South Africa
    Beans
    168
    Distro
    Ubuntu

    Smile Re: Backup script to check if a path is mounted

    I wish I knew more about perl. Its such a handy tool.

    I have expanded a little on the script to allow for two hard-drives. In other words, rotational offsite backup. One drive is left on site, and another off-site. When the manager goes home, she unplugs the drive from the server and takes it home. The next mourning she brings the other drive to work.

    Code:
    #!/bin/bash
    #               Backup Drive 1
    
    if mountpoint -q /media/backup1
      then
        echo "Backup Drive #1 is mounted."
           rsync -verbose -a /var /media/backup1/rsync
           rsync -verbose -a /home /media/backup1/rsync
    
    else
            echo "Backup Drive #1 not mounted."
    fi
    
    #               Backup Drive 2
    
    if mountpoint -q /media/backup2
      then
        echo "Backup Drive #2 is mounted."
           rsync -verbose -a /var /media/backup2/rsync
           rsync -verbose -a /home /media/backup2/rsync
    
    else
            echo "Backup Drive #2 not mounted."
    fi
    I will use your solution, but I would need to add additional functionality.

    I would like to be notified in detail about the process, as the technical contact i need to keep an eye on things to make sure its working as needed. I suppose I can just have cron e-mail its output to me. Even better, would be to log everything to a file, and have either cron or the script e-mail it to me as an attachment.

    Also, I need to send an e-mail to the manager, so that she will know that todays backup has completed, and to which drive. I assume I can just add something like
    Code:
    mail -s "Backup to Backup Drive 1 Completed" someemail@host
    inside the if statement of the first drive and another similar one to the if statement of the second?


    Your help is greatly appreciated. I'm sure other people are finding this useful as well.
    Last edited by Demented ZA; March 9th, 2011 at 05:41 PM. Reason: Cleanup

  8. #8
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Backup script to check if a path is mounted

    Well you can use the --log-file switch to tell it where to put the log file.

    You can cat it and then pipe it to mail if you desire. I have no experience attaching files from the command line, so I do it that way.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  9. #9
    Join Date
    Jul 2009
    Location
    South Africa
    Beans
    168
    Distro
    Ubuntu

    Smile Re: Backup script to check if a path is mounted

    I found sending attachments from terminal to be easy using mutt:

    Code:
    mutt -s "Subject" -a test.txt -- name@domain.com
    I'm going to try output the result for the first two lines of rsync to a file called backup1.txt, and then the second two rsync lines to a file called backup2.txt. Then, directly after I call the second rsync in each statement, I'll invoke mutt to send me a mail, and add the attached files.

    I'll post my script here afterwards

    Thank you for your help

  10. #10
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Backup script to check if a path is mounted

    Oh cool. I haven't really messed with mutt all that much. Thanks.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

Page 1 of 3 123 LastLast

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
  •