Merge VirtualBox Snapshots via Bash Script

Use case

My backup script automatically creates daily snapshots of each VM images (for incremental backups). Thus I have a huge amount of snapshots after a while. Unfortunately the VirtualBox-UI does not provide a method of removing/mergin all snapshots at once.

Solution

So I wrote a small script that is doing this job for me (and configured it via cronjob to be run once a week).
The code must be executed as sudo.

# Hello World
echo '========================================'
echo '= THIS SCRIPT COMES WITH NO WARRANTY   ='
echo '= Bash version '${BASH_VERSION}'       ='
echo '= [CTRL] + [Z] to kill process         ='
echo '========================================'

# Exit with message and error code
function failure() {	
	echo ''
	echo 'ERROR:'$1
	echo ''
	exit 1
}

# Check whether this script got called as super user. If not, we can not 
# proceed.
if [ "$(whoami)" != "root" ]; then
	failure 'Aborting: You must be root to execute this script.'
fi
  
# Get all users
declare -a users 	
tmp_users="$( getent passwd | grep /home/ | cut -d ':' -f 1 )"
for u in ${tmp_users}; do
	tmp_dir="/home/${u}"
	if [ -d "$tmp_dir" ]; then
		users+=(${u})
	fi
done	

# Do delete/merge all snapshots
for ((i=0;i<${#users[@]};i++)); do

	echo "> Processing user: ${users[i]}"

	# Get all installed VMs
	vms=$(su -c "VBoxManage list vms | sed -E 's/^\"(.*)\".*/\1/g'" -s /bin/sh ${users[i]})

	# Create new snapshot of each
	for vm_name in $vms; do 
		
		echo "> > Processing VM ${vm_name}.."

		# Loop through each snapshot and delete/merge it
		snapshots=$(su -c "VBoxManage showvminfo ${vm_name} --machinereadable | grep SnapshotName | cut -d '\"' -f2" -s /bin/sh ${users[i]})
		for snapshot_name in $snapshots; do 
			echo "> > > Merging snapshot: ${snapshot_name}.."
			snapshots=$(su -c "VBoxManage snapshot ${vm_name} delete ${snapshot_name}" -s /bin/sh ${users[i]})
		done
	done
	
done

Conclusion

The script tries to remove all snapshots of all images of all users.
In VirtualBox terms “removing an snapshot” actually means that they get merged into the main virtual file – without any loss of data.

 

Tagged with: , ,
Posted in Ubuntu, VirtualBox
9 comments on “Merge VirtualBox Snapshots via Bash Script
  1. It is easy to work in fabricating and thermoforming. XLPE foams work well in the building and construction industry where thermal insulation, sound and vibration are critical, due to its low moisture permeability and high buoyancy.

  2. Are you not in a position to drive your car? Hiring a safe driver Dubai is the best thing to do if you are in Dubai and Awesome Drive is the best safe Driver Company. Awesome Drive welcomes you to a chauffeur-driven service. It is a driver company operating in Dubai.

  3. Looking for the perfect family getaway in Dubai? Look no further than the Family Hotel Apartment with Burj Khalifa view at Royal Continental Suites in Business Bay. This luxurious accommodation offers stunning views of the iconic Burj Khalifa, allowing you to marvel at its beauty from the comfort of your own living room. The spacious and elegantly furnished apartment is designed to cater to families, providing ample space for everyone to relax and unwind. With modern amenities and a fully equipped kitchen, you can enjoy home-cooked meals while taking in the breathtaking cityscape outside your window. Whether you’re looking for a peaceful retreat or a fun-filled adventure, this hotel apartment has everything you need for an unforgettable family vacation experience.
    https://royalcontinentalhotels.com/rcs/family-hotel-apartment-with-burj-khalifa-view/

  4. Experience the ease of sending smiles with our flower and gift delivery services across the UAE. Whether you’re looking to order flowers for same day delivery in Abu Dhabi, Dubai Marina, or any other location, our top-rated online flower shop ensures timely delivery.
    https://blushpetals.ae/collections/shop-all

  5. Are you looking for an effective thermal insulation solution that can withstand extreme temperatures and harsh weather conditions? Then XLPE Thermal Insulation Sheets might just be what you need! Cross-linked polyethylene, or XLPE, is a versatile insulation material widely used in various industries due to its exceptional properties. From electrical cables to HVAC systems, there are numerous applications of XLPE Thermal Insulation Sheets that make it a go-to choice for many businesses worldwide. In this blog post, we will dive into the world of XLPE and explore its benefits, uses, and applications. So let’s get started!

    https://airmastertape.com/xlpe-thermal-insulation-sheets-application/

  6. Moving to a new state is often tricky as you need to get settled down. No home is complete without television so you better look for the best “cable service near me” so you can spend some high-quality leisure time with new friends and family.

  7. Bulk Texting says:

    Transactional SMS have increased exponentially in the last few years. Banks, stores, and companies send out notifications and alerts to users or customers or employees on a daily basis. They connect their systems to a texting service using a SMS gateway.

  8. This blog post really grabbed my attention. With that said I am going to subscribe. Therefore I will get more updates on what you have to say. Please keep writing as I want to learn more.

Leave a Reply to xlpe foam insulation Cancel reply

Your email address will not be published. Required fields are marked *

*

Anti-spam protection

Prove that you are Human by typing the emphasized characters:


Protected by Gab Captcha 2

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>