View | Details | Raw Unified | Return to bug 196228 | Differences between
and this patch

Collapse All | Expand All

(-)zfs-snapshot (-47 / +57 lines)
Lines 63-117 Link Here
63
    type=$3
63
    type=$3
64
    skip=$4
64
    skip=$4
65
65
66
    # create the regex matching the type of snapshots we're currently working 
67
    # on
68
    case "$type" in
69
        hourly)
70
        # hourly-2009-01-01-00
71
        regex="^$pool@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]$"
72
        ;;
73
        daily)
74
        # daily-2009-01-01
75
        regex="^$pool@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$"
76
        ;;
77
        weekly)
78
        # weekly-2009-01
79
        regex="^$pool@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]"
80
        ;;
81
        monthly)
82
        # monthly-2009-01
83
        regex="^$pool@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]"
84
        ;;
85
        yearly)
86
        # yearly-2009
87
        regex="^$pool@$type-[0-9][0-9][0-9][0-9]"
88
        ;;
89
        *)
90
        echo "unknown snapshot type: $type"
91
        exit 1
92
    esac
93
94
    create_snapshot $pool $type
66
    create_snapshot $pool $type
67
    clean_snapshots $pool $type
68
}
69
70
clean_snapshots()
71
{
72
    pool=$1
95
73
96
    # get a list of all of the snapshots of this type sorted alpha, which
74
    # loop through datasets, do look for number of snapshots each
97
    # effectively is increasing date/time
75
    for dataset in $datasets; do
98
    # (using sort as zfs's sort seems to have bugs)
76
99
    snapshots=`zfs list -H -o name -t snapshot | sort | grep $regex`
77
        # create the regex matching the type of snapshots we're currently working 
100
    # count them
78
        # on
101
    count=`echo $snapshots | wc -w`
79
        case "$type" in
102
    if [ $count -ge 0 ]; then
80
            hourly)
103
        # how many items should we delete
81
            # hourly-2009-01-01-00
104
        delete=`expr $count - $keep`
82
            regex="^$dataset@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]$"
105
        count=0
83
            ;;
106
        # walk through the snapshots, deleting them until we've trimmed deleted
84
            daily)
107
        for snapshot in $snapshots; do
85
            # daily-2009-01-01
108
            if [ $count -ge $delete ]; then
86
            regex="^$dataset@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$"
109
                break
87
            ;;
110
            fi
88
            weekly)
111
            delete_snapshot $snapshot
89
            # weekly-2009-01
112
            count=`expr $count + 1`
90
            regex="^$dataset@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]"
113
        done
91
            ;;
114
    fi
92
            monthly)
93
            # monthly-2009-01
94
            regex="^$dataset@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]"
95
            ;;
96
            yearly)
97
            # yearly-2009
98
            regex="^$dataset@$type-[0-9][0-9][0-9][0-9]"
99
            ;;
100
            *)
101
            echo "unknown snapshot type: $type"
102
            exit 1
103
        esac
104
105
        # get a list of all of the snapshots of this type sorted alpha, which
106
        # effectively is increasing date/time
107
        # (using sort as zfs's sort seems to have bugs)
108
        snapshots=`zfs list -H -o name -t snapshot | sort | grep $regex`
109
        # count them
110
        count=`echo $snapshots | wc -w`
111
        if [ $count -ge 0 ]; then
112
            # how many items should we delete
113
            delete=`expr $count - $keep`
114
            count=0
115
            # walk through the snapshots, deleting them until we've trimmed deleted
116
            for snapshot in $snapshots; do
117
                if [ $count -ge $delete ]; then
118
                    break
119
                fi
120
                delete_snapshot $snapshot
121
                count=`expr $count + 1`
122
            done
123
        fi
124
    done
115
}
125
}
116
126
117
# take snapshots of type, for pools, keeping keep old ones, 
127
# take snapshots of type, for pools, keeping keep old ones, 

Return to bug 196228