Welcome to bytebang » The blog about all and nothing » Create snapshots via commandline in ESXi 5.5

Create snapshots via commandline in ESXi 5.5

Apr 10 2016

The Problem

I wanted to play around with automation of things inside my ESXi server. Automated snapshots are one of these things that can be useful if you can automate them - so I started researching how to do it. Here is the result !

The Solution

The solution is simple: esxcli is a tool which is built exactly for such a purpose. It can be accessed from within the command line of the hypervisor (so don't forget to enable SSH to be able to log into the machine).

/vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0 # esxcli
Usage: esxcli [options] {namespace}+ {cmd} [cmd options]

Options:
  --formatter=FORMATTER
                        Override the formatter to use for a given command. Available formatter: xml, csv, keyvalue
  --debug               Enable debug or internal use options
  --version             Display version information for the script
  -?, --help            Display usage information for the script

Available Namespaces:
  device                Device manager commands
  esxcli                Commands that operate on the esxcli system itself allowing users to get additional information.
  fcoe                  VMware FCOE commands.
  graphics              VMware graphics commands.
  hardware              VMKernel hardware properties and commands for configuring hardware.
  iscsi                 VMware iSCSI commands.
  network               Operations that pertain to the maintenance of networking on an ESX host. This includes a wide variety of commands to manipulate virtual networking components (vswitch, portgroup,
                        etc) as well as local host IP, DNS and general host networking settings.
  sched                 VMKernel system properties and commands for configuring scheduling related functionality.
  software              Manage the ESXi software image and packages
  storage               VMware storage commands.
  system                VMKernel system properties and commands for configuring properties of the kernel core system.
  vm                    A small number of operations that allow a user to Control Virtual Machine operations.
  vsan                  VMware VSAN commands.

It can be used for different purposes. Here a re a few examples:

List all virtual machines

/vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0 # esxcli vm process list
office.bytebang.local
   World ID: 2888116
   Process ID: 0
   VMX Cartel ID: 2888115
   UUID: 56 4d c3 d9 78 11 87 21-ea 8b fe f8 c0 36 c9 c7
   Display Name: office.bytebang.local
   Config File: /vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0/office.bytebang.local/office.bytebang.local.vmx

machine01.bytebang.local
   World ID: 3742262
   Process ID: 0
   VMX Cartel ID: 3742261
   UUID: 56 4d 21 1b f4 48 13 3a-7d 2a b2 0c 73 0f fd e6
   Display Name: machine01.bytebang.local
   Config File: /vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0/machine01.bytebang.local/machine01.bytebang.local.vmx

monitoring
   World ID: 3063343
   Process ID: 0
   VMX Cartel ID: 3063342
   UUID: 56 4d e6 2f be e1 63 7a-74 ec 98 fc 72 52 98 37
   Display Name: monitoring
   Config File: /vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0/monitoring/monitoring.vmx

machine02
   World ID: 442364
   Process ID: 0
   VMX Cartel ID: 442361
   UUID: 56 4d 16 60 47 17 ff 13-81 cd 73 87 34 6e fd ac
   Display Name: machine02
   Config File: /vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0/machine02/machine02.vmx

SERVER.bytebang.local
   World ID: 6547812
   Process ID: 0
   VMX Cartel ID: 6547811
   UUID: 56 4d 3a f3 12 d7 76 4d-f1 d3 cd be 0e 85 88 e3
   Display Name: SERVER.bytebang.local
   Config File: /vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0/SERVER.bytebang.local/SERVER.bytebang.local.vmx

or ... if you prefer it more compact

/vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0 # vim-cmd vmsvc/getallvms
Vmid         Name                                File                                           Guest OS               Version    Annotation
3      machine02                [midperf] machine02/machine02.vmx                               windows8Server64Guest   vmx-09
5      office.bytebang.local    [midperf] office.bytebang.local/SRVADE.bytebang.local.vmx       windows7Server64Guest   vmx-08
7      monitoring               [midperf] monitoring/monitoring.vmx                             ubuntu64Guest           vmx-08  
8      machine01.bytebang.local [midperf] machine01.bytebang.local/machine01.bytebang.local.vmx winNetEnterpriseGuest   vmx-08
9      SERVER.bytebang.local    [midperf] SERVER.bytebang.local/SERVER.bytebang.local.vmx       windows7Server64Guest   vmx-09

The interesting thing here is the vmid - which is an unique identifier for a virtual machine.

List snapshots for a machine

To list the snapshots for a machine you have to provide the vmid of the machine.

/vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0 # vim-cmd vmsvc/snapshot.get 7
Get Snapshot:
|-ROOT
--Snapshot Name        : before cabeling
--Snapshot Id          : 14
--Snapshot Desciption  :
--Snapshot Created On  : 2/2/2016 9:15:29
--Snapshot State       : powered on

We can see that we habe a single snapshot with the id 14, labelled 'before cabeling'.

Take a snapshot

To take a snapshot from the actual state you have to provide the vmid of the machine and a name for the new snapshot:

/vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0 # vim-cmd vmsvc/snapshot.create 7 test
Create Snapshot:

/vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0 # vim-cmd vmsvc/snapshot.get 7
Get Snapshot:
|-ROOT
--Snapshot Name        : before cabeling
--Snapshot Id          : 14
--Snapshot Desciption  :
--Snapshot Created On  : 2/2/2016 9:15:29
--Snapshot State       : powered on
--|-CHILD
----Snapshot Name        : test
----Snapshot Id          : 15
----Snapshot Desciption  :
----Snapshot Created On  : 4/5/2016 12:18:40
----Snapshot State       : powered off

Here we created a new snapshot named 'test' which is the child of our old snapshot. Of course you can provide more details how the hypervisor should create the snapshot, but i wanted to keep it simple.

Delete snapshots

Finally you can delete snapshots as well. Therefore you have to provide the vmid and the snapshotid of the snapshot which should be removed:

/vmfs/volumes/5693d9a9-3ad56f7a-8c5e-901b0e6ab3b0 # vim-cmd vmsvc/snapshot.remove 7 15
Remove Snapshot:
|-ROOT
--Snapshot Name        : before cabeling
--Snapshot Id          : 14
--Snapshot Desciption  :
--Snapshot Created On  : 2/2/2016 9:15:29
--Snapshot State       : powered on

Thats it - have fun !

Get Social


(c) 2024, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen
-