Statistiques
| Révision :

root / bin / enable-snapshot @ 49

Historique | Voir | Annoter | Télécharger (7,35 ko)

1
#!/bin/bash
2
# $Id: enable-snapshot 49 2013-10-10 08:59:11Z kreverch $
3

    
4
DIR_CRON="/etc/cron.d/"
5
DIR_CONF_SNAPSHOT="/opt/gZFS/snapshots/"
6
BIN_ZFS_SNAPSHOT="/sbin/zfs-snapshot"
7
BINZFS="/sbin/zfs"
8
BINZPOOL="/sbin/zpool"
9

    
10
function usage {
11
  echo "Usage : $0 options" 1>&2
12
  echo "OPTIONS :" 1>&2
13
  echo "  -i : interactive mode, this option is not compatible with other options" 1>&2
14
  echo "  -m : minute field for cron snapshot" 1>&2
15
  echo "  -H : hour field for cron snapshot" 1>&2
16
  echo "  -D : Day of Month field for cron snapshot" 1>&2
17
  echo "  -M : Month field for cron snapshot" 1>&2
18
  echo "  -d : Day of week for cron snapshot" 1>&2
19
  echo "  -N : Name of ZFS Volume/FileSystem to snapshot" 1>&2
20
  echo "  -t : Type (Possible values: volume or filesystem)" 1>&2
21
  echo "  -k : Number of snapshot to keep" 1>&2
22
}
23

    
24
function checkField4Cron {
25
  if [[ $OPTARG =~ [a-zA-Z] ]]
26
  then
27
     echo "-$OPTION contain a bad value" 1>&2
28
     exit 1
29
  fi
30
}
31

    
32
function createCronFile4Snapshot {
33
FILE_NAME=$(echo "snapshot-"${NAMEZFS//\//-})
34
echo "$MINUTE $HOUR $MONTH $DAYMONTH $DAYWEEK root $BIN_ZFS_SNAPSHOT $DIR_CONF_SNAPSHOT$FILE_NAME.conf" > $DIR_CRON$FILE_NAME
35
echo "name=$NAMEZFS" >$DIR_CONF_SNAPSHOT$FILE_NAME.conf
36
echo "type=$TYPEZFS" >>$DIR_CONF_SNAPSHOT$FILE_NAME.conf
37
echo "nbsnapshots=$SNAP2KEEP" >>$DIR_CONF_SNAPSHOT$FILE_NAME.conf
38
}
39

    
40
function interactive {
41
$BINZFS list
42
echo "*** Select the ZFS volume/filesystem : ***"
43

    
44
while read inputline
45
do
46
   NAME_VOL=$inputline
47
   if [ "$NAME_VOL" = "" ]
48
   then
49
      echo "*** Select the ZFS volume/filesystem : ***"
50
   else
51

    
52
      echo "*** Are you sur : $inputline ? (y/n) ***"
53
   
54

    
55
      while read inputConfirm
56
      do
57
         if [ "$inputConfirm" = "y" ]||[ "$inputConfirm" = "n" ]
58
         then
59
            break
60
         fi
61
         echo "*** Are you sur : $inputline ? (y/n) ***"
62
      done
63

    
64
      if [ "$inputConfirm" = "y" ]
65
      then
66
         $BINZFS list $NAME_VOL
67
         if [ $? -eq 0 ]
68
         then
69
            echo "*** The ZFS volume/filesystem $NAME_VOL exists ***"
70
            break
71
         else
72
            echo "*** The ZFS volume/filesystem $NAME_VOL does not exist ***"    
73
            echo "*** Select the ZFS volume/filesystem : ***"
74
         fi
75
      fi
76
      if [ "$inputConfirm" != "y" ]
77
      then
78
         echo "*** Select the ZFS volume/filesystem : ***"
79
      fi
80

    
81
   fi
82
done
83

    
84
echo "*** ZFS Type : volume or filesystem ? ***"
85

    
86
while read inputline
87
do
88
  if [ "$inputline" = "volume" ] || [ "$inputline" = "filesystem" ]
89
  then
90
     TYPE_ZFS=$inputline
91
     break
92
  else
93
    echo "*** ZFS Type : volume or filesystem ? ***"
94
  fi
95

    
96
done
97

    
98

    
99
echo "*** How many snapshot to keep ? ***"
100

    
101

    
102

    
103
while read inputline
104
do
105
   NB_SNAP=$inputline
106
   if [ "$NB_SNAP" = "" ]
107
   then
108

    
109
       echo "*** How many snapshot to keep ? ***"
110
              
111
   else
112
       
113
       echo "*** Creation of the configuration file ***"
114
       FILE_NAME=$(echo "snapshot-"${NAME_VOL//\//-}) 
115
       >$DIR_CONF_SNAPSHOT$FILE_NAME.conf
116
       echo "name=$NAME_VOL" > $DIR_CONF_SNAPSHOT$FILE_NAME.conf
117
       echo "nbsnapshots=$NB_SNAP" >> $DIR_CONF_SNAPSHOT$FILE_NAME.conf
118
       echo "type=$TYPE_ZFS" >> $DIR_CONF_SNAPSHOT$FILE_NAME.conf
119
       break    
120
   fi
121
done
122

    
123
echo "*** Snapshots Frequency ***"
124
echo "1 - Every days"
125
echo "2 - Every weeks"
126
echo "3 - Every Months"
127
echo "*******************************"
128
while read inputline
129
do 
130
   case $inputline in
131
   
132
    "1" )
133
          echo "*** Enter the Time (HH:MM) "
134
          while read inputheure
135
          do
136
             if [[ "$inputheure" =~ [0-9][0-9]":"[0-9][0-9] ]]
137
             then
138
                echo "Snapshot will be taken at : $inputheure"
139
                H_DEBUT=$(echo $inputheure |cut -d":" -f1)
140
                M_DEBUT=$(echo $inputheure |cut -d":" -f2)
141
                echo "$M_DEBUT $H_DEBUT * * * root $BIN_ZFS_SNAPSHOT $DIR_CONF_SNAPSHOT$FILE_NAME.conf" > $DIR_CRON$FILE_NAME
142
                break;
143
             else
144
                echo "*** Enter the Time (HH:MM) "
145
             fi
146
          done
147

    
148
          break ;;
149
    "2" ) 
150
          echo "*** Select day of week : 0-6 (0 is sunday) "
151
          while read inputjour
152
          do
153
             if [[ "$inputjour" =~ [0-9] ]]
154
             then
155
               
156
                 echo "*** Enter the Time (HH:MM) "
157
                 while read inputheure
158
                 do
159
                    if [[ "$inputheure" =~ [0-9][0-9]":"[0-9][0-9] ]]
160
                    then
161
                       echo "Snapshot will be taken at : $inputheure"
162
                       H_DEBUT=$(echo $inputheure |cut -d":" -f1)
163
                       M_DEBUT=$(echo $inputheure |cut -d":" -f2)
164
                       break;
165
                    else
166
                       echo "*** Enter the Time (HH:MM) "
167
                    fi
168
                  done
169
                
170
                  echo "$M_DEBUT $H_DEBUT * * $inputjour root $BIN_ZFS_SNAPSHOT $DIR_CONF_SNAPSHOT$FILE_NAME.conf" > $DIR_CRON$FILE_NAME
171
                  echo "*** Snapshot Configuration [OK]"
172
                  break;
173
             else
174
                 echo "*** Select day of week : 0-6 (0 is sunday) "
175
             fi
176
          done
177

    
178
          break ;;
179

    
180
     "3" )
181
          echo "*** Selelct day of month : 01-30"
182
          while read inputjour
183
          do
184
             if [[ "$inputjour" =~ [0-9][0-9] ]]
185
             then
186

    
187
                 echo "*** Enter the Time (HH:MM) "
188
                 while read inputheure
189
                 do
190
                    if [[ "$inputheure" =~ [0-9][0-9]":"[0-9][0-9] ]]
191
                    then
192
                       echo "Snapshot will be taken at : $inputheure"
193
                       H_DEBUT=$(echo $inputheure |cut -d":" -f1)
194
                       M_DEBUT=$(echo $inputheure |cut -d":" -f2)
195
                       break;
196
                    else
197
                       echo "*** Enter the Time (HH:MM) "
198
                    fi
199
                  done
200

    
201
                  echo "$M_DEBUT $H_DEBUT $inputjour * * root $BIN_ZFS_SNAPSHOT $DIR_CONF_SNAPSHOT$FILE_NAME.conf" > $DIR_CRON$FILE_NAME
202
                  echo "*** Snapshot configuration [OK]"
203
                  break;
204
             else
205
                 echo "*** Select the day of month : 01-30 "
206
             fi
207
          done
208

    
209
          break ;;
210

    
211
   esac
212
   echo "*** Snapshots Frequency *** "
213
   echo "1 - Every days"
214
   echo "2 - Every weeks"
215
   echo "3 - Every months"
216
   echo "*******************************"
217
   
218
done
219
/etc/init.d/cron restart
220
}
221

    
222
while getopts "im:H:D:M:d:N:t:k:h" OPTION
223
do
224
     case $OPTION in
225
         h)
226
             usage
227
             exit 1
228
             ;;
229
         i)
230
            interactive
231
            exit 0
232
            ;;
233
         m)
234
            checkField4Cron
235
            MINUTE=$OPTARG
236
            ;;
237
         H)
238
            checkField4Cron
239
            HOUR=$OPTARG
240
            ;;
241
         D) 
242
            checkField4Cron
243
            DAYMONTH=$OPTARG
244
            ;;
245
         M)
246
            checkField4Cron
247
            MONTH=$OPTARG
248
            ;;
249
         d)
250
            checkField4Cron
251
            DAYWEEK=$OPTARG
252
            ;;
253
         N)
254
            NAMEZFS=$OPTARG
255
            ;;
256
         t)
257
            TYPEZFS=$OPTARG
258
            ;;
259
         k)
260
            SNAP2KEEP=$OPTARG
261
            ;;
262
         *)
263
            usage
264
            exit 1
265
            ;;
266
     esac
267
done
268

    
269
if [[ -z $MINUTE ]] || [[ -z $HOUR ]] || [[ -z $DAYMONTH ]] || [[ -z $MONTH ]] || [[ -z $DAYWEEK ]] || [[ -z $NAMEZFS ]] || [[ -z $TYPEZFS ]] || [[ -z $SNAP2KEEP ]]
270
then
271
     usage
272
     exit 1
273
fi
274

    
275
createCronFile4Snapshot
276

    
277
/etc/init.d/cron restart
278

    
279