LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   check that mount point is an NFS export before copying to it (https://www.linuxquestions.org/questions/linux-newbie-8/check-that-mount-point-is-an-nfs-export-before-copying-to-it-879831/)

landog 05-10-2011 09:45 AM

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

tredegar 05-10-2011 09:58 AM

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.

TobiSGD 05-10-2011 10:16 AM

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.

landog 05-12-2011 07:19 AM

thanks
 
I ended up with this:
Code:

"$(df -P /mnt/nas |tail -1 |awk '{print $NF}')" = "/mnt/nas"

mikikito 06-29-2011 04:07 AM

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

Karl Godt 06-29-2011 05:32 AM

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


mikikito 06-29-2011 05:54 AM

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 !!!

mikikito 06-29-2011 12:31 PM

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 ?

chrism01 06-29-2011 07:31 PM

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.

mikikito 06-30-2011 03:15 AM

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



All times are GMT -5. The time now is 10:59 PM.