source: src-sh/lpreserver/lpreserver @ cb8f638

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since cb8f638 was cb8f638, checked in by Kris Moore <kris@…>, 15 months ago

Add "lpreserver status" command, which will show last snapshot / replication for all datasets

  • Property mode set to 100755
File size: 8.3 KB
Line 
1#!/bin/sh
2# Command-line interaction script for life-preserver
3# Author: Kris Moore
4# License: BSD
5# "Life-Preserver" Copyright 2013 PC-BSD Software (iXsystems)
6######################################################################
7
8# Source external functions
9. /usr/local/share/pcbsd/scripts/functions.sh
10
11require_root() 
12{
13  UTEST="`whoami`"
14  if [ "$UTEST" != "root" ] ; then
15     exit_err "This command must be run as root!"
16  fi
17}
18
19# Source our functions
20PROGDIR="/usr/local/share/lpreserver"
21
22# Source our local functions
23. ${PROGDIR}/backend/functions.sh
24
25title()
26{
27  echo "Life-Preserver
28---------------------------------"
29};
30
31
32# Function to display help information
33help_main()
34{
35  title
36  echo "Available commands
37
38Type in help <command> for information and usage about that command
39
40       help - This help file"
41
42if [ -e "/usr/local/bin/life-preserver" ] ; then
43   echo "        gui - Launch the GUI"
44fi
45
46cat<<__EOF__
47     mksnap - Create a ZFS snapshot of a zpool/dataset
48   listsnap - List snapshots of a zpool/dataset
49   listcron - Listing of scheduled snapshots
50   cronsnap - Schedule snapshot creation via cron
51 revertsnap - Revert zpool/dataset to a snapshot
52  replicate - Enable / Disable ZFS replication to a remote system
53     status - List datasets, along with last snapshot / replication date
54     rmsnap - Remove a snapshot
55__EOF__
56};
57
58help_replicate()
59{
60 title
61 echo "Help replicate
62
63Life-Preserver replication sub-system
64
65Replication uses ZFS to send your snapshots to a remote system which also has
66a ZFS pool, such as FreeNAS or another PC-BSD system. The <hostdataset> must
67also be scheduled for snapshots for replication to occur.
68
69On the remote system, you will need to have created the <remotedataset> beforehand.
70
71If you plan to replicate to a non-root user, please set the following permissions
72on the remote dataset:
73
74# zfs allow -u <user> create,receive,mount,userprop,destroy <remotedataset>
75
76Available Flags:
77        list - List replication targets
78         add - Add a new replication target
79      remove - Remove a replication target
80
81Add Options:
82
83        add <remotehost> <user> <port> <hostdataset> <remotedataset> <time>
84
85        Example 1:
86
87        add freenas.8343 backupuser 22 tank1/usr/home/kris tankbackup/backups sync
88
89        Will schedule replication of tank1/usr/home/kris to tankbackup/backups with each snapshot
90
91        Example 2:
92
93        add freenas.8343 backupuser 22 tank1 tankbackup/backups 22
94
95        Will schedule replication of tank1 to tankbackup/backups at 10PM, notated in 24hour time
96
97Remove Options:
98
99        remove <dataset>
100       
101        Remove a replication task indicated by <dataset>
102        Use 'replicate list' for a list of IDs.
103
104Usage:
105
106  lpreserver replicate <subcmd> <flags>
107
108Example:
109
110  lpreserver replicate
111"
112};
113
114
115help_set()
116{
117 title
118 echo "Help set
119
120Set Life-Preserver options
121
122Usage:
123
124  lpreserver set
125
126Config options
127
128     duwarn - Set to a disk percentage [0-99] at which to warn of low disk space
129
130      email - Set the e-mail address to receive notifications
131                This will require that the \"mail\" command is setup for outgoing mail
132
133  emailopts - (ALL/WARN/ERROR)
134                  ALL - Send mail for every snapshot, plus warnings and errors
135                 WARN - Send mail for warnings and errors (Default)
136                ERROR - Send mail for errors only
137
138  recursive - (ON/OFF)
139                   ON - Will create / destroy snapshots recursively, for all children of
140                        a target zpool/dataset. (Default)
141                  OFF - Will only create / destroy snapshots on the specified dataset. 
142
143Example:
144
145  lpreserver set email "kris@example.org,admin@example.org"
146
147  lpreserver set emailopts ALL
148"
149};
150
151help_get()
152{
153 title
154 echo "Help get
155
156Listing of Life-Preserver options set
157
158Usage:
159
160  lpreserver get
161
162Example:
163
164  lpreserver get
165"
166};
167
168help_listcron()
169{
170 title
171 echo "Help listcron
172
173List scheduled cron snapshots
174
175Usage:
176
177  lpreserver listcron
178"
179};
180
181help_status()
182{
183 title
184 echo "Help status
185
186List monitored dataset's last snapshot and replication
187
188Usage:
189
190  lpreserver status
191
192"
193};
194
195
196help_listsnap()
197{
198 title
199 echo "Help listsnap
200
201List ZFS snapshots of a dataset
202
203Usage:
204
205  lpreserver listsnap <dataset>
206
207Example:
208
209  lpreserver listsnap tank0/usr/home/kris
210"
211};
212
213help_cronsnap()
214{
215 title
216 echo "Help cronsnap
217
218Schedule a ZFS snapshot
219
220Usage:
221
222  For a listing of all scheduled snapshots
223
224  # lpreserver listcron
225
226  or
227
228  To start / stop snapshot scheduling
229
230  # lpreserver cronsnap <dataset> <action> <frequency> <numToKeep>
231 
232  action = start / stop
233  frequency = daily@XX / hourly / 30min / 10min / 5min
234                    ^^ Hour to execute
235  numToKeep = Number of snapshots to keep total
236
237Example:
238
239  lpreserver cronsnap tank1/usr/home/kris start daily@22 10
240
241  or
242
243  lpreserver cronsnap tank1/usr/home/kris stop
244"
245};
246
247help_revertsnap()
248{
249 title
250 echo "Help revertsnap
251
252Revert ZFS to a snapshot of a dataset
253
254Usage:
255
256  lpreserver revertsnap <dataset> <SNAP>
257
258Example:
259
260  lpreserver revertsnap tank1/usr/home/kris 20130729-140510
261"
262};
263
264help_rmsnap()
265{
266 title
267 echo "Help rmsnap
268
269Remove ZFS snapshot of a dataset
270
271Usage:
272
273  lpreserver rmsnap <dataset> <SNAP>
274
275Example:
276
277  lpreserver rmsnap tank1/usr/home/kris 20130729-140510
278
279NOTE:
280By default snapshots are recursive and will remove snapshots
281of all children datasets automatically. To change this use:
282
283# lpreserver set recursive off
284"
285};
286
287
288help_mksnap()
289{
290 title
291 echo "Help mksnap
292
293Create a new ZFS snapshot of a dataset
294
295Usage:
296
297  lupdate mksnap <dataset>
298
299Example:
300
301  lupdate mksnap tank1/usr/home/kris
302
303NOTE:
304By default snapshots are recursive and will create snapshots
305of all children datasets automatically. To change this use:
306
307# lpreserver set recursive off
308"
309};
310
311# Check if we need to enable the zfs monitor
312enable_watcher
313
314# Check what the user wants to do
315case "$1" in
316
317   help) case "$2" in
318           get) help_get ;;
319           set) help_set ;;
320           mksnap) help_mksnap ;;
321           listcron) help_listcron ;;
322           listsnap) help_listsnap ;;
323           revertsnap) help_revertsnap ;;
324           replicate) help_replicate ;;
325           cronsnap) help_cronsnap ;;
326           rmsnap) help_rmsnap ;;
327           status) help_status ;;
328           *) help_main ;;
329         esac  ;;
330
331    get) require_root
332         # Display our options 
333         title
334         echo "      Recursive mode: $RECURMODE"
335         echo "E-mail notifications: $EMAILMODE"
336         echo "    E-mail addresses: $EMAILADDY"
337         echo "  Disk space warn at: ${DUWARN}%"
338         exit 0
339         ;;
340
341    set) require_root
342         OPT="${2}" 
343
344         case $OPT in
345        emailopts) case $3 in
346                  ALL|WARN|ERROR) echo "E-Mail mode set to: $3" 
347                                  echo "$3" > ${DBDIR}/emaillevel ;;
348                     *) exit_err "Unknown option" ;;
349                   esac
350                   ;;
351            email) echo "Setting email notification to: $3" 
352                   echo "$3" > ${DBDIR}/emails ;;
353           duwarn) if [ ! $(is_num "$3") ] ; then exit_err "Invalid number, must be 0-99"; fi
354                   echo "Setting disk warning capacity to: ${3}%" 
355                   echo "$3" > ${DBDIR}/duwarn ;;
356        recursive) case $3 in
357                  OFF|Off|off)  echo "Recursive mode disabled" 
358                                touch ${DBDIR}/recursive-off ;;
359                     ON|On|on)  echo "Recursive mode enabled" 
360                                rm ${DBDIR}/recursive-off 2>/dev/null ;;
361                     *) exit_err "Unknown option" ;;
362                   esac
363                   ;;
364                *) exit_err "Invalid option!" ;;
365           esac
366         ;;
367
368   mksnap) require_root
369         DATASET="$2"
370         ${PROGDIR}/backend/zfsmksnap.sh "${DATASET}"
371         ;;
372
373 listcron) require_root
374         ${PROGDIR}/backend/zfslistcron.sh
375         ;;
376
377 listsnap) require_root
378         DATASET="$2"
379         ${PROGDIR}/backend/zfslistsnap.sh "${DATASET}"
380         ;;
381
382 cronsnap) require_root
383         DATASET="$2"
384         ${PROGDIR}/backend/zfscronsnap.sh "${DATASET}" "$3" "$4" "$5"
385         ;;
386
387revertsnap) require_root
388         DATASET="$2"
389         SNAP="$3"
390         ${PROGDIR}/backend/zfsrevertsnap.sh "${DATASET}" "$SNAP"
391         ;;
392
393    rmsnap) require_root
394         DATASET="$2"
395         SNAP="$3"
396         ${PROGDIR}/backend/zfsrmsnap.sh "${DATASET}" "${SNAP}"
397         ;;
398    status) listStatus
399         ;;
400 replicate) require_root
401            shift
402            case ${1} in
403                add) add_rep_task "$2" "$3" "$4" "$5" "$6" "$7" ;;
404                list) list_rep_task ;;
405                remove) cat ${REPCONF} | grep -q "^${2}:" 
406                        if [ $? -eq 0 ] ; then
407                           rem_rep_task "$2"
408                           echo "Removed replication task for $2"
409                        else
410                           exit_err "No such replication task for dataset $2"
411                        fi
412                        ;;
413                *) exit_err "Invalid option!" ;;
414            esac
415         ;;
416   *) help_main ;;
417esac
418
419exit 0
Note: See TracBrowser for help on using the repository browser.