Is there a way to use the -exec command to perform some automated action on ZFS snapshots?

Status
Not open for further replies.

Apollo

Wizard
Joined
Jun 13, 2013
Messages
1,458
I have found some information related to the use of the -exec command associated with "find".

The syntax of the command would be like the following:

Code:
find ~/ -iname readme.txt -exec cat '{}' \;


What this does is simply finding any files named "readme.txt" under "~/" folder. Every instanced found will be displayed using the "cat" command.

I figured, I could use the similar syntax for a different purpose.
What I am trying to do is to list all the snapshots on my Freenas machine that would have a specific flag set.

This is useful when it comes to deleting snapshots that have been placed under "hold", which would prevent deletion of the snapshot.

The commands I have been using are as follow:

Code:
zfs list -t snapshot -o name | grep -i dataset -exec zfs holds '{}' \;
zfs list -t snapshot -o name | grep -i dataset exec zfs holds '{}' \;
zfs list -t snapshot -o name  -exec zfs holds '{}' \;
zfs list -t snapshot -o name  exec zfs holds '{}' \;
zfs list -t snapshot -o name  |exec zfs holds '{}' \;


The last one is causing Putty to crash.

I can't seem to understand the logic behind the use of the -exec command.
Does it only work with the "find" command?
The idea is to list all the snapshots on the pool or simply target a single dataset and return the holds status.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
-exec isn't a command, it's an option of the find command.

Can you provide a sample list of snapshots you want to do something on and others where you don't want?
 

Apollo

Wizard
Joined
Jun 13, 2013
Messages
1,458
-exec isn't a command, it's an option of the find command.

Can you provide a sample list of snapshots you want to do something on and others where you don't want?
That makes sense.
I don't have a particular list of snapshot. Any snapshots will do, but to be specific, I would target all the snapshots of a specific dataset.
A few times I was unable to either delete a dataset or delete a snapshot because one of the flags were set.
The GUI is not easy to work with as the filters are not permanent.

If -exec is an option to the "find" command, then what route should I take? Should I look into some kind of script?
Ideally, I would think to pass the output of the "zfs list -t snapshot -o name" as argument to the "zfs holds insert_snatpshot_name_here" to display the holds status of the snapshot.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
I made a script: http://pastebin.com/A8fV9MqZ

It's untested because, well, I can't test it. But it's simple enough so hopefully I didn't make any mistakes.

You might want to run the command zfs list -t snapshot -o name | grep "your_dataset_name" alone before running the script to check that you have what you want in the list ;)
 

Apollo

Wizard
Joined
Jun 13, 2013
Messages
1,458
I made a script: http://pastebin.com/A8fV9MqZ

It's untested because, well, I can't test it. But it's simple enough so hopefully I didn't make any mistakes.

You might want to run the command zfs list -t snapshot -o name | grep "your_dataset_name" alone before running the script to check that you have what you want in the list ;)
Thanks.
Works great but for dataset and or snapshots containing space in their name it cause the name to be split causing to return the following errors:
bla-bla-bla is not a snapshot
no datasets available

What could be the work around to have the ${snapshot} parameter include the entire snapshot name including the space character?
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Well, don't use spaces in the names... Just kidding :D

I corrected the script in principle ;)
 

Apollo

Wizard
Joined
Jun 13, 2013
Messages
1,458
Well, don't use spaces in the names... Just kidding :D

I corrected the script in principle ;)
I tried your suggestion and still gives me the same outcome.

Using the following command returns the content of the list correctly . The snapshot names containing spaces are preserved.

Code:
echo "${snapshots}"


I found some examples online and in some instances I have read that the "for ... in ..." loop will take the words in the list separated by spaces.

The solution should be to create the list inserting the "" as it creates it for every entry or maybe use a different mean of erformig the loop function.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Yeah, the shell for() is cool because you can throw at it a list separated with spaces, newlines, tabs, ... or an array and it will iter on it automatically. The downside is that you have some cases (like this one) where you can't throw anything without much care :)

I'll see what I can do ;)
 

Apollo

Wizard
Joined
Jun 13, 2013
Messages
1,458
I just noticed under your signature the links to your threads. Very nice.
I can see the French touch.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Fixed and tested (the only thing I haven't tested is the zfs holds) ;)

FYI the IFS variable is the internal field separator that is used for splitting, by default it contains space, tab and newline so by assigning only the newline char the for iterate only on lines :)

Thanks ;) the French touch?
 

Apollo

Wizard
Joined
Jun 13, 2013
Messages
1,458
It does the trick nicely.
Do you mind sharing where you get those type of information? Are there any reading material you would recommend or is this knowledge you have acquired?
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Google :D

No, really, I search extensively when I don't know something (and then if I can't find the answer I ask on forums).

My background is electronics and then IT. I learned most of what I know (especially in electronics) by myself by experimenting and searching (then I learned that schools aren't good at teaching you something... there is 5-10 min videos on youtube who teach you more and better than 1 or 2 hours class period...). So there isn't some general good material, however for more precise topics I have some bookmarked links that I consider excellent.

But, back to the question: for everything related to programming, scripting, OS, ... I think one of the best source of info is stackoverflow (and serverfault for system oriented questions). For the last problem for example I used this search https://www.google.com/search?q=sh script for iterate lines spaces and then looked at the results with stackoverflow in the URL first (if I can't find the info here I look at the others results) ;) the secret is "how to do a good request?", with the experience (and sometime a bit of trial/error: too much results with something you don't want? add the keyword you don't want with '-' before, like -this for example) you learn how to put the right keywords in the search bar.
 
Last edited:
Status
Not open for further replies.
Top