simonkf
Contributor
Contributor

Remove old snapshots but filter by name

Hi. I'd like to use the built in workflow 'Remove Old Snapshots' except I'd like it to filter out snapshots that have a specific phrase in the snapshot name E.G do-not-delete

I thought it would be just a case of editing the scriptable task 'Get snapshots' and changing the line

if(days>numbrOfDay){

to something like

if(days>numbrOfDay && ss.name.substr(0,13) != "do-not-delete"){

but for the life of me I can't seem to determine the name attribute and the API reference isn't clear either.

Can anyone point me in the right direction?

Thanks

0 Kudos
12 Replies
ChristianWehner
VMware Employee
VMware Employee

Hi,

the function which here is used to identify all snapshots is: getAllSnapshotResultInDatastoreBrowser which does not return VcVirtualMachineSnapshot objects, it returns SnapshotReturnFiles to snapshot files on datastores. Here you could filter on the path to the snapshot but not on values stored within this snapshot. Later within this workflow, you has some objects of kind VcVirtualMachineSnapshot, but those don't have any attribute called name or description where you could use your code for.

I think it is simpler to build your own workflow.

As hint: within API search for VcVirtualMachineSnapshotTree instead of VcVirtualMachineSnapshot where you will find all the attributes you are searching for, I'm sure. Also VcVirtualMachineSnapshotTree is the type of object you get returned on VirtualMachine.snapshot.rootSnapshotList.

Hope this helps,

Chris

0 Kudos
simonkf
Contributor
Contributor

What I don't understand if that the output 'snapshots' of the script contains the values which are the names of the snapshots themselves.

Couldn't I filter these in another scriptable task?

0 Kudos
ChristianWehner
VMware Employee
VMware Employee

You are right, this is caused by vCO. What you see is a vCO inventory object (the objects you can browse within your inventory tab within vCO. If you take a look to the API reference this are the gray instead of the green objects) of VcVirtualMachineSnapshot which has the attributes: name, vimHost, id and virtualMachineName, but you never can work with this inventory object within one of your actions or workflows. Within your actions and workflows you work with scripting objects and the scripting object of VcVirtualMachineSnapshot does not provide the name of the snapshot and also not the description.

You could do something like: Building a separate scriptable task with the snapshots as input. And then search the inventory for a object of type VcVirtualMachineSnapshotTree where the VcVirtualMachineSnapshot.id matches the VcVirtualMachineSnapshotTree.id:

var newSnapshotArray = new Array() ;

for each ( var mySnapshot in snapshots ) {

   var myVcVirtualMachineSnapshotTree = Server.findForType( "VcVirtualMachineSnapshotTree",mySnapshot.id );

   if ( myVcVirtualMachineSnapshotTree.name.search( "do-not-delete" ) == -1 ) { // do-not-delete is not part of the snapshot name

       newSnapshotArray.push( mySnapshot );

   }

}

snapshots = newSnapshotArray;

Then set snapshots as input and also as output to the scriptable task and this should work. But this code is untested from my side.

BR,

Chris

simonkf
Contributor
Contributor

Ah I understand your logic.Thanks for your help.

I tried the script but I received the error: Server error : Unknown object type 'VcVirtualMachineSnapshotTree'

0 Kudos
ChristianWehner
VMware Employee
VMware Employee

Try

var myVcVirtualMachineSnapshotTree = Server.findForType( "VC:VirtualMachineSnapshotTree",mySnapshot.id );


instead of


var myVcVirtualMachineSnapshotTree = Server.findForType( "VcVirtualMachineSnapshotTree",mySnapshot.id );

0 Kudos
simonkf
Contributor
Contributor

Unfortunately I had already tried that

Server error : ch.dunes.model.sdk.SDKNoSuchTypeException: VC:VirtualMachineSnapshotTree

0 Kudos
ChristianWehner
VMware Employee
VMware Employee

... OK then we need to change our behavior. We build our own snapshot search scriptable task which will be easier, than searching here for a way of changing the current workflow this. Untested but maybe the thing you need:

var xpath = "xpath:name[not(contains(text(), '(do-not-delete)'))]";

var snapshots = VcPlugin.getAllVirtualMachineSnapshots( null,xpath );

Regards,

Chris

0 Kudos
simonkf
Contributor
Contributor

Thanks Chris

Won't that return VcVirtualMachineSnapshot? So i have the same issue as before?

Also how would I extract creation time now that I am not using the per-defined workflow?

0 Kudos

Mybe this is the way you need it to exclude your string in the Snapshot List:

--------------------------------------------------------

snapshots = new Array();

var snapshotsTemp = VcPlugin.getAllVirtualMachineSnapshots( );

var newSnapshotArray = new Array() ;

for each ( var mySnapshot in snapshotsTemp ) {

   if ( mySnapshot.name.search( snapshotName ) == -1 ) {

       newSnapshotArray.push( mySnapshot );

   }

}

snapshots = newSnapshotArray;

----------------------------------------------------------

Be aware of this post for Snapshot removal:

Re: Virtual Machine Disks consolidation is needed

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
simonkf
Contributor
Contributor

Thanks. That's helped me with filtering by name but discovering age is still an issue.

0 Kudos

But you can use the original "Get snapshots" scriptable task in the "Remove old Snapshots" Workflow in the vCenter library. And then write the result with the additional exclusion to a new array.

It’s not the nicest solution but is should work.

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
0 Kudos

You have to test it, but this should work:

snapshotsTemp = new Array();

var searchResults = System.getModule("com.vmware.library.vc.vm.snapshot").getAllSnapshotResultInDatastoreBrowser(false,false,false,true) ;

var instance = new VcManagedObjectReference();

instance.type = "ServiceInstance";

instance.value = "ServiceInstance";

var instanceObject = null;

var dateNow;

var timeForDateNow;

var timeForDateModif;

var diff;

var days;

var ss;

for (var i in searchResults) {

  var files = searchResults[i].file;

  for (var j in files) {

  ss = snapshotProperties.get(searchResults[i].folderPath+files[j].path);

  if(ss){

  if(instanceObject==null){

  instanceObject = VcPlugin.convertToVimManagedObject(ss , instance);

  }

  dateNow = instanceObject.currentTime();

  timeForDateNow = dateNow.getTime();

  timeForDateModif = files[j].modification.getTime();

  diff = timeForDateNow-timeForDateModif;

  days = diff/86400000;

  if(days>numbrOfDay){

  snapshotsTemp.push(ss);

  System.log("The snapshot "+searchResults[i].folderPath +files[j].path+" of the VM "+ss.config.name+" had "+Math.floor(days)+" days");

  snapshotProperties.remove(searchResults[i].folderPath+files[j].path);

  }

  }

  }

}

snapshots = new Array();

var newSnapshotArray = new Array() ;

for each ( var mySnapshot in snapshotsTemp ) {

   if ( mySnapshot.name.search( SnapshotName ) == -1 ) {

       newSnapshotArray.push( mySnapshot );

   }

}

snapshots = newSnapshotArray;

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
0 Kudos