LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-10-2011, 09:45 AM   #1
landog
LQ Newbie
 
Registered: Jan 2006
Posts: 23

Rep: Reputation: 0
check that mount point is an NFS export before copying to it


I am copying some backup files to a NAS by connecting an NFS export on the NAS to a mount point on my linus box. I then copy the files to it with a cron job that runs nightly.

I have mounted the NAS to /mnt/nas.

How can I test that the mount point is active before I copy to it? I wouldn't want to copy to /mnt/nas unless it was actually connected to the NAS. (I presume that would copy to the root partition??)

Thanks,
-dog
 
Old 05-10-2011, 09:58 AM   #2
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
You could put a file on the NAS called nas_is_mounted
Then test to see if it exists at your mountpoint. If it doesn't then the NAS is not mounted.

Otherwise, you could check the output of mount with and without the NAS being mounted and spot the difference.
 
Old 05-10-2011, 10:16 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You can test that easily with a combination of mount and grep. The code
Code:
mount | grep /mnt/nas
will return exitcode 0 if the NAS is mounted and 1 if it is not mounted.
 
1 members found this post helpful.
Old 05-12-2011, 07:19 AM   #4
landog
LQ Newbie
 
Registered: Jan 2006
Posts: 23

Original Poster
Rep: Reputation: 0
thanks

I ended up with this:
Code:
"$(df -P /mnt/nas |tail -1 |awk '{print $NF}')" = "/mnt/nas"
 
Old 06-29-2011, 04:07 AM   #5
mikikito
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
thanks

Hi,

I'm trying to do exactly the same thing : copying some files from a debian to a nas synology mounted as nfs to /mnt/nas.

I then copy the files to it with a cron job that runs nightly a script.

in the /etc/fstab :
Code:
192.168.1.102:/volume1/test  /mnt/nas/  nfs  user,noauto 0  0
And i don't find how to test if the nas is always mounted and online. because :

Quote:
I wouldn't want to copy to /mnt/nas unless it was actually connected to the NAS. (I presume that would copy to the root partition??)
this is exactly what happened and make the debian box crasy (partition root full at 100%!!!)


Could you explain me more about and how to use it :
Code:
"$(df -P /mnt/nas |tail -1 |awk '{print $NF}')" = "/mnt/nas"
thanks

Last edited by mikikito; 06-29-2011 at 04:16 AM.
 
Old 06-29-2011, 05:32 AM   #6
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Rep: Reputation: 45
Code:
if [ "`mount | tr -s ' ' | cut -f 3 -d ' ' | grep '^/mnt/nas'`" != "" ] ; then
do_rsync_func
else
echo 'NAS not mounted !'
echo 'Exiting script '"$0"' now !'
echo 'Bye'
fi
 
Old 06-29-2011, 05:54 AM   #7
mikikito
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
Thanks for your answer

In first it seemes to work but if i turn off the nas and execute the script again :
Code:
building file list ... done
^Crsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(543) [sender=3.0.7]
This is my script :
Code:
# Script to backup personal files to the external NAS drive as NFS.
# Specify the mount point here (DO NOT end mount_point with a forward-slash).
mount_point='/mnt/test'
tobackup='/home/scripts/'

echo "#####"
echo ""
# Test if the NAS is mounted and online
if [ "`mount | tr -s ' ' | cut -f 3 -d ' ' | grep '^$mount_point'`" != "" ] ; then
# Do the backup
rsync -var --stats --delete-after $tobackup $mount_point/
else
# Check whether target volume is mounted, and mount it if not.
  if ! mountpoint -q ${mount_point}/; then
  	echo "Mounting the external USB drive."
  	echo "Mountpoint is ${mount_point}"
  	if ! mount ${mount_point}; then
  		echo "An error code was returned by mount command!"
  		exit 5
  	else echo "Mounted successfully.";
  	fi
  else echo "${mount_point} is already mounted.";
  fi
echo 'NAS not mounted !'
echo 'Exiting script '"$0"' now !'
echo 'Bye'
fi
Sorry i'm newbie with linux !!!
 
Old 06-29-2011, 12:31 PM   #8
mikikito
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
Wink finally

Finally i made another script that seems to be ok if i turn off the nas server or turn off his ethernet cable

If someone nice could read it and tell me my error it will be wonderful

This is the script (my first one) to make a synchronisation each day of different directories to a NAS server :

Code:
#!/bin/bash
### sauvegarde différentielle journalière
### ATTENTION : placer un fichier ".test" a la racine du montage NFS
### In /etc/fstab : 192.168.1.102:/volume1/test  /mnt/test/  nfs  user,noauto 0  0
###CRON : 0 5 * * * /home/scripts/backup.bash > /home/scripts/logs/backup.log 2>&1            pour lancer le script à 05h00 tous les jours.


### Variables
IPNAS="192.168.1.102" # IP server NAS
DIRNAS=/volume1/test  # REPERTOIRE PARTAGE DU SERVEUR NAS
DIR2NAS="/mnt/test"   # répertoire local vers lequel sera monté le partage réseau
#RSYNC_OPTIONS="-acRz --delete-during --delete-excluded --ignore-errors --force --numeric-ids --partial --stats --timeout=900"
#RSYNC_OPTIONS="-rltgoDvh --ignore-errors --force --stats --progress"
RSYNC_OPTIONS="-var --stats --delete-after"  

# Directories to backup
DIR2BCKP="()"        # ne pas modifier !
DIR2BCKP[0]="/home/prive/"
DIR2BCKP[1]="/home/public/"
# Directories to create for backup 
DIR2CREATE="()"        # ne pas modifier !
DIR2CREATE[0]="prive"
DIR2CREATE[1]="public"
# Exclusions
EXCLUDE="()"  # ne pas modifier !
EXCLUDE[0]="exclude_prive.txt"  #liste des fichiers et repertoires a exclure
EXCLUDE[1]=""
## Temps d'execution du script
START=$(date +%s)

## Journalisation
LOG="/home/scripts/logs/execution_$(date +%A)-result.log"
LOG_RSYNC="/home/scripts/logs/rsync_$(date +%A)-result.log"
#rm /home/scripts/logs/rsync_$(date +%A)-result.log


### system commands used by this script
declare -i count=${#DIR2BCKP[@]}
declare -i i=0
# Temps d'execution du script
START=$(date +%s) 
# internationalisation
. /etc/default/locale
export LANG

## Debut du script pour les logs
echo "##### Debut de la copie en date du $(date) #####" >> $LOG
 
if [ "$(ping -c 3 $IPNAS | grep '0 received')" ]
   then
     echo "Server NAS not online !" >> $LOG
     exit 1
   else
 
        if ! df | grep -q '$IPNAS:$DIRNAS'
        then
                mount -t nfs $IPNAS:$DIRNAS $DIR2NAS
		            echo "Server NAS mounted successfully." >> $LOG
        fi
fi
 
if [ -f $DIR2NAS/.test ]; then

  while [ "$i" -lt "$count" ]; do
        # If not exists backup directories create them
        [ -d "$DIR2NAS/${DIR2CREATE[$i]}" ] && mkdir "$DIR2NAS/${DIR2CREATE[$i]}"
        # do the backup
	      RSYNC_RES=$(rsync $RSYNC_OPTIONS "${DIR2BCKP[$i]}" --log-file=$LOG_RSYNC --exclude-from "${EXCLUDE[$i]}" "$DIR2NAS/$(date +%A)/${DIR2CREATE[$i]}")
        echo $RSYNC_RES >> $LOG	
        i=$(($i+1))
  done 	
  ## Afficher le temps d'execution             
  STOP=$(date +%s)                             
  RUNTIME=`expr $STOP - $START`
  ## envoi du mail (a faire!!!! je sais pas comment)
  #mailx vmail < $LOG ;
  
  if ! umount ${DIR2NAS}; then
		echo "An error code was returned by umount command !" >> $LOG
		exit 5
	else echo "Server NAS dismounted successfully." >> $LOG
	fi
else
  echo "Server NAS not mounted !"  >> $LOG
fi

## Fin du script de log
## test si tout ok !!!! pas sur
if [ $? -eq 0 ]
 then {
	ETAT=Parfait
 }
 else {
	ETAT=Erreur
 }
fi
echo "=> Resultat : "$ETAT >> $LOG 
printf "Temps d'execution : %02d:%02d:%02d\n" $((RUNTIME/3600)) $((RUNTIME/60%60)) $((RUNTIME%60)) >> $LOG 
echo "##### Fin de la copie en date du $(date +"%d-%m-%Y a %H:%M") #####" >> $LOG 

exit
If i run this script on shell, all is ok

But if I cron it, i have the error : rsync: failed to open exclude file exclude_prive.txt: No such file or directory (2) and i don't understannd why ?

Last edited by mikikito; 06-29-2011 at 12:33 PM.
 
Old 06-29-2011, 07:31 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
cron doesn't login as your user, therefore exclude_prive.txt is not in your pwd (ie current dir).
For cron jobs you need to ensure all cmds and files have the complete pathname attached, OR at the start, cd into the relevant dir and source whatever setup cmds you need.
Another thing; you attempt to mount the NAS and echo "Server NAS mounted successfully." WITHOUT checking to see if it has...
Either check $? immediately, or grep the entry from /etc/mnttab.

Incidentally, if you add
Code:
set -xv
at the top of your code, you'll see exactly what it's doing if you re-direct std, stderr to a log file.

Last edited by chrism01; 06-29-2011 at 07:32 PM.
 
Old 06-30-2011, 03:15 AM   #10
mikikito
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
thanks chrism01

So i write the entire path to exclude_file.

and test the result of mount

The script now :
Code:
#!/bin/bash
### sauvegarde différentielle journalière
### ATTENTION : placer un fichier ".test" a la racine du montage NFS
### In /etc/fstab : 192.168.1.102:/volume1/test  /mnt/test/  nfs  user,noauto 0  0
###CRON : 0 5 * * 1-5 /home/scripts/backup.bash > /home/scripts/logs/backup.log 2>&1            pour lancer le script à 05h00 du lundi au vendredi.

# internationalisation
. /etc/default/locale
export LANG

### Variables
IPNAS="192.168.1.102" # IP server NAS
DIRNAS=/volume1/test  # REPERTOIRE PARTAGE DU SERVEUR NAS
DIR2NAS="/mnt/test"   # répertoire local vers lequel sera monté le partage réseau
#RSYNC_OPTIONS="-acRz --delete-during --delete-excluded --ignore-errors --force --numeric-ids --partial --stats --timeout=900"
#RSYNC_OPTIONS="-rltgoDvh --ignore-errors --force --stats --progress"
RSYNC_OPTIONS="-var --stats --delete-after"  

# Directories to backup
DIR2BCKP="()"        # ne pas modifier !
DIR2BCKP[0]="/home/prive/"
DIR2BCKP[1]="/home/public/"
# Directories to create for backup 
DIR2CREATE="()"        # ne pas modifier !
DIR2CREATE[0]="prive"
DIR2CREATE[1]="public"
# Exclusions
EXCLUDE="()"  # ne pas modifier !
EXCLUDE[0]="/home/scripts/exclude_prive.txt"  #liste des fichiers et repertoires a exclure
EXCLUDE[1]=""
## Temps d'execution du script
START=$(date +%s)

## Journalisation
LOG="/home/scripts/logs/execution_$(date +%A)-result.log"
LOG_RSYNC="/home/scripts/logs/rsync_$(date +%A)-result.log"
#rm /home/scripts/logs/rsync_$(date +%A)-result.log


### system commands used by this script
declare -i count=${#DIR2BCKP[@]}
declare -i i=0
# Temps d'execution du script
START=$(date +%s) 

## Debut du script pour les logs
echo "##### Debut de la copie en date du $(date) #####" >> $LOG
 
if [ "$(ping -c 3 $IPNAS | grep '0 received')" ]
   then
     echo "Server NAS not online !" >> $LOG
     exit 1
   else
        if ! df | grep -q '$IPNAS:$DIRNAS'
        then
          mount -t nfs $IPNAS:$DIRNAS $DIR2NAS
          if [ $? -eq 0 ]
          then {
          echo "Server NAS mounted successfully." >> $LOG
          }
          else {
          echo "Server NAS not mounted successfully !" >> $LOG
          exit 1
          }
          fi
        fi
fi
 
if [ -f $DIR2NAS/.test ]; then
  # If not exists backup directories create them
  [ -d "$DIR2NAS/$(date +%A)" ] || mkdir "$DIR2NAS/$(date +%A)"	

  while [ "$i" -lt "$count" ]; do	 
    #[ -d "$DIR2NAS/${DIR2CREATE[$i]}" ] && mkdir "$DIR2NAS/${DIR2CREATE[$i]}"
    # do the backup
    RSYNC_RES=$(rsync $RSYNC_OPTIONS "${DIR2BCKP[$i]}" --log-file=$LOG_RSYNC --exclude-from=${EXCLUDE[$i]} "$DIR2NAS/$(date +%A)/${DIR2CREATE[$i]}")
    echo $RSYNC_RES >> $LOG	
    i=$(($i+1))
  done 	
  
  ## Afficher le temps d'execution             
  STOP=$(date +%s)                             
  RUNTIME=`expr $STOP - $START`
  ## envoi du mail (a faire!!!! je sais pas comment)
  #mailx vmail < $LOG ;
  if ! umount ${DIR2NAS}; then
		echo "An error code was returned by umount command !" >> $LOG
		exit 5
	else echo "Server NAS dismounted successfully." >> $LOG
	fi
else
  echo "Server NAS not mounted !"  >> $LOG
fi

## Fin du script de log
printf "Temps d'execution : %02d:%02d:%02d\n" $((RUNTIME/3600)) $((RUNTIME/60%60)) $((RUNTIME%60)) >> $LOG 
echo "##### Fin de la copie en date du $(date +"%d-%m-%Y a %H:%M") #####" >> $LOG 

exit
 
  


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
nfs export and loop mount problem thomasd Linux - Networking 9 06-01-2010 01:47 PM
mount NFS to mount point then share out the mount point ionic_slim Linux - Networking 2 04-20-2008 12:17 PM
NFS:How can I export a directory which is mount from other's walkinmud Linux - Networking 2 05-25-2006 04:23 AM
smb mount -> nfs export wie20345 Linux - Networking 3 02-12-2005 02:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:27 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
Open Source Consulting | Domain Registration