Index: zfs-replica
===================================================================
--- zfs-replica	(revision 61)
+++ zfs-replica	(revision 62)
@@ -10,6 +10,8 @@
 getConfigSnapshotTool="/sbin/get-conf-snapshot"
 zfsSnapshot="/sbin/zfs-snapshot"
 FILE_NAME=$1
+TEMP_FILE_NAME="$1-tmp"
+>$TEMP_FILE_NAME
 BINZFS="/sbin/zfs"
 BINZPOOL="/sbin/zpool"
 
@@ -160,19 +162,14 @@
     $BINZFS send $snapshot2replicate | $BINZFS recv $namereplica
     if [[ "$?" == "0" ]]
     then
-       cpt=0
        while read line
        do 
-         if [ $cpt -eq 0 ]
-         then
-            echo $line
-            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >$FILE_NAME)
-            cpt=1
-         else
-            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$FILE_NAME)
-         fi 
+            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$TEMP_FILE_NAME)
        done <<< "$(cat $FILE_NAME)"
+       /bin/rm $FILE_NAME
+       /bin/mv $TEMP_FILE_NAME $FILE_NAME
        del_old_snapshot
+       exit 0
     fi 
    
 else
@@ -188,21 +185,16 @@
     $BINZFS send $snapshot2replicate |ssh -o BatchMode=yes root@$server $BINZFS recv $namereplica
     if [[ "$?" == "0" ]]
     then
-       cpt=0
        while read line
        do 
-         if [ $cpt -eq 0 ]
-         then
-            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >$FILE_NAME
-            cpt=1
-         else
-            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$FILE_NAME
-         fi
+            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$TEMP_FILE_NAME)
        done <<< "$(cat $FILE_NAME)"
+       /bin/rm $FILE_NAME
+       /bin/mv $TEMP_FILE_NAME $FILE_NAME
        del_old_snapshot
+       exit 0
     fi 
   fi
-exit 0
 fi
 
 }
@@ -211,49 +203,40 @@
 
 function incremental_replication {
 
-echo "Start incremental replication"
+echo "Start incremental replication for : $snasphot2replicate"
 if [[ "$server" == "" ]]
 then
-    $BINZFS send -i $lastsnapshotReplicated $namevolfs@$snaphot2replicate | $BINZFS recv $namereplica
+    $BINZFS send -i $lastsnapshotReplicated $namevolfs@$snasphot2replicate | $BINZFS recv $namereplica
+    snapshot2replicate="$namevolfs@$snasphot2replicate"
     if [[ "$?" == "0" ]]
     then
-       cpt=0
        while read line
-       do 
-         echo $line
-         if [ $cpt -eq 0 ]
-         then
-            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >$FILE_NAME
-            cpt=1
-         else
-            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >$FILE_NAME
-         fi
+       do
+            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g"
        done <<< "$(cat $FILE_NAME)"
+       /bin/rm $FILE_NAME
+       /bin/mv $TEMP_FILE_NAME $FILE_NAME
        del_old_snapshot
+       exit 0
     fi 
-   
 else
   checkServerConnection
-if [ "$snaphot2replicate" == "" ]
+if [ "$snasphot2replicate" == "" ]
 then
    exit 1
 else
-    $BINZFS send -i $lastsnapshotReplicated $namevolfs@$snaphot2replicate | ssh -o BatchMode=yes root@$server $BINZFS recv $namereplica
+    $BINZFS send -i $lastsnapshotReplicated $namevolfs@$snasphot2replicate | ssh -o BatchMode=yes root@$server $BINZFS recv $namereplica
+    snapshot2replicate="$namevolfs@$snasphot2replicate"
     if [[ "$?" == "0" ]]
     then
-       cpt=0
        while read line
        do 
-         echo $line
-         if [ $cpt -eq 0 ]
-         then
-            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$namevolfs@$snaphot2replicate:g" >$FILE_NAME
-            cpt=1
-         else
-            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$namevolfs@$snaphot2replicate:g" >>$FILE_NAME
-         fi
+            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$TEMP_FILE_NAME)
        done <<< "$(cat $FILE_NAME)"
+       /bin/rm $FILE_NAME
+       /bin/mv $TEMP_FILE_NAME $FILE_NAME
        del_old_snapshot
+       exit 0
     fi 
   fi
 fi
@@ -292,7 +275,7 @@
 
 
 #--- FULL REPLICATION ---#
-if [[ $NBsnaphot -eq 1 ]] || [[ "$lastsnapshotReplicated" == "" ]]
+if [[ $NBsnasphot -eq 1 ]] || [[ "$lastsnapshotReplicated" == "" ]]
 then
    echo "Replication by incremental snapshot is not possible"
    echo "Start FULL Replication ..."
@@ -312,16 +295,16 @@
 
     if [[ $cpt -eq 1 ]]
     then
-       snaphot2replicate=$(echo $line |sed -e "s/ \+/ /g"|cut -d " " -f1|cut -d "@" -f2)
-       echo $lastsnaphot
-       echo $snaphot2replicate
+       snasphot2replicate=$(echo $line |sed -e "s/ \+/ /g"|cut -d " " -f1|cut -d "@" -f2)
+       echo $lastsnasphot
+       echo $snasphot2replicate
        break
     fi
     cpt=$cpt+1
   done <<< "$($BINZFS list -H -r -t snapshot $namevolfs|tac)"
   lastsnapshot=$(echo $lastsnapshotReplicated |sed -e "s/ \+/ /g"|cut -d " " -f1|cut -d "@" -f2) 
  
-  if [ "$snaphot2replicate" == "$lastsnapshot" ]
+  if [ "$snasphot2replicate" == "$lastsnapshot" ]
   then
      echo "Nothing to replicate" 1>&2
      exit 1
@@ -330,10 +313,10 @@
   if [ "$server" == "" ]
   then
      check_lastsnapshotReplica=$($BINZFS list -H -r -t snapshot $namereplica |grep $lastsnapshot|wc -l)
-     check_currentsnapshotReplica=$($BINZFS list -H -r -t snapshot $namereplica |grep $snaphot2replicate=|wc -l)
+     check_currentsnapshotReplica=$($BINZFS list -H -r -t snapshot $namereplica |grep $snasphot2replicate=|wc -l)
   else
      check_lastsnapshotReplica=$(ssh -o BatchMode=yes root@$server $BINZFS list -H -r -t snapshot $namereplica |grep $lastsnapshot|wc -l)
-     check_currentsnapshotReplica=$(ssh -o BatchMode=yes root@$server $BINZFS list -H -r -t snapshot $namereplica |grep $snaphot2replicate=|wc -l)
+     check_currentsnapshotReplica=$(ssh -o BatchMode=yes root@$server $BINZFS list -H -r -t snapshot $namereplica |grep $snasphot2replicate=|wc -l)
   fi
   if [[ $check_lastsnapshotReplica -eq 0 ]]
   then
@@ -347,7 +330,7 @@
     exit 1
   fi  
 fi
- echo $snaphot2replicate
+ echo $snasphot2replicate
 incremental_replication
 }
 
