ESXi 5.1 Command line Create Snapshots

Posted on Tuesday, April 7, 2015



 
 I have an ESXi 5.1.0, I make use of it a lot.  Typically I am using VSphere 5.5 as my only view/control into the ESXi box.

I have started playing around with the command line tools on the ESXi box.

In this post I am going to try and figure out if I can list the snapshots per VM, figure out the last snapshot taken of a VM, and take a new Snapshot of a VM.
 




Snapshots


First get a list of all the VMs on your ESXi box and their VMIDs.


  > vim-cmd vmsvc/getallvms | awk '{printf("%-5s%-30s\n",$1,$2)}'






I am going to just look at one VM the one with VMID = 34





List all the snapshots of a VM


List all the snapshots for a VM


  > vim-cmd vmsvc/snapshot.get 34




Get the Epoch Time of the last Snapshot


This one liner gets a little more complicated.  I want to get the timestamps for all the snapshots, then convert the time to epoch, then sort it in reverse order and grab the first (most recent snapshot)


  > vim-cmd vmsvc/snapshot.get 34 | grep "Snapshot Created" | awk '{split ($5,a,"/"); print a[3] "-" a[1] "-" a[2] "-" $6}'  | while read i; do date +"%s" -d $i; done | sort -r | head -n 1





Let me double check to make sure this lines up right.  My newest snapshot was made on 3/27/2015  00:19:42  UTC time

Checking the timestamp at http://www.epochconverter.com/ [1]




Looks good!






How many days old is the last snapshot?

  

  > vim-cmd vmsvc/snapshot.get 34 | grep "Snapshot Created" | awk '{split ($5,a,"/"); print a[3] "-" a[1] "-" a[2] "-" $6}'  | while read i; do epoch=`date +"%s" -d $i`; num=`date +"%s"`; sum=`expr $num - $epoch)`; days=`expr $sum / 86400`;echo $days; done |sort | head -n 1





My last snapshot is 2 days old (but not quite 3)






Shutdown VM 34 and take a new snapshot 

I like to take snapshots of shutdown VMs, I thought I would figure out how to automate this process via the command line.


  > vim-cmd vmsvc/power.off 34
  > vim-cmd vmsvc/snapshot.create 34 "SnapShot For Blog"
  > vim-cmd vmsvc/power.on 34








Looking at my snapshots from vSphere I can see it did take a snapshot







If I check how many days since my last snapshot was taken


  > vim-cmd vmsvc/snapshot.get 34 | grep "Snapshot Created" | awk '{split ($5,a,"/"); print a[3] "-" a[1] "-" a[2] "-" $6}'  | while read i; do epoch=`date +"%s" -d $i`; num=`date +"%s"`; sum=`expr $num - $epoch)`; days=`expr $sum / 86400`;echo $days; done |sort | head -n 1




I get back 0, as expected (since I just made this snapshot).




Removing Snapshots


There is a snapshot.remove command.  Let me see how it works.

First list all the snapshots for a VM


  > vim-cmd vmsvc/snapshot.get 34








I am going to remove this snapshot, number 4.  To remove it from VMID 34 run this command.


  > vim-cmd vmsvc/snapshot.remove 34 4




It took a few moments, but it removed it then listed all the current snapshots.





References


[1]        Epochtime converter
            http://www.epochconverter.com/

            Accessed 3/2015

No comments:

Post a Comment