The following is how I copy .vmdk files and the .vmx files from one datastore to another. This is used to backup all of my vms.

Before starting any of the below steps I went ahead and made environment variables to help shorten the file. This is super simple to do here are the commands. Just change the DTSTORE to the name of your current vm working datastore. Then change the ARSTORE to the datastore that you wish to use to hold the backups. Also in the ARSTORE location note that I have made a folder called VM-TEMP this is just my wording for location of working backup.

export DATA_SOURCE="/vmfs/volumes/DTSTORE"
export DATA_DEST="/vmfs/volumes/ARSTORE/VM-TEMP"

Making a folder in the temporary location named the current date.

mkdir "$DATA_DEST"`date +%b%d%Y`

Remove / Consolidate the current snapshots you have of vms on your ESXI system. Run the following command to get output of current vm numbers.

Command= vim-cmd /vmsvc/getallvms

Then enter this line for every vm that you would like to make a backup for changing the ending number to a number form the above command.

vim-cmd vmsvc/snapshot.removeall 1

At this point we need to make a home folder for each vm that is being backed up. In this example im going to use vm1 as a folder name for the “1” form the snapshot taken above. Do this for each vm you are making a backup of.

mkdir "$SESI_DEST"/`date +%b%d%Y`/VM1/

Now we need to make one snapshot of the vms that you are wanting to copy. We are doing this so that the vms can be running during the backup process. Just note that at this point is where the backup will take pace in time.

vim-cmd vmsvc/snapshot.create 1 BackupINPROG

Below is the line that will copy the vm into the temp backup location. At this point change VM1 to the name of your vm folder.. Next change VM1.vmdk to the name of your base vmdk file not the snapshot.

vmkfstools -i "$SESI_SOURCE"/VM1/VM1.vmdk "$SESI_DEST"/`date +%b%d%Y`/VM1/VM1.vmdk -d thin''

Remove / Consolidate the current snapshots you have of vms on your ESXI system. We are doing this again so that the vm can continue its normal life after the copy process and to change the vmx files name of the vmdk back to its root name so that when you make a copy of vmx its not looking for the snapshot we took just a few steps back.

vim-cmd vmsvc/snapshot.removeall 1

Below copy the .vmx file for the vm.

Change VM1 to the name of your vm folder.. Next change VM1.vmx

cp "$SESI_SOURCE"/VM1/VM1.vmx "$SESI_DEST"/`date +%b%d%Y`/VM1/VM1.vmx

At this point the backup is through all that is left is to move the final file to a long term archive drive. Change LODEST to the name of the attached datastore that will be housing the vm backups. The mv line will then make the copy for you there will be 0 output on the screen when its working just wait for it.

export SESI_DISKMOVE="/vmfs/volumes/LODEST/VMS-PERM/"

You are now done!


Here is my exact text file that runs my backup .. I have removed my vm names and replaced the names with VM1, VM2, VM3..

Click Here To View

Last update 04-22-2016