Statistics
| Revision:

root / bin / zfs-snapshot @ 55

History | View | Annotate | Download (2.4 kB)

1
#!/bin/bash
2
# $Id: zfs-snapshot 55 2013-10-10 16:20:06Z 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
BINZFS="/sbin/zfs"
9
BINZPOOL="/sbin/zpool"
10

    
11
function create_snapshot_volume {
12
#*** ETAPE DE CREATION DU SNAPSHOT ***#
13

    
14
echo ""
15
echo "*** Snapshot ZFS from : $NAME_VOL"
16
echo "*** ZFS Type : $TYPE_ZFS"
17
echo ""
18

    
19
echo "*** Snapshot Creation from $NAME_VOL"
20
ZDATE=$(date +%Y%m%d%H%M%S)
21
$BINZFS snapshot $NAME_VOL@snapshot-$ZDATE
22
echo ""
23
#*** FIN ETAPE DE CREATION DU SNAPSHOT ***#
24
}
25

    
26
function create_snapshot_filesystem {
27
#*** ETAPE DE CREATION DU SNAPSHOT ***#
28

    
29
echo ""
30
echo "*** Snapshot ZFS from : $NAME_VOL"
31
echo "*** ZFS Type : $TYPE_ZFS"
32
echo ""
33
echo "*** Snapshot Creation from $NAME_VOL"
34
ZDATE=$(date +%Y%m%d%H%M%S)
35
$BINZFS snapshot $NAME_VOL@snapshot-$ZDATE
36
echo ""
37
#*** FIN ETAPE DE CREATION DU SNAPSHOT ***#
38
}
39

    
40

    
41
function del_old_snapshot {
42
#*** ETAPE SUPPRESSION DES SNAPSHOTS OBSELETES ***#
43
echo ""
44
CPT_SNAP=$($BINZFS list -t snapshot -r $NAME_VOL |cut -d" " -f1 |grep -i $NAME_VOL\@ |wc -l)
45
echo "*** The number of snapshot to keep for $NAME_VOL is : $CPT_SNAP"
46
$BINZFS list -t snapshot -r $NAME_VOL 
47

    
48
if [ $CPT_SNAP -gt $NB_SNAP ]
49
then
50
   echo "*** Detection of the oldest snapshot "
51
   for ID_SNAP_OLDER in $($BINZFS list -t snapshot -r $NAME_VOL |cut -d" " -f1 |grep -i $NAME_VOL\@)
52
   do
53
      echo "*** The oldest snapshot is : $ID_SNAP_OLDER"
54
      break
55
   done
56
   $BINZFS destroy $ID_SNAP_OLDER
57
   if [ $? -eq 0 ]
58
   then
59
      echo "*** Remove the snapshot $ID_SNAP_OLDER [OK]"
60
   else
61
      echo "/!\\ Remove the snapshot $ID_SNAP_OLDER [ERROR]." 1>&2
62
   fi
63

    
64
fi
65
#*** FIN ETAPE SUPPRESSION DES SNAPSHOTS OBSELETES ***#
66
}
67

    
68
echo "**** $1"
69
if [ "$#" -eq 0 ]
70
then
71
   echo "/!\\ File Configuration missing ... /!\\" 1>&2
72
   exit 1
73
fi 
74

    
75
if [ ! -f $1 ]
76
then
77
   echo "/!\\ File Configuration does not exist /!\\" 1>&2
78
   exit 1
79
fi
80

    
81
NAME_VOL=$(cat $1 |grep "name" |cut -d"=" -f2)
82
TYPE_ZFS=$(cat $1 |grep "type" |cut -d"=" -f2)
83
NB_SNAP=$(cat $1 |grep "nbsnapshots"|cut -d"=" -f2)
84
if [ "$NAME_VOL" = "" ] || [ "$TYPE_ZFS" = "" ] || [ "$NB_SNAP" = "" ]
85
then
86
   echo "/!\\ Problem with the Configuration File : incoherence data or incomplete /!\\" 1>&2
87
   exit 1
88
fi
89

    
90
if [ "$TYPE_ZFS" == "volume" ]
91
then
92
   create_snapshot_volume
93
   del_old_snapshot
94

    
95

    
96
else
97
   if [ "$TYPE_ZFS" == "filesystem" ]
98
   then
99
      create_snapshot_filesystem
100
      del_old_snapshot
101

    
102
   fi
103
fi