Statistics
| Revision:

root / bin / zfs-replica @ 59

History | View | Annotate | Download (9.5 kB)

1
#!/bin/bash
2
# $Id: zfs-replica 59 2013-10-12 06:03:09Z kreverch $
3
# Copyright (C) 2013 Kevin Reverchon, Loïs Taulelle
4
# This file/program is part of gZFS free software
5
# See COPYING file for details
6
#
7

    
8
#--- Define variables ---#
9
DIR_CONF_REPLICA="/opt/gZFS/replicas/"
10
getConfigSnapshotTool="/sbin/get-conf-snapshot"
11
zfsSnapshot="/sbin/zfs-snapshot"
12
FILE_NAME=$1
13
BINZFS="/sbin/zfs"
14
BINZPOOL="/sbin/zpool"
15

    
16
#********************************
17
#* function initenv
18
#*******************************
19
function initenv {
20
if [[ -z $FILE_NAME ]]
21
then
22
   echo "/!\\ Replica Configuration file missing /!\\" 1>&2
23
   exit 1
24
fi
25

    
26
if [ ! -f $1 ]
27
then
28
   echo "/!\\ Replica Configuration file missing /!\\" 1>&2
29
   exit 1
30
fi
31

    
32
if [ ! -e $FILE_NAME ]
33
then
34
   echo "Replica Configuration file not found" 1>&2
35
   exit 1;
36
fi
37
echo $FILE_NAME
38
lastsnapshotReplicated=$(cat $FILE_NAME|grep lastsnapshotreplicated|cut -d "=" -f2)
39
server=$(cat $FILE_NAME|grep server|cut -d "=" -f2)
40
nbreplica=$(cat $FILE_NAME|grep nbreplica|cut -d "=" -f2)
41
namereplica=$(cat $FILE_NAME|grep namereplica|cut -d "=" -f2)
42
namevolfs=$(cat $FILE_NAME|grep "name="|cut -d "=" -f2)
43

    
44
if [[ -z $namereplica ]] || [[ -z $nbreplica ]] || [[ -z $namevolfs ]]
45
then
46
   echo "Incoherence data in $FILE_NAME" 1>&2
47
   exit 1
48
fi
49

    
50
echo "$getConfigSnapshotTool $namevolfs"
51
getlistSnapshot=$($getConfigSnapshotTool $namevolfs)
52
if [[ "$getlistSnapshot" == "" ]]
53
then
54
  echo "Please, enable a snapshot configuration for $namevolfs" 1>&2
55
  exit 1
56
fi
57
}
58

    
59
#****************************************
60
#* function check server ssh connection
61
#***************************************
62
function checkServerConnection {
63

    
64
ssh -o BatchMode=yes root@$server ls
65

    
66
if [ "$?" != "0" ]
67
then
68
  echo "SSH Connection server $server failed, please check the private/public key" 1>&2
69
  exit 1
70
fi
71

    
72
}
73

    
74
function checkCoherence {
75
  ssh -o BatchMode=yes root@$server $BINZFS list $namereplica
76
  if [ "$?" != "0" ]
77
  then
78
     return false
79
  fi
80

    
81
}
82

    
83
function del_old_snapshot {
84
#*** ETAPE SUPPRESSION DES SNAPSHOTS OBSELETES ***#
85
if [[ "$server" == "" ]]
86
then
87
  echo ""
88
  CPT_SNAP=$($BINZFS list -t snapshot -r $namereplica |cut -d" " -f1 |grep -i $namereplica\@ |wc -l)
89
  echo "*** Number of snapshots for $namereplica is : $CPT_SNAP"
90
  $BINZFS list -t snapshot -r $namereplica
91

    
92
  if [ $CPT_SNAP -gt $nbreplica ]
93
  then
94
    echo "*** Detection of the oldest snapshot"
95
    for ID_SNAP_OLDER in $($BINZFS list -t snapshot -r $namereplica |cut -d" " -f1 |grep -i $namereplica\@)
96
    do
97
      echo "*** The oldest snapshot is : $ID_SNAP_OLDER"
98
      break
99
    done
100
    $BINZFS destroy $ID_SNAP_OLDER
101
    if [ $? -eq 0 ]
102
    then
103
       echo "*** Remove the snapshot $ID_SNAP_OLDER [OK]"
104
    else
105
      echo "/!\\ Remove the snapshot $ID_SNAP_OLDER [ERROR]."
106
    fi
107
  fi
108

    
109
else
110
  echo ""
111
  CPT_SNAP=$(ssh -o BatchMode=yes root@$server $BINZFS list -t snapshot -r $namereplica |cut -d" " -f1 |grep -i $namereplica\@ |wc -l)
112
  echo "*** Number of snapshots for $namereplica is : $CPT_SNAP"
113
  ssh -o BatchMode=yes root@$server $BINZFS list -t snapshot -r $namereplica
114

    
115
  if [ $CPT_SNAP -gt $nbreplica ]
116
  then
117
    echo "*** Detection of the oldest snapshot"
118
    for ID_SNAP_OLDER in $(ssh -o BatchMode=yes root@$server $BINZFS list -t snapshot -r $namereplica |cut -d" " -f1 |grep -i $namereplica\@)
119
    do
120
      echo "*** The oldest snapshot is : $ID_SNAP_OLDER"
121
      break
122
    done
123
    ssh -o BatchMode=yes root@$server $BINZFS destroy $ID_SNAP_OLDER
124
    if [ $? -eq 0 ]
125
    then
126
       echo "*** Remove the snapshot $ID_SNAP_OLDER [OK]"
127
    else
128
      echo "/!\\ Remove the snapshot $ID_SNAP_OLDER [ERROR]."
129
    fi
130
  fi
131

    
132
fi
133
#*** FIN ETAPE SUPPRESSION DES SNAPSHOTS OBSELETES ***#
134
}
135

    
136

    
137
#*************************************
138
#* function FULL REPLICATION
139
#*************************************
140
function fullreplication {
141

    
142
parentdataset=$(awk '{
143
              n = split($0, t, "/")
144
              for (i = 0; ++i <= n-1;)
145
                if (i == 1)
146
                  printf t[i]
147
                else
148
                  printf "/"t[i]
149
              }' <<< $(echo $namereplica))
150

    
151
if [[ "$server" == "" ]]
152
then
153

    
154
    $BINZFS list $parentdataset
155
    if [[ "?" != "0" ]]
156
    then
157
      $BINZFS create -p $parentdataset
158
    fi
159

    
160
    $BINZFS send $snapshot2replicate | $BINZFS recv $namereplica
161
    if [[ "$?" == "0" ]]
162
    then
163
       cpt=0
164
       while read line
165
       do 
166
         if [ $cpt -eq 0 ]
167
         then
168
            echo $line
169
            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >$FILE_NAME)
170
            cpt=1
171
         else
172
            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$FILE_NAME)
173
         fi 
174
       done <<< "$(cat $FILE_NAME)"
175
       del_old_snapshot
176
    fi 
177
   
178
else
179
  checkServerConnection
180
  if [[ "$snapshot2replicate" != "" ]]
181
  then
182
    ssh -o BatchMode=yes root@$server $BINZFS list $parentdataset
183
    if [[ "?" != "0" ]]
184
    then
185
      ssh -o BatchMode=yes root@$server $BINZFS create -p $parentdataset
186
    fi
187

    
188
    $BINZFS send $snapshot2replicate |ssh -o BatchMode=yes root@$server $BINZFS recv $namereplica
189
    if [[ "$?" == "0" ]]
190
    then
191
       cpt=0
192
       while read line
193
       do 
194
         if [ $cpt -eq 0 ]
195
         then
196
            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >$FILE_NAME
197
            cpt=1
198
         else
199
            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$FILE_NAME
200
         fi
201
       done <<< "$(cat $FILE_NAME)"
202
       del_old_snapshot
203
    fi 
204
  fi
205
exit 0
206
fi
207

    
208
}
209

    
210

    
211

    
212
function incremental_replication {
213

    
214
echo "Start incremental replication"
215
if [[ "$server" == "" ]]
216
then
217
    $BINZFS send -i $lastsnapshotReplicated $namevolfs@$lastsnaphot | root@$server $BINZFS recv $namereplica
218
    if [[ "$?" == "0" ]]
219
    then
220
       cpt=0
221
       while read line
222
       do 
223
         if [ $cpt -eq 0 ]
224
         then
225
            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >$FILE_NAME
226
            cpt=1
227
         else
228
            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >$FILE_NAME
229
         fi
230
       done <<< "$(cat $FILE_NAME)"
231
       del_old_snapshot
232
    fi 
233
   
234
else
235
  checkServerConnection
236
if [ "$snaphot2replicate" == "" ]
237
then
238
   exit 1
239
else
240
    $BINZFS send -i $lastsnapshotReplicated $namevolfs@$snaphot2replicate | ssh -o BatchMode=yes root@$server $BINZFS recv $namereplica
241
    if [[ "$?" == "0" ]]
242
    then
243
       cpt=0
244
       while read line
245
       do 
246
         echo $line
247
         if [ $cpt -eq 0 ]
248
         then
249
            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$namevolfs@$snaphot2replicate:g" >$FILE_NAME
250
            cpt=1
251
         else
252
            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$namevolfs@$snaphot2replicate:g" >>$FILE_NAME
253
         fi
254
       done <<< "$(cat $FILE_NAME)"
255
       del_old_snapshot
256
    fi 
257
  fi
258
fi
259

    
260
}
261

    
262

    
263
#****************************************
264
#* NO INTERACTIVE MODE
265
#****************************************
266
function nointeractive {
267

    
268
#--- check and init env ---#
269
initenv
270

    
271
fileconf=$($getConfigSnapshotTool $namevolfs|grep fileconf|cut -d "=" -f2)
272
NBsnapshot=$($BINZFS list -H -r -t snapshot $namevolfs|wc -l)
273

    
274
if [[ $NBsnapshot -eq 0 ]]
275
then
276
   echo "no snapshot, start snapshoting ..."  
277
   $zfsSnapshot $fileconf
278
fi
279

    
280
valchk=checkCoherence
281
if [[ "$valchk" == "false" ]] && [[ "$lastsnapshotReplicated" != "" ]]
282
then
283
   echo "Replication by incremental snapshot is not possible : checkcoherence return false "
284
   echo "Start FULL Replication ..."
285
   while read line
286
   do
287
     snapshot2replicate=$(echo $line |echo $line |sed -e "s/ \+/ /g"|cut -d " " -f1)
288
   done <<< "$($BINZFS list -H -r -t snapshot $namevolfs)"
289
   fullreplication
290
fi
291

    
292

    
293
#--- FULL REPLICATION ---#
294
if [[ $NBsnaphot -eq 1 ]] || [[ "$lastsnapshotReplicated" == "" ]]
295
then
296
   echo "Replication by incremental snapshot is not possible"
297
   echo "Start FULL Replication ..."
298
   while read line
299
   do 
300
     snapshot2replicate=$(echo $line |echo $line |sed -e "s/ \+/ /g"|cut -d " " -f1)
301
   done <<< "$($BINZFS list -H -r -t snapshot $namevolfs)"
302
   fullreplication
303
fi
304

    
305
#-- INCREMENTAL REPLICATION ---#
306
if [[ $NBsnapshot -gt 1 ]]
307
then
308
  cpt=1
309
  while read line
310
  do
311

    
312
    if [[ $cpt -eq 1 ]]
313
    then
314
       snaphot2replicate=$(echo $line |sed -e "s/ \+/ /g"|cut -d " " -f1|cut -d "@" -f2)
315
       echo $lastsnaphot
316
       echo $snaphot2replicate
317
       break
318
    fi
319
    cpt=$cpt+1
320
  done <<< "$($BINZFS list -H -r -t snapshot $namevolfs|tac)"
321
  lastsnapshot=$(echo $lastsnapshotReplicated |sed -e "s/ \+/ /g"|cut -d " " -f1|cut -d "@" -f2) 
322
 
323
  if [ "$snaphot2replicate" == "$lastsnapshot" ]
324
  then
325
     echo "Nothing to replicate" 1>&2
326
     exit 1
327
 
328
  fi
329
  if [ "$server" == "" ]
330
  then
331
     check_lastsnapshotReplica=$($BINZFS list -H -r -t snapshot $namereplica |grep $lastsnapshot|wc -l)
332
     check_currentsnapshotReplica=$($BINZFS list -H -r -t snapshot $namereplica |grep $snaphot2replicate=|wc -l)
333
  else
334
     check_lastsnapshotReplica=$(ssh -o BatchMode=yes root@$server $BINZFS list -H -r -t snapshot $namereplica |grep $lastsnapshot|wc -l)
335
     check_currentsnapshotReplica=$(ssh -o BatchMode=yes root@$server $BINZFS list -H -r -t snapshot $namereplica |grep $snaphot2replicate=|wc -l)
336
  fi
337
  if [[ $check_lastsnapshotReplica -eq 0 ]]
338
  then
339
     echo "Incoherence, incremental replication failed : $lastsnapshotReplicated is missing" 1>&2
340
     exit 1
341
  fi
342
  
343
  if [[ $check_currentsnapshotReplica -eq 1 ]]
344
  then
345
    echo "No replica to do, $lastSnapshot already exists" 1>&2
346
    exit 1
347
  fi  
348
fi
349
 echo $snaphot2replicate
350
incremental_replication
351
}
352

    
353
nointeractive
354