Statistics
| Revision:

root / bin / zfs-replica @ 62

History | View | Annotate | Download (9.1 kB)

1
#!/bin/bash
2
# $Id: zfs-replica 62 2013-10-13 19:56: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
TEMP_FILE_NAME="$1-tmp"
14
>$TEMP_FILE_NAME
15
BINZFS="/sbin/zfs"
16
BINZPOOL="/sbin/zpool"
17

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

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

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

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

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

    
61
#****************************************
62
#* function check server ssh connection
63
#***************************************
64
function checkServerConnection {
65

    
66
ssh -o BatchMode=yes root@$server ls
67

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

    
74
}
75

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

    
83
}
84

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

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

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

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

    
134
fi
135
#*** FIN ETAPE SUPPRESSION DES SNAPSHOTS OBSELETES ***#
136
}
137

    
138

    
139
#*************************************
140
#* function FULL REPLICATION
141
#*************************************
142
function fullreplication {
143

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

    
153
if [[ "$server" == "" ]]
154
then
155

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

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

    
185
    $BINZFS send $snapshot2replicate |ssh -o BatchMode=yes root@$server $BINZFS recv $namereplica
186
    if [[ "$?" == "0" ]]
187
    then
188
       while read line
189
       do 
190
            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$TEMP_FILE_NAME)
191
       done <<< "$(cat $FILE_NAME)"
192
       /bin/rm $FILE_NAME
193
       /bin/mv $TEMP_FILE_NAME $FILE_NAME
194
       del_old_snapshot
195
       exit 0
196
    fi 
197
  fi
198
fi
199

    
200
}
201

    
202

    
203

    
204
function incremental_replication {
205

    
206
echo "Start incremental replication for : $snasphot2replicate"
207
if [[ "$server" == "" ]]
208
then
209
    $BINZFS send -i $lastsnapshotReplicated $namevolfs@$snasphot2replicate | $BINZFS recv $namereplica
210
    snapshot2replicate="$namevolfs@$snasphot2replicate"
211
    if [[ "$?" == "0" ]]
212
    then
213
       while read line
214
       do
215
            echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g"
216
       done <<< "$(cat $FILE_NAME)"
217
       /bin/rm $FILE_NAME
218
       /bin/mv $TEMP_FILE_NAME $FILE_NAME
219
       del_old_snapshot
220
       exit 0
221
    fi 
222
else
223
  checkServerConnection
224
if [ "$snasphot2replicate" == "" ]
225
then
226
   exit 1
227
else
228
    $BINZFS send -i $lastsnapshotReplicated $namevolfs@$snasphot2replicate | ssh -o BatchMode=yes root@$server $BINZFS recv $namereplica
229
    snapshot2replicate="$namevolfs@$snasphot2replicate"
230
    if [[ "$?" == "0" ]]
231
    then
232
       while read line
233
       do 
234
            $(echo $line|sed -e "s:lastsnapshotreplicated=.*$:lastsnapshotreplicated=$snapshot2replicate:g" >>$TEMP_FILE_NAME)
235
       done <<< "$(cat $FILE_NAME)"
236
       /bin/rm $FILE_NAME
237
       /bin/mv $TEMP_FILE_NAME $FILE_NAME
238
       del_old_snapshot
239
       exit 0
240
    fi 
241
  fi
242
fi
243

    
244
}
245

    
246

    
247
#****************************************
248
#* NO INTERACTIVE MODE
249
#****************************************
250
function nointeractive {
251

    
252
#--- check and init env ---#
253
initenv
254

    
255
fileconf=$($getConfigSnapshotTool $namevolfs|grep fileconf|cut -d "=" -f2)
256
NBsnapshot=$($BINZFS list -H -r -t snapshot $namevolfs|wc -l)
257

    
258
if [[ $NBsnapshot -eq 0 ]]
259
then
260
   echo "no snapshot, start snapshoting ..."  
261
   $zfsSnapshot $fileconf
262
fi
263

    
264
valchk=checkCoherence
265
if [[ "$valchk" == "false" ]] && [[ "$lastsnapshotReplicated" != "" ]]
266
then
267
   echo "Replication by incremental snapshot is not possible : checkcoherence return false "
268
   echo "Start FULL Replication ..."
269
   while read line
270
   do
271
     snapshot2replicate=$(echo $line |echo $line |sed -e "s/ \+/ /g"|cut -d " " -f1)
272
   done <<< "$($BINZFS list -H -r -t snapshot $namevolfs)"
273
   fullreplication
274
fi
275

    
276

    
277
#--- FULL REPLICATION ---#
278
if [[ $NBsnasphot -eq 1 ]] || [[ "$lastsnapshotReplicated" == "" ]]
279
then
280
   echo "Replication by incremental snapshot is not possible"
281
   echo "Start FULL Replication ..."
282
   while read line
283
   do 
284
     snapshot2replicate=$(echo $line |echo $line |sed -e "s/ \+/ /g"|cut -d " " -f1)
285
   done <<< "$($BINZFS list -H -r -t snapshot $namevolfs)"
286
   fullreplication
287
fi
288

    
289
#-- INCREMENTAL REPLICATION ---#
290
if [[ $NBsnapshot -gt 1 ]]
291
then
292
  cpt=1
293
  while read line
294
  do
295

    
296
    if [[ $cpt -eq 1 ]]
297
    then
298
       snasphot2replicate=$(echo $line |sed -e "s/ \+/ /g"|cut -d " " -f1|cut -d "@" -f2)
299
       echo $lastsnasphot
300
       echo $snasphot2replicate
301
       break
302
    fi
303
    cpt=$cpt+1
304
  done <<< "$($BINZFS list -H -r -t snapshot $namevolfs|tac)"
305
  lastsnapshot=$(echo $lastsnapshotReplicated |sed -e "s/ \+/ /g"|cut -d " " -f1|cut -d "@" -f2) 
306
 
307
  if [ "$snasphot2replicate" == "$lastsnapshot" ]
308
  then
309
     echo "Nothing to replicate" 1>&2
310
     exit 1
311
 
312
  fi
313
  if [ "$server" == "" ]
314
  then
315
     check_lastsnapshotReplica=$($BINZFS list -H -r -t snapshot $namereplica |grep $lastsnapshot|wc -l)
316
     check_currentsnapshotReplica=$($BINZFS list -H -r -t snapshot $namereplica |grep $snasphot2replicate=|wc -l)
317
  else
318
     check_lastsnapshotReplica=$(ssh -o BatchMode=yes root@$server $BINZFS list -H -r -t snapshot $namereplica |grep $lastsnapshot|wc -l)
319
     check_currentsnapshotReplica=$(ssh -o BatchMode=yes root@$server $BINZFS list -H -r -t snapshot $namereplica |grep $snasphot2replicate=|wc -l)
320
  fi
321
  if [[ $check_lastsnapshotReplica -eq 0 ]]
322
  then
323
     echo "Incoherence, incremental replication failed : $lastsnapshotReplicated is missing" 1>&2
324
     exit 1
325
  fi
326
  
327
  if [[ $check_currentsnapshotReplica -eq 1 ]]
328
  then
329
    echo "No replica to do, $lastSnapshot already exists" 1>&2
330
    exit 1
331
  fi  
332
fi
333
 echo $snasphot2replicate
334
incremental_replication
335
}
336

    
337
nointeractive
338