Script to cleanup snapshot list.

Status
Not open for further replies.

Apollo

Wizard
Joined
Jun 13, 2013
Messages
1,458
I have different snapshot creation and retention capability meaning that some snapshot will be taken at different interval and be retained over a different timeframe.
Overtime, snapshots are increasing in number until they expire.
When I do complete replication over a new volume, either localy or over the network, each snapshot will be sent over until replication is complete.
If time is not the issue, then replication will take place.
The issue is that snapshot which do not record any changes in the dataset are still created and the number can be significant.
I have noticed that the replication of snapshots pointing to little or no change are going to increase the replication time.
To speed this up, I would like to destroy snapshots that do not record changes in the dataset.

In order to see if a snapshot is pointing to change in dataset, I would run the following command:

zfs diff volume@snap1 volume@snap2
If nothing is returned, then volume@snap2 can be destroyed.

I have been doing such tedious work without much automation, and it is prone to user errors and mistakes.

Can anybody suggest a script that would do the following or similar:

Run following command to create the list of snapshots with emphasis on string search (ie: searching for "-2d" only for snapshots with 2 days retention ): This one doesn't really need to be part of a script
zfs list -t snapshot -o name -r volume | grep "string to isolate" > snapshot-list.txt

snapshot-list.txt contains the list of all the snapshots satisfying the search criteria.

The script should be able to read and process snapshot-list.txt in order to do the following:

- zfs diff should be run based on the sanpshot list. If a snapshot doesn't return any changes to the dataset, then the snapshot should be stored in a file for further processing such as snapshot-to-destroy.txt.

- When zfs diff is complete, a command should be able to read the name of the snapshots stored within snapshot-to-destroy.txt and run the following command until the last snapshot in the file has been processed:

zfs destroy snap1
zfs destroy snap2
...

At the end, the script should have destroyed all the snapshots listed in the snapshot-to-destroy.txt file.

Dealing with files for storing snapshots to destroy is an extra step I think is crucial as it can be looked at before it can be serviced.

Thanks.
 

bigphil

Patron
Joined
Jan 30, 2014
Messages
486
I've been using this script for years and I'm sure it wouldn't take you too much work to adjust it to do what you'd like. The groundwork is already done.
 

Apollo

Wizard
Joined
Jun 13, 2013
Messages
1,458
I've been using this script for years and I'm sure it wouldn't take you too much work to adjust it to do what you'd like. The groundwork is already done.

Thanks Bigphil,

I wish there was something better though.
I guess I now have to learn Python.

Thanks.
 
Status
Not open for further replies.
Top