[SOLVED] check that mount point is an NFS export before copying to it
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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??)
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
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 ?
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.
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.