<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Eric’s Substack: General Technology]]></title><description><![CDATA[Miscellaneous posts around tech in general.]]></description><link>https://blog.ecapuano.com/s/general-technology</link><image><url>https://substackcdn.com/image/fetch/$s_!lrkf!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c9bc99-8815-431a-8473-21ce5748124c_400x400.jpeg</url><title>Eric’s Substack: General Technology</title><link>https://blog.ecapuano.com/s/general-technology</link></image><generator>Substack</generator><lastBuildDate>Fri, 03 Apr 2026 21:40:34 GMT</lastBuildDate><atom:link href="https://blog.ecapuano.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Eric Capuano]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[ecapuano@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[ecapuano@substack.com]]></itunes:email><itunes:name><![CDATA[Eric Capuano]]></itunes:name></itunes:owner><itunes:author><![CDATA[Eric Capuano]]></itunes:author><googleplay:owner><![CDATA[ecapuano@substack.com]]></googleplay:owner><googleplay:email><![CDATA[ecapuano@substack.com]]></googleplay:email><googleplay:author><![CDATA[Eric Capuano]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Automatically Revert ESXi VM Snapshot on a Schedule]]></title><link>https://blog.ecapuano.com/p/automatically-revert-esxi-vm-snapshot</link><guid isPermaLink="false">https://blog.ecapuano.com/p/automatically-revert-esxi-vm-snapshot</guid><dc:creator><![CDATA[Eric Capuano]]></dc:creator><pubDate>Wed, 08 Feb 2023 18:44:18 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!lrkf!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c9bc99-8815-431a-8473-21ce5748124c_400x400.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://blog.ecapuano.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://blog.ecapuano.com/subscribe?"><span>Subscribe now</span></a></p><h2>Why is this useful?</h2><p>Let&#8217;s say you have a VM that you use for unsavory things like malware analysis and you would like to automatically revert it on a scheduled basis. There are many ways to go about this manually, but for my particular use-case, I wanted it done automatically everyday at 3AM. </p><p>While this can be accomplished a few different ways such as <a href="https://communities.vmware.com/t5/ESXi-Discussions/Independent-Persistent-Vs-Independent-Non-Persistent/td-p/2176432">non-persistent disks</a>, I found the snapshot revert method ideal for me because the machine stays up and on at all times except the brief moment the snapshot is being restored.</p><h2>Great, just tell me how to do it!</h2><p>In my example, I am using standalone ESXi 6.7 (no vCenter). There is a VM running that has a single snapshot called &#8220;Clean&#8221;. Of course, one could use the ESXi WebUI to revert the snapshot at will, but in order to get it to revert automatically on a schedule, I scripted up the following tasks in ESXi CLI and created a cronjob.</p><ol><li><p>Snapshot your VM in the WebUI</p></li><li><p>SSH to the ESXi server and run the following commands:</p><ol><li><p>Get your target VM ID with this command: </p><ol><li><p>vim-cmd vmsvc/getallvms</p></li></ol></li><li><p>Learn the snapshot ID of your VM with this command:</p><ol><li><p>vim-cmd vmsvc/get.snapshotinfo [VM_id]</p></li></ol></li><li><p>Create a cronjob that reverts this VM everyday at 3AM (<a href="https://crontab.guru/">customize as neccesary</a>)</p><ol><li><p>Backup existing crontab</p><ol><li><p>cp /var/spool/cron/crontabs/root /var/spool/cron/crontabs/root.old</p></li></ol></li><li><p>Edit running crontab</p><ol><li><p>vi /var/spool/cron/crontabs/root</p></li></ol></li><li><p>Add the new job on the last line, this job runs everyday at 3AM. The 0 at the end is to ensure the VM stays powered on after the snapshot is restored.</p><ol><li><p>*   3   *   *   *   /bin/vim-cmd vmsvc/snapshot.revert [VM_id] [snapshot_id] 0</p></li></ol></li><li><p>Restart cron service (yes, this is a odd way of doing it, but ESXi is weird like that)</p><ol><li><p>PID=$(cat /var/run/crond.pid)</p><p>echo "Old cron PID was $PID"</p><p>kill $PID</p><p>/usr/lib/vmware/busybox/bin/busybox crond<br>PID=$(cat /var/run/crond.pid)</p><p>echo "NEW cron PID is $PID"</p></li></ol></li><li><p>And you&#8217;re set! Your VM will now revert to the snapshot everyday at 3AM.</p></li></ol></li></ol></li></ol><p></p><p>Ok so now your VM will revert to this snapshot everyday at 3AM, but what happens if you create a new snapshot and want the cron to use the new snapshot ID? Luckily, I crafted a little script that will ensure that your latest snapshot ID is always used by the cron. </p><p><strong>NOTE</strong>: This script only works if there is <strong>ONE snapshot</strong> for the VM. Multiple snapshots will break it.</p><p>The script:</p><pre><code>#/bin/bash
# NOTE: This script only works if there is ONE snapshot for the VM.
# Multiple snapshots will break it.

# BE SURE TO CHANGE VM_id TO YOUR VM's ID!
VM_id=33

# get snapshot ID for VM
SNAPSHOTID=$(vim-cmd vmsvc/get.snapshotinfo $VM_id | grep id | egrep -o '[0-9]+')

# edit cron to reflect new snapshot id

sed -ri "s/$VM_id [0-9]+ 0/$VM_id $SNAPSHOTID 0/" /var/spool/cron/crontabs/root

# restart crond
PID=$(cat /var/run/crond.pid)
echo "Old cron PID was $PID"
kill $PID
/usr/lib/vmware/busybox/bin/busybox crond
PID=$(cat /var/run/crond.pid)
echo "NEW cron PID is $PID"
echo "New crontab contents:"
cat /var/spool/cron/crontabs/root
</code></pre><p>Now that we have a script that can automatically update the crontab when a new snapshot ID exists, we can use another cronjob to make sure this happens before the snapshot revert happens.</p><ol><li><p>Edit running crontab</p><ol><li><p>vi /var/spool/cron/crontabs/root</p></li></ol></li><li><p>Add the new job on the last line, you want this job to run before the revert job runs. This job runs everyday at 2AM.</p><ol><li><p>*   2   *   *   *   /path_to_above_script.sh</p></li></ol></li><li><p>Restart cron service</p><ol><li><p>PID=$(cat /var/run/crond.pid)</p><p>echo "Old cron PID was $PID"</p><p>kill $PID</p><p>/usr/lib/vmware/busybox/bin/busybox crond<br>PID=$(cat /var/run/crond.pid)</p><p>echo "NEW cron PID is $PID"</p></li></ol></li><li><p>And you&#8217;re set! Your VM will now revert to the snapshot everyday at 3AM, even if you delete and recreate a new snapshot!</p></li></ol><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.ecapuano.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Eric&#8217;s Substack! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item></channel></rss>