Statistiques
| Révision :

root / bin / enable-snapshot @ 67

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

1
#!/bin/bash
2
# $Id: enable-snapshot 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_CRON="/etc/cron.d/"
9
DIR_CONF_SNAPSHOT="/opt/gZFS/snapshots/"
10
BIN_ZFS_SNAPSHOT="/sbin/zfs-snapshot"
11
BINZFS="/sbin/zfs"
12
BINZPOOL="/sbin/zpool"
13

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

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

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

    
44
function interactive {
45
$BINZFS list
46
echo "*** Select the ZFS volume/filesystem : ***"
47

    
48
while read inputline
49
do
50
   NAME_VOL=$inputline
51
   if [ "$NAME_VOL" = "" ]
52
   then
53
      echo "*** Select the ZFS volume/filesystem : ***"
54
   else
55

    
56
      echo "*** Are you sur : $inputline ? (y/n) ***"
57
   
58

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

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

    
85
   fi
86
done
87

    
88
echo "*** ZFS Type : volume or filesystem ? ***"
89

    
90
while read inputline
91
do
92
  if [ "$inputline" = "volume" ] || [ "$inputline" = "filesystem" ]
93
  then
94
     TYPE_ZFS=$inputline
95
     break
96
  else
97
    echo "*** ZFS Type : volume or filesystem ? ***"
98
  fi
99

    
100
done
101

    
102

    
103
echo "*** How many snapshot to keep ? ***"
104

    
105

    
106

    
107
while read inputline
108
do
109
   NB_SNAP=$inputline
110
   if [ "$NB_SNAP" = "" ]
111
   then
112

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

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

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

    
182
          break ;;
183

    
184
     "3" )
185
          echo "*** Selelct day of month : 01-30"
186
          while read inputjour
187
          do
188
             if [[ "$inputjour" =~ [0-9][0-9] ]]
189
             then
190

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

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

    
213
          break ;;
214

    
215
   esac
216
   echo "*** Snapshots Frequency *** "
217
   echo "1 - Every days"
218
   echo "2 - Every weeks"
219
   echo "3 - Every months"
220
   echo "*******************************"
221
   
222
done
223
/etc/init.d/cron restart
224
}
225

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

    
273
if [[ -z $MINUTE ]] || [[ -z $HOUR ]] || [[ -z $DAYMONTH ]] || [[ -z $MONTH ]] || [[ -z $DAYWEEK ]] || [[ -z $NAMEZFS ]] || [[ -z $TYPEZFS ]] || [[ -z $SNAP2KEEP ]]
274
then
275
     usage
276
     exit 1
277
fi
278

    
279
createCronFile4Snapshot
280

    
281
/etc/init.d/cron restart
282

    
283