LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Puppy
User Name
Password
Puppy This forum is for the discussion of Puppy Linux.

Notices


Reply
  Search this Thread
Old 02-17-2016, 10:58 PM   #1
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Rep: Reputation: 169Reputation: 169
Check if drive is present


I use this script to backup files.

What can I use to check whether the drives are plugged in, so I know that the copy failed ?

I would like to use gxmessage to let me know if the drives are not plugged in or mounted.
Quote:
cd /etc
zip -u fstab_Puppy_6.3.0.zip fstab
cp -u fstab_Puppy_6.3.0.zip /mnt/sdb1/Linux_Files
cp -u fstab_Puppy_6.3.0.zip /mnt/sdc2/Linux_Files
 
Old 02-18-2016, 02:51 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Not familiar with puppy but you could achieve this with bash scripting.

Create a file ".backupdrive" in the Linux_Files folders with your drives mounted.

In a bash script you can loop through the mount points and detect if that file exists, if it does then you're good and can back up to the mount point.
Code:
#!/bin/bash

# Clear our exit value, we'll set this later if we get an issue.
EXITWITH=0

# Loop through a list of mountpoints (yes, we could make this a variable but we'll keep it simple for now)
for MOUNTPOINT in sdb1 sdc2 ; do

  # Test for the non-existence of our identifying file in the relevant mount point/folder
  if [[ ! -f mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then

    # Display an error message
    echo "/mnt/${MOUNTPOINT} Drive not mounted or wrong drive."

    # Override the EXITWITH value as we're going to exit when we get out the loop
    EXITWITH=1

  fi

done

# Now when we get here we will have set our EXITWITH to 1 if there was a problem.
# So now we just test this and if it's not equal to 0 then we exit the script.
if [ ${EXITWITH} -ne 0 ] ; then
  exit ${EXITWITH}
fi

# Regular Script Continues Here
The above will check ALL the supplied mountpoints and will report which (if any) are missing.

If you want a simpler script that will just exit with an error if any of the mountpoints are missing you can use:

Code:
#!/bin/bash

for MOUNTPOINT in sdb1 sdc2 ; do
  if [[ ! -f mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then
    echo "/mnt/${MOUNTPOINT} Drive not mounted or wrong drive."
    exit 1
  fi
done

# Regular Script Continues Here
Replace the "echo" with whatever notification mechanism you'd prefer.

Hope this gives you a starting point.

Last edited by TenTenths; 02-18-2016 at 07:29 AM. Reason: Typo in description of the simpler script, no code changes.
 
Old 02-18-2016, 07:15 AM   #3
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Thanks ten tenths.
 
Old 02-18-2016, 07:29 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
You're welcome, let me know if it works out for you.
 
Old 02-18-2016, 03:52 PM   #5
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
I used your script with this one addition.

Quote:
gxmessage -timeout 2 "DRIVE /MNT/${MOUNTPOINT} NOT MOUNTED OR WRONG DRIVE."
 
Old 02-18-2016, 04:11 PM   #6
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
This worked last night, but now it says mnt/sdb1 not mounted when it is ??

Quote:
#
for MOUNTPOINT in sdb1 sdc2 ; do
if [[ ! -f mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then
gxmessage -timeout 2 "DRIVE /MNT/${MOUNTPOINT} NOT MOUNTED OR WRONG DRIVE."
exit 1
fi
done
gxmessage -timeout 2 "Got past drive check."
 
Old 02-18-2016, 04:23 PM   #7
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,147

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
I've stopped leaving my backup drive mounted due to the encrypting ransomware. My backup script checks for known strings in a set of canary files before mounting the drive and starting the backup. It umounts the drive when done.
 
Old 02-18-2016, 10:45 PM   #8
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
What are canary files ?

What are the names of ransom ware programs ?

I have never had a problem with malware in Linux, not the case in Win XP.
 
Old 02-19-2016, 04:32 AM   #9
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by Fixit7 View Post
This worked last night, but now it says mnt/sdb1 not mounted when it is ??
check that whatever process you have didn't blank the .backupdrive file, or ensure that whatever user your script runs as has access to that file. For example, if you created that file as "root" you may want to chmod 644 it.
 
Old 02-19-2016, 06:19 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,740

Rep: Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923
Code:
if [[ ! -f mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then
I would think it should be:
Code:
if [[ ! -f /mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then
 
Old 02-19-2016, 06:22 AM   #11
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by michaelk View Post
Code:
if [[ ! -f mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then
I would think it should be:
Code:
if [[ ! -f /mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then
Yeah, you're probably right, when I was writing the script I was in a folder and faking mnt under it.
 
Old 02-19-2016, 03:53 PM   #12
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Quote:
Originally Posted by michaelk View Post
Code:
if [[ ! -f mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then
I would think it should be:
Code:
if [[ ! -f /mnt/${MOUNTPOINT}/Linux_Files/.backupdrive ]] ; then
I do not see any difference.
 
Old 02-19-2016, 03:57 PM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,740

Rep: Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923
mnt/${MOUNTPOINT}/Linux_Files/.backupdrive
vs
/mnt/${MOUNTPOINT}/Linux_Files/.backupdrive

Without the leading / mnt/${MOUNTPOINT}/Linux_Files/ becomes a relative path. If you run the script without being in /(root} directory the conditional will always be true.

Last edited by michaelk; 02-19-2016 at 04:00 PM.
 
Old 02-19-2016, 03:59 PM   #14
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Thanks.
 
Old 02-19-2016, 04:01 PM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,740

Rep: Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923
I actually meant to say you will always get the drive not mounted message.
 
  


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
[SOLVED] Bash script to check if file is present or not, check periodically every 30 mins Iyyappan Linux - Server 10 07-03-2013 05:19 AM
How to Check whether a Parent Directory is Present prravin1 Programming 4 04-17-2012 10:27 AM
Check if monitor is present KyPeN Linux - Hardware 2 07-28-2007 11:00 AM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Puppy

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