1 -- Recursively snapshot every dataset with a given property
3 -- Usage: zfs program <pool> autosnap.lua -- [-n] [-p <property>] <snapshot>
12 usage: zfs program <pool> autosnap.lua -- [-n] [-p <property>] <snapshot>
14 -n: performs checks only, does not take snapshots
15 -p <property>: property to check. [default: com.sun:auto-snapshot]
16 <snapshot>: root snapshot to create [example: tank/data@backup]
19 property
= "com.sun:auto-snapshot"
23 for i
, arg
in ipairs(argv
) do
26 elseif arg
== "-p" then
27 elseif argv
[i
-1] == "-p" then
34 if root_snap
== nil or property
== nil then
40 for i
= 1, #root_snap
do
41 if root_snap
:sub(i
, i
) == "@" then
42 root_ds_name
= root_snap
:sub(1, i
-1)
43 snap_name
= root_snap
:sub(i
+1, root_snap
:len())
47 function auto_snap(root
)
48 auto
, source
= zfs
.get_prop(root
, property
)
49 if auto
== "true" then
50 ds_snap_name
= root
.. "@" .. snap_name
53 err
= zfs
.check
.snapshot(ds_snap_name
)
55 err
= zfs
.sync
.snapshot(ds_snap_name
)
57 results
[ds_snap_name
] = err
59 for child
in zfs
.list
.children(root
) do
64 auto_snap(root_ds_name
)
66 for ds
, err
in pairs(results
) do
68 err_txt
= err_txt
.. "failed to create " .. ds
.. ": " .. err
.. "\n"