Index: zfs-replica
===================================================================
--- zfs-replica	(revision 66)
+++ zfs-replica	(revision 67)
@@ -17,7 +17,48 @@
 BINMBUFFER="mbuffer -4 -v 0 -q -s 128k -m 1G"
 LOCALHOST=$(hostname -s)
 
+LOGFILEROTATE=21
+LOGDIR="/var/log/zfs-replica"
+
+if [ "$2" == "--cronlog" ];
+then
+  LOG=true
+  if [ ! -d $LOGDIR ];
+  then
+    mkdir -p $LOGDIR
+  fi
+else
+  LOG=false
+fi
+
 #********************************
+#* function ok_exit
+#*******************************
+function ok_exit {
+  if $LOG; then
+    echo $1 1>&2
+    # restauration de stderr et stdout, fermeture des fd temporaires
+    exec 2>&7 7>&-
+    exec 1>&6 6>&-
+  fi
+  exit 0
+}
+
+#********************************
+#* function err_exit
+#*******************************
+function err_exit {
+  if $LOG; then
+    echo $1 1>&2
+    # restauration de stderr et stdout, fermeture des fd temporaires
+    exec 2>&7 7>&-
+    exec 1>&6 6>&-
+  fi
+  cat $LOGFILE
+  exit 1
+}
+
+#********************************
 #* function initenv
 #*******************************
 function initenv {
@@ -27,36 +68,45 @@
    exit 1
 fi
 
-if [ ! -f $1 ]
+if [ ! -s $FILE_NAME ]
 then
-   echo "/!\\ Replica Configuration file missing /!\\" 1>&2
-   exit 1
+   echo "Replica Configuration file not found"
+   exit 0
 fi
-
-if [ ! -e $FILE_NAME ]
-then
-   echo "Replica Configuration file not found" 1>&2
-   exit 1;
-fi
-echo $FILE_NAME
 lastsnapshotReplicated=$(cat $FILE_NAME|grep lastsnapshotreplicated|cut -d "=" -f2)
 server=$(cat $FILE_NAME|grep server|cut -d "=" -f2)
 nbreplica=$(cat $FILE_NAME|grep nbreplica|cut -d "=" -f2)
 namereplica=$(cat $FILE_NAME|grep namereplica|cut -d "=" -f2)
 namevolfs=$(cat $FILE_NAME|grep "name="|cut -d "=" -f2)
 
+if $LOG;
+then
+  LOGFILE="$LOGDIR/${namevolfs//\//-}.log"
+
+  if [ -f $LOGFILE ];
+  then
+    savelog -t -c $LOGFILEROTATE -q -n $LOGFILE
+  fi
+
+  # sauvegarde de stdout et stderr dans des fd (file descriptor) temporaires
+  #+redirection de stdout vers $TMPFILE
+  #+redirection de stderr vers stdout
+  #+ = ">> $TMPFILE 2>&1"
+  exec 6>&1 7>&2
+  exec 1>$LOGFILE
+  exec 2>&1
+fi
+
 if [[ -z $namereplica ]] || [[ -z $nbreplica ]] || [[ -z $namevolfs ]]
 then
-   echo "Incoherence data in $FILE_NAME" 1>&2
-   exit 1
+   err_exit "Incoherence data in $FILE_NAME"
 fi
 
 echo "$getConfigSnapshotTool $namevolfs"
 getlistSnapshot=$($getConfigSnapshotTool $namevolfs)
 if [[ "$getlistSnapshot" == "" ]]
 then
-  echo "Please, enable a snapshot configuration for $namevolfs" 1>&2
-  exit 1
+  err_exit "Please, enable a snapshot configuration for $namevolfs"
 fi
 }
 
@@ -65,12 +115,11 @@
 #***************************************
 function checkServerConnection {
 
-ssh -o BatchMode=yes root@$server ls
+ssh -o BatchMode=yes root@$server date
 
 if [ "$?" != "0" ]
 then
-  echo "SSH Connection server $server failed, please check the private/public key" 1>&2
-  exit 1
+  err_exit "SSH Connection server $server failed, please check the private/public key"
 fi
 
 }
@@ -187,7 +236,7 @@
        /bin/rm $FILE_NAME
        /bin/mv $TEMP_FILE_NAME $FILE_NAME
        del_old_snapshot
-       exit 0
+       ok_exit
     fi 
    
 else
@@ -214,7 +263,7 @@
        /bin/rm $FILE_NAME
        /bin/mv $TEMP_FILE_NAME $FILE_NAME
        del_old_snapshot
-       exit 0
+       ok_exit
     fi 
   fi
 fi
@@ -239,13 +288,13 @@
        /bin/rm $FILE_NAME
        /bin/mv $TEMP_FILE_NAME $FILE_NAME
        del_old_snapshot
-       exit 0
+       ok_exit
     fi 
 else
   checkServerConnection
 if [ "$snasphot2replicate" == "" ]
 then
-   exit 1
+   err_exit
 else
     #$BINZFS send -i $lastsnapshotReplicated $namevolfs@$snasphot2replicate | ssh -o BatchMode=yes root@$server $BINZFS recv $namereplica
     computePort
@@ -262,7 +311,7 @@
        /bin/rm $FILE_NAME
        /bin/mv $TEMP_FILE_NAME $FILE_NAME
        del_old_snapshot
-       exit 0
+       ok_exit
     fi 
   fi
 fi
@@ -332,8 +381,7 @@
  
   if [ "$snasphot2replicate" == "$lastsnapshot" ]
   then
-     echo "Nothing to replicate" 1>&2
-     exit 1
+     err_exit "Nothing to replicate"
  
   fi
   if [ "$server" == "" ]
@@ -346,14 +394,12 @@
   fi
   if [[ $check_lastsnapshotReplica -eq 0 ]]
   then
-     echo "Incoherence, incremental replication failed : $lastsnapshotReplicated is missing" 1>&2
-     exit 1
+     err_exit "Incoherence, incremental replication failed : $lastsnapshotReplicated is missing"
   fi
   
   if [[ $check_currentsnapshotReplica -eq 1 ]]
   then
-    echo "No replica to do, $lastSnapshot already exists" 1>&2
-    exit 1
+    err_exit "No replica to do, $lastSnapshot already exists"
   fi  
 fi
  echo $snasphot2replicate
@@ -362,3 +408,7 @@
 
 nointeractive
 
+# restauration de stderr et stdout, fermeture des fd temporaires
+exec 2>&7 7>&-
+exec 1>&6 6>&-
+
