Statistiques
| Révision :

root / bin / enable-replica @ 64

Historique | Voir | Annoter | Télécharger (6,51 ko)

1
#!/bin/bash
2
# $Id: enable-replica 51 2013-10-10 11:20:37Z ltaulell $
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
DIR_CONF_REPLICA="/opt/gZFS/replicas/"
9
DIR_CRON="/etc/cron.d/"
10
BIN_ZFS_REPLICA="/sbin/zfs-replica"
11
BINZFS="/sbin/zfs"
12
BINZPOOL="/sbin/zpool"
13

    
14
# TODO: traduire les msg
15

    
16
function usage {
17
  echo "USAGE :"
18
  echo "$0 -o volume_fs_to_replicate -r volume_fs_destination -n number_replicas_to_keep -m minute_cron -H hour_cron -D Day_of_Month_cron -M month_cron -d day_of_week_cron [-s server destination]" 1>&2
19
  echo "or" 1>&2
20
  echo "$0 -i : for a interactive mode" 1>&2
21
  echo "" 1>&2
22
  echo "OPTIONS : " 1>&2
23
  echo "  -o : origin volume or a filesystem to replicate" 1>&2
24
  echo "  -r : volume or filesystem replicated" 1>&2
25
  echo "  -n : number replicas to keep" 1>&2
26
  echo "  -s : if replica destination is on another zfs server, you need to specify this option with the name of server." 1>&2
27
  echo "      Warning ! Remote replication use ssh. So please, generate a ssh private/public key and the public key in /root/.ssh/authorized_key" 1>&2
28
  echo "      on remote server" 1>&2
29
  echo "  -m : minute field for cron replication" 1>&2
30
  echo "  -H : hour field for cron replication" 1>&2
31
  echo "  -D : Day of Month field for cron replication" 1>&2
32
  echo "  -M : Month field for cron replication" 1>&2
33
  echo "  -d : Day of week for cron replication" 1>&2
34
  echo "  -h : display this message"
35

    
36
}
37

    
38
function checkField4Cron {
39
  if [[ $OPTARG =~ [a-zA-Z] ]]
40
  then
41
     echo "-$OPTION contain a bad value" 1>&2
42
     exit 1
43
  fi
44
}
45

    
46
function createCronFile4Snapshot {
47
FILE_NAME=$(echo "replica-"${volfs//\//-})
48
echo "$MINUTE $HOUR $MONTH $DAYMONTH $DAYWEEK root $BIN_ZFS_REPLICA $DIR_CONF_REPLICA$FILE_NAME.conf" > $DIR_CRON$FILE_NAME
49
}
50

    
51
function interactive {
52
$BINZFS list
53
echo "*** Select the ZFS volume/filesystem to replicate : ***" 
54

    
55
while read inputline
56
do
57
   volfs=$inputline
58
   if [ "$volfs" = "" ]
59
   then
60
      echo "*** Select the ZFS volume/filesystem to replicate : ***" 
61
   else
62

    
63
      echo "*** Are you sur : $inputline ? (y/n) ***"
64

    
65

    
66
      while read inputConfirm
67
      do
68
        if [ "$inputConfirm" = "y" ]||[ "$inputConfirm" = "n" ]
69
         then
70
            break
71
         fi
72
         echo "*** Are you sur  : $inputline ? (y/n) ***"
73
      done
74
      if [ "$inputConfirm" = "y" ]
75
      then
76
         $BINZFS list $volfs
77
         if [ $? -eq 0 ]
78
         then
79
            echo "*** The ZFS volume/filesystem selected $volfs exists ***"
80
            break
81
         else
82
            echo "*** The ZFS volume/filesystem selected $volfs does not exist ***"    
83
            echo "*** Select the ZFS volume/filesystem to replicate : ***"
84
         fi
85
      fi
86
      if [ "$inputConfirm" != "y" ]
87
      then
88
         echo "*** Select the ZFS volume/filesystem to replicate : ***"
89
      fi
90
   fi
91
done
92

    
93
echo "*** Enter replica name (syntax pool_name/replica_name, example zpfront/monvolume) ***" 
94
echo "*** /!\ This program does not check if the replica name is valid /!\***" 
95
while read inputline
96
do
97
   volfsdestination=$inputline
98
   if [ "$volfsdestination" = "" ]
99
   then
100
      echo "*** Enter replica name (syntax pool_name/replica_name, example zpfront/monvolume) ***" 
101
      echo "*** /!\ This program does not check if the replica name is valid /!\***" 
102
   else
103

    
104
      echo "*** Are you sur : $inputline ? (y/n) ***"
105

    
106

    
107
      while read inputConfirm
108
      do
109
        if [ "$inputConfirm" = "y" ]||[ "$inputConfirm" = "n" ]
110
         then
111
            break
112
         fi
113
         echo "*** Are you sur : $inputline ? (y/n) ***"
114
      done
115
      if [ "$inputConfirm" != "y" ]
116
      then
117
         echo "*** Enter replica name (syntax pool_name/replica_name, example zpfront/monvolume) ***" 
118
         echo "*** /!\ This program does not check if the replica name is valid /!\***" 
119
      else
120
        break
121
      fi
122
   fi
123
done
124

    
125
echo "*** Replicate to a remote server (y/n)?"
126
while read inputConfirm
127
do
128
   if [ "$inputConfirm" = "y" ]||[ "$inputConfirm" = "n" ]
129
   then
130
      break
131
   fi
132
   echo "*** Are you sur : $inputline ? (y/n) ***"
133
done
134
if [ "$inputConfirm" = "y" ]
135
then
136
   echo "*** Enter server name : "
137
   while read inputserver
138
   do
139
      if [ "$inputserver" != "" ]
140
      then
141
         server=$inputserver
142
         break
143
      fi
144
      echo "*** Enter server name : "
145
   done
146
else
147
   server=""
148
fi
149
   
150
echo "*** Enter the replica number to keep : ***"
151
while read inputline
152
do
153
   nbreplica=$inputline
154
   if [ "$nbreplica" = "" ]
155
   then
156
     echo "*** Enter the replica number to keep : ***"
157
   else
158
     echo "*** Creation of configuration file ***"
159
     FILE_NAME=$(echo "replica-"${volfs//\//-})
160
     >$DIR_CONF_REPLICA$FILE_NAME.conf
161
      echo "name=$volfs" >> $DIR_CONF_REPLICA$FILE_NAME.conf
162
      echo "namereplica=$volfsdestination-replica">> $DIR_CONF_REPLICA$FILE_NAME.conf
163
      echo "nbreplica=$nbreplica" >> $DIR_CONF_REPLICA$FILE_NAME.conf
164
      echo "lastsnapshotreplicated=" >> $DIR_CONF_REPLICA$FILE_NAME.conf
165
      echo "server=$server">> $DIR_CONF_REPLICA$FILE_NAME.conf
166
     break
167
   fi
168
done
169

    
170
}
171

    
172

    
173

    
174
function nointeractive {
175
  FILE_NAME=$(echo "replica-"${volfs//\//-})
176
  >$DIR_CONF_REPLICA$FILE_NAME.conf
177
  echo "name=$volfs" >> $DIR_CONF_REPLICA$FILE_NAME.conf
178
  echo "namereplica=$volfsdestination-replica">> $DIR_CONF_REPLICA$FILE_NAME.conf
179
  echo "nbreplica=$nbreplica" >> $DIR_CONF_REPLICA$FILE_NAME.conf
180
  echo "lastsnapshotreplicated=" >> $DIR_CONF_REPLICA$FILE_NAME.conf
181
  echo "server=$server">> $DIR_CONF_REPLICA$FILE_NAME.conf
182
}
183

    
184

    
185
while getopts "is:r:o:n:m:H::D:M:d:h" OPTION
186
do
187
     case $OPTION in
188
         h)
189
             usage
190
             exit 1
191
             ;;
192
         r)
193
            volfsdestination=$OPTARG
194
            ;;
195
         o)
196
            volfs=$OPTARG
197
            ;;
198
         n)
199
            nbreplica=$OPTARG
200
            ;;
201
         s)
202
            server=$OPTARG
203
            ;;
204
         m)
205
            checkField4Cron
206
            MINUTE=$OPTARG
207
            ;;
208
         H)
209
            checkField4Cron
210
            HOUR=$OPTARG
211
            ;;
212
         D)
213
            checkField4Cron
214
            DAYMONTH=$OPTARG
215
            ;;
216
         M)
217
            checkField4Cron
218
            MONTH=$OPTARG
219
            ;;
220
         d)
221
            checkField4Cron
222
            DAYWEEK=$OPTARG
223
            ;;
224
         i)
225
            interactive
226
            exit 0
227
            ;;
228
     esac
229
done
230
if [[ -z $volfsdestination ]] || [[ -z $volfs ]] || [[ -z $nbreplica ]] || [[ -z $MINUTE ]] || [[ -z $HOUR ]] || [[ -z $DAYMONTH ]] || [[ -z $MONTH ]] || [[ -z $DAYWEEK ]] 
231
then
232
     usage
233
     exit 1
234
fi
235

    
236
nointeractive
237
createCronFile4Snapshot
238

    
239
/etc/init.d/cron restart