Requirement:
There is no default feature available in Vcenter to schedule snapshot deletion. We need to automate snapshot deletion of VM.
Pre-requisite:
- SSH service should be running on ESXi host.
- Target VM should be run on pre-defined host.
- One Linux system with cron job running.
Step 1:
Enable ssh service on ESXi host.
Login to Vcenter > Select host > Configuration > Security Profile > Services > Select SSH > Options > Start > OK
Step 2:
Allow SSH access to ESX/ESXi hosts with public/private key
i). Generate public/private keys on Linux system.
$ssh-keygen -t rsa
These will generate 2 files in ~/.ssh: id_rsa and id_rsa.pub.
ii). On the remote host, store id_rsa.pub (the public key content) in ~/.ssh/authorized_keys.
For ESXi 5.0, the location of authorized_keys is: /etc/ssh/keys-<username>/authorized_keys
iii). To allow root access, change PermitRootLogin no to PermitRootLogin yes in the /etc/ssh/sshd_config file. By default it is yes.
iv). To disable password login, ensure that ChallengeResponseAuthentication and PasswordAuthentication are set to no. It is optional.
- v. Reload the service with the command:
$/etc/init.d/SSH start
Step 3:
Create snapshot-del.sh script on ESXi host.
#!/bin/bash
# name : snapshot-del.sh
# date : 10-22-2013
# revision : 1.0
# description : This script removes snapshot if the count is greater than 2
VM_ID=11
SNAP_COUNT=`vim-cmd vmsvc/snapshot.get $VM_ID|grep 'Snapshot Id'|wc -l`
if [ "$SNAP_COUNT" -gt 2 ]; then
echo "Deleting snapshot"
SNAP_DEL=`vim-cmd vmsvc/snapshot.get $VM_ID|grep 'Snapshot Id'|head -1|awk '{print $4}'`
echo $SNAP_DEL
vim-cmd vmsvc/snapshot.remove $VM_ID $SNAP_DEL
echo "snapshot $SNAP_DEL deleted"
else
echo "Snapshot count not greater than 2"
fi
Step 4:
Create a script on Linux system to run from cron
#!/bin/bash
echo `date +%a%t%d%t%b%t%Y%t%T` >> /opt/vmware/script/snapshot-del.log
ssh 'IP of the ESXi host' 'Full path of the script' >> /opt/vmware/script/snapshot-del.log
echo >> /opt/vmware/script/snapshot-del.log