Skip to content
This repository
Newer
Older
100755 147 lines (119 sloc) 4.606 kb
e8a7c686 »
2010-07-08 added zfSnap.sh - script to create/destroy zfs snapshots with cron
1 #!/bin/sh
2
fbe72010 »
2010-08-03 Add license text. add links
3 # "THE BEER-WARE LICENSE":
4 # <aldis@bsdroot.lv> wrote this file. As long as you retain this notice you
5 # can do whatever you want with this stuff. If we meet some day, and you think
6 # this stuff is worth it, you can buy me a beer in return Aldis Berjoza
7
8 # http://wiki.bsdroot.lv/zfsnap
9 # http://aldis.git.bsdroot.lv/zfSnap/
10
9bbaf77f »
2010-08-05 fix + v1.2.0
11 VERSION=1.2.0
4c83b9ce »
2010-08-02 much, much imporved script
12
13 s2time() {
14 # convert seconds to human readable time
15 xtime=$*
16
17 years=`expr $xtime / 31536000`
18 xtime=`expr $xtime % 31536000`
19 [ ${years:-0} -gt 0 ] && years="${years}y" || years=""
20
21 months=`expr $xtime / 2592000`
22 xtime=`expr $xtime % 2592000`
23 [ ${months:-0} -gt 0 ] && months="${months}m" || months=""
24
25 days=`expr $xtime / 86400`
26 xtime=`expr $xtime % 86400`
27 [ ${days:-0} -gt 0 ] && days="${days}d" || days=""
28
29 hours=`expr $xtime / 3600`
30 xtime=`expr $xtime % 3600`
31 [ ${hours:-0} -gt 0 ] && hours="${hours}h" || hours=""
32
33 minutes=`expr $xtime / 60`
34 [ ${minutes:-0} -gt 0 ] && minutes="${minutes}M" || minutes=""
35
36 seconds=`expr $xtime % 60`
37 [ ${seconds:-0} -gt 0 ] && seconds="${seconds}s" || seconds=""
38
39 echo "${years}${months}${days}${hours}${minutes}${seconds}"
40 }
41
42 time2s() {
43 # convert human readable time to seconds
44 echo "$1" | sed -e 's/y/*31536000+/' -e 's/m/*2592000+/' -e 's/w/*604800+/' -e 's/d/*86400+/' -e 's/h/*3600+/' -e 's/M/*60+/' -e 's/s//' -e 's/\+$//' | bc -l
45 }
46
47 help() {
48 cat << EOF
98705367 »
2010-08-03 fix
49 ${0##*/} v${VERSION} by Aldis Berjoza <aldis@bsdroot.lv>
4c83b9ce »
2010-08-02 much, much imporved script
50
25821792 »
2010-07-18 added -p and -P switch
51 Syntax:
98705367 »
2010-08-03 fix
52 ${0##*/} [ generic options ] [[ -a ttl ] [ -r|-R ] z/fs1 ... ] ...
2cdfe3d6 »
2010-07-12 fix
53
4c83b9ce »
2010-08-02 much, much imporved script
54 GENERIC OPTIONS:
622f202e »
2010-08-03 remove bleat from help, add url to wiki
55 -d = delete old snapshots
56 -v = verbose output
94c29030 »
2010-08-03 added -n switch for testing
57 -n = show actions that would be performed (don't make/delete snapshots)
29c9058d »
2010-08-01 added -D switch
58
4c83b9ce »
2010-08-02 much, much imporved script
59 OPTIONS:
622f202e »
2010-08-03 remove bleat from help, add url to wiki
60 -a ttl = set how long snapshot should be kept
61 -r = create recursive snapshots for all zfs file systems that fallow
62 this switch
63 -R = create non-recursive snapshots for all zfs file systems that
64 fallow this switch
65
66 MORE INFO:
67 http://wiki.bsdroot.lv/zfsnap
68
4c83b9ce »
2010-08-02 much, much imporved script
69 EOF
70 exit
71 }
72
73 [ $# = 0 ] && help
74 [ "$1" = '-h' -o $1 = "--help" ] && help
e8a7c686 »
2010-07-08 added zfSnap.sh - script to create/destroy zfs snapshots with cron
75
57ad95fd »
2010-08-03 Code cleanup
76 tfrmt='%Y-%m-%d_%T'
86fc2ab3 »
2010-08-04 fix patterns and imporve portability
77 htime_pattern='([0-9]+y)?([0-9]+m)?([0-9]+w)?([0-9]+d)?([0-9]+h)?([0-9]+M)?([0-9]+[s]?)?'
78 date_pattern='20[0-9][0-9]-[01][0-9]-[0-3][0-9]_[0-2][0-9]:[0-5][0-9]:[0-5][0-9]'
57ad95fd »
2010-08-03 Code cleanup
79 age='1m' # default max snapshot age
4c83b9ce »
2010-08-02 much, much imporved script
80 delete_snapshots=0
81 verbose=0
94c29030 »
2010-08-03 added -n switch for testing
82 dry_run=0
e8a7c686 »
2010-07-08 added zfSnap.sh - script to create/destroy zfs snapshots with cron
83
94c29030 »
2010-08-03 added -n switch for testing
84 while [ "$1" = '-d' -o "$1" = '-v' -o "$1" = '-n' ]; do
4c83b9ce »
2010-08-02 much, much imporved script
85 [ "$1" = "-d" ] && delete_snapshots=1 && shift
86 [ "$1" = "-v" ] && verbose=1 && shift
94c29030 »
2010-08-03 added -n switch for testing
87 [ "$1" = "-n" ] && dry_run=1 && shift
e8a7c686 »
2010-07-08 added zfSnap.sh - script to create/destroy zfs snapshots with cron
88 done
89
94c29030 »
2010-08-03 added -n switch for testing
90 [ $dry_run -eq 1 ] && zfs_list=`zfs list -H | awk '{print $1}'`
4c83b9ce »
2010-08-02 much, much imporved script
91 ntime=`date "+$tfrmt"`
92 while [ "$1" ]; do
93 while [ "$1" = '-r' -o "$1" = '-R' -o "$1" = '-a' ]; do
94 [ "$1" = '-r' ] && zopt='-r' && shift
95 [ "$1" = '-R' ] && zopt='' && shift
f3e48e50 »
2010-08-03 some fixes and simplifications
96 [ "$1" = '-a' ] && age="$2" && shift 2 && echo "$age" | grep -q -E -e "^[0-9]+$" && age=`s2time $age`
4c83b9ce »
2010-08-02 much, much imporved script
97 done
98
106a6d86 »
2010-08-05 implemented recursive snapshot deletion
99 # create snapshots
4c83b9ce »
2010-08-02 much, much imporved script
100 if [ $1 ]; then
a9fc9bcc »
2010-08-03 improve dry_run and !dry_run
101 zfs_snapshot="zfs snapshot $zopt $1@${ntime}--${age}"
94c29030 »
2010-08-03 added -n switch for testing
102 if [ $dry_run -eq 0 ]; then
103 [ $verbose -eq 1 ] && echo -n "zfs snapshot $zopt $1@${ntime}--${age} ... "
a9fc9bcc »
2010-08-03 improve dry_run and !dry_run
104 $zfs_snapshot > /dev/stderr \
94c29030 »
2010-08-03 added -n switch for testing
105 && { [ $verbose -eq 1 ] && echo 'DONE'; } \
106 || { [ $verbose -eq 1 ] && echo 'FAIL'; }
107 else
a9fc9bcc »
2010-08-03 improve dry_run and !dry_run
108 echo "$zfs_snapshot"
94c29030 »
2010-08-03 added -n switch for testing
109 good_fs=0
110 for i in $zfs_list; do
111 [ "$i" = "$1" ] && { good_fs=1; break; }
112 done
113 [ $good_fs -eq 0 ] && echo "ERR: looks like zfs filesystem '$1' doesn't exist" > /dev/stderr
114 fi
4c83b9ce »
2010-08-02 much, much imporved script
115 shift
116 fi
117 done
118
106a6d86 »
2010-08-05 implemented recursive snapshot deletion
119 rm_snapshots=""
120 # delete snapshots
4c83b9ce »
2010-08-02 much, much imporved script
121 if [ "$delete_snapshots" -eq 1 ]; then
106a6d86 »
2010-08-05 implemented recursive snapshot deletion
122 for i in `zfs list -H -t snapshot | awk '{print $1}' | grep -E -e "^.*@${date_pattern}--${htime_pattern}$" | sed -e "s/^.*@//" | sort -u`; do
123 dtime=$(time2s `echo $i | sed -E -e "s/^${date_pattern}--//"`)
124 [ `expr $(date +%s) - $dtime` -gt $(date -j -f "$tfrmt" $(echo "$i" | sed -E -e "s/--${htime_pattern}$//") +%s) ] && rm_snapshot_pattern="$rm_snapshot_pattern $i"
125 done
126
9bbaf77f »
2010-08-05 fix + v1.2.0
127 if [ "$rm_snapshot_pattern" != '' ]; then
128 rm_snapshots=$(zfs list -t snapshot -H | awk '{print $1}' | grep -E -e `echo $rm_snapshot_pattern | sed -e 's/ /|/g'` | sort -r)
129 while [ "$rm_snapshots" != "" ]; do
130 rm_this_snapshot=`echo "$rm_snapshots" | head -n 1`
131
132 zfs_destroy="zfs destroy -r $rm_this_snapshot"
133 if [ $dry_run -eq 0 ]; then
134 [ $verbose -eq 1 ] && echo -n "zfs destroy $i ... "
135 $zfs_destroy > /dev/stderr \
136 && { [ $verbose -eq 1 ] && echo 'DONE'; } \
137 || { [ $verbose -eq 1 ] && echo 'FAIL'; }
138 else
139 echo "$zfs_destroy"
140 fi
141
142 rm_snapshots=$(printf "%s\n" $rm_snapshots | sed -E -e "s#`echo $rm_this_snapshot | sed -e 's#@#(/.+)?@#'`##g" | sort -r -u)
143 done
144 fi
29c9058d »
2010-08-01 added -D switch
145 fi
e8a7c686 »
2010-07-08 added zfSnap.sh - script to create/destroy zfs snapshots with cron
146 exit 0
Something went wrong with that request. Please try again.