-
Notifications
You must be signed in to change notification settings - Fork 57
/
zrep_status
268 lines (225 loc) · 6.57 KB
/
zrep_status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
###### File: zrep_status
# be sure to have included zrep_vars
# This file contains all "status" related routines.
# It should be folded into final "zrep" script
#
#Give this a top level zrep registered filesystem, NOT snapshot.
# Will echo out various status points, such as last sync date.
# Or if given no args, will echo out sync date for all zrep mastered fs
# Note that the date given is time of SNAPSHOT, not time sync completed.
#
zrep_status(){
typeset check fs srcfs desthost destfs date lastsynced
typeset verbose=0 vdate="" monitorr=0
typeset printall=0
if [[ "$1" == "-v" ]] ; then
verbose=1 ; shift
fi
if [[ "$1" == "-m" ]] ; then
monitor=1 ; shift
fi
if [[ "$1" == "" ]] ; then
set -- `zrep_list_master`
elif [[ "$1" == "-a" ]] ; then
set -- `zrep_list`
printall=1
fi
while [[ "$1" != "" ]] ; do
fs="$1"
destfs=`$ZFSGETVAL ${ZREPTAG}:dest-fs $fs`
if [[ "$destfs" == "-" ]] || [[ "$destfs" == "" ]]; then
zrep_errquit "$fs is not a zrep registered filesystem"
fi
if ((monitor)) ; then
# have to allow for ONE unsent, since it might be in progress
typeset count=`getunsentcount $fs`
if [[ "$count" -gt 1 ]] ; then
echo WARNNING: unset snapshot count at $count
fi
continue
fi
lastsynced=`getlastsnapsent $fs`
if [[ "$lastsynced" == "" ]] ; then
date="[NEVER]"
else
if (( Z_HAS_SNAPPROPS )) ; then
typeset sentseconds=`$ZFSGETVAL ${ZREPTAG}:sent $lastsynced`
date=`printf "%(${ZREP_DATEFORMAT})T" "$sentseconds"`
vdate=${date%:*}
fi
#This is also a fallback for no-perl FreeBSD systems
if [[ "$vdate" == "" ]] ; then
date=`$ZFSGETVAL creation $lastsynced`
vdate=${date#????}
fi
fi
if ((printall)) && ((verbose)) ; then
# If we are printing out ALL filesystems,
# then we have to make sure left side is always
# "src filesystem", not "named filesystem"
# then we have to check what the src fs is
srcfs=`$ZFSGETVAL ${ZREPTAG}:src-fs $fs`
else
# Yes, okay, if -a is used, then
# technically, this isnt always "src".
# but it prints out right, so close enough :)
srcfs="$fs"
fi
if ((verbose)) ; then
desthost=`$ZFSGETVAL ${ZREPTAG}:dest-host $srcfs`
printf "%-25s->%-35s %s\n" $srcfs "$desthost:$destfs" "$vdate"
else
printf "%-52s " $srcfs
echo "last: $date"
fi
shift
done
}
_master_fs_names(){
zfs get -H -o name -s local ${ZREPTAG}:master "$@"
}
# convenience function to list only local filesystems for which we are
# zrep master for.
# In contrast, zrep_list, lists ALL zrep registered filesystem, at the moment.
#
# Annoyingly... it would be way faster if we could just stick with the
# pure "zfs get" implementation, but we also need to deal with the zone
# issue. When a single zfs filesystem is visible aross multiple zones,
# we dont want them all thinking they are master
#
# Durn. Individual validation required.
zrep_list_master(){
typeset srchost
for fs in `_master_fs_names "$@"` ; do
srchost=`$ZFSGETVAL ${ZREPTAG}:src-host $fs`
if [[ "$srchost" == "$Z_LOCAL_HOST" ]] ; then
echo $fs
fi
done
}
# Given ONE filesystem, print all zrep properties for it.
# Note that this is internal routine. we do not validate input.
list_verbose(){
echo $1:
# sneaky cheat: only user-set properties will
# match these 'source' types. So "grep zrep:" is not
# neccessary. Although we may pick up other user-set values,
# but that is not neccessarily a bad thing
zfs get -H -o property,value -s $PROPTYPES all $1
echo "last snapshot synced: `getlastsnapsent $1`"
}
# Note: called by both user, AND by zrep_status
#
# Usage:
# zrep_list [-v]
# zrep_list [-L]
# zrep_list [-v] fs1 fs2
#(also zrep_list -s which passes to zrep_list_snaps)
#
# list all zrep-initialized filesystems (NOT snapshots..)
# If no specific fs listed, will show master, AND received filesystems,
# unless -L given (in which case, only local masters will be shown)
#
# Normal output is one line per fs.
#
# -v gives all properties of each filesystem
# Give only one of -L or -v
#
zrep_list(){
typeset fslist="" verbose=0
# This works because we only set this property on the actual fs.
# "source type" on snapshots for this property is "inherited" not local
# or "received"
typeset printcmd="zfs get -H -o name -s $PROPTYPES ${ZREPTAG}:dest-fs"
case $1 in
-v)
verbose=1
printcmd=list_verbose
shift
;;
-L)
# reminder: cant have this, AND verbose.
printcmd="zrep_list_master"
shift
;;
-s)
shift
zrep_list_snaps "$@"
return
;;
esac
# If specific fs(s) named, iterate over them and quit
if [[ "$1" != "" ]] ; then
while [[ "$1" != "" ]] ; do
if zfs list -t filesystem,volume $1 >/dev/null 2>&1 ; then
$printcmd $1
else
zrep_errquit "Expecting filesystem, but got $1"
fi
shift
done
return
fi
# Must be "list all" now. But which output format?
# If not verbose, we have a nice shortcut to just list
# all filesystems that zrep has marked.
if (( $verbose == 0)) ; then
$printcmd
return
fi
# oh well. have to step through them one by one now, to
# echo out the properties associated with each zrep filesystem
fslist=`zfs get -H -o name -s $PROPTYPES ${ZREPTAG}:dest-fs`
for fs in $fslist ; do
$printcmd $fs
echo ""
done
}
# Similar to zrep_list, but lists SNAPSHOTS instead of filesystems
# The purpose is to allow a sysadmin to see easily when snapshots have
# been created.
# Either give a list of specific filesystems, or no args, which
# will attempt to list all zrep-related snapshots
# It will list only zrep MASTER filesystem snapshots, in that case.
zrep_list_snaps(){
if [[ "$1" == "" ]] ; then
set -- `_master_fs_names`
if [[ "$1" == "" ]] ; then
_errprint "No zrep master filesystems found"
return 0
fi
fi
while [[ "$1" != "" ]] ; do
zfs list -r -t snapshot -o name,creation $1
shift
done
}
# Given a filesytem name, prints out full snapshot name of last successfully synced snap
zrep_getlastsent(){
if [[ "$1" == "" ]] ; then
_errprint ERROR: zrep uptodate requires the name of a zrep managed fs
exit 1
fi
echo `getlastsnapsent $1`
}
# Give a filesystem name.
# Gets last sent snapshot, and determines if there have
# been any writes since then.
# If not, then file sytem is "up to date"
zrep_uptodate(){
if [[ "$1" == "" ]] ; then
_errprint ERROR: zrep uptodate requires the name of a zrep managed fs
exit 1
fi
typeset bytecount
bytecount=`$ZFSGETVAL written "$1"`
# two fail conditions are:
# 1. not zrep filesystem
# 2. system does not support "zfs get written"
# Either way we count it as "not up to date"
if [[ $bytecount == 0 ]] ; then
return 0
else
return 1
fi
}