I think that we can all agree that backup software exists for a reason. Well, lots of reasons. Very good reasons. So, if you ask me in the abstract how to make a copy or backup of a virtual machine’s virtual hard disk file while the virtual machine is running, I’m probably going to refer you to your backup vendor.
If you don’t have one, or don’t have one that you can trust, then I am naturally going to recommend that you download Altaro VM Backup. Backup is their wheelhouse and they’re going to have a lot more experience in it than any of us. The outcomes will be better than anything that we administrators can do on our own.
But, I also understand that sometimes you have one-off needs and you need to get something done right now.
Or, you need to script something.
Or your backup software isn’t quite granular enough.
Or you have some other need that’s unique to your situation.
If you need to get a copy or backup of one or more of a virtual machine’s hard disks without shutting down the virtual machine, you have three options, shown in their preferred order:
- Use your backup application, as we discussed.
- Export the virtual machine with Hyper-V Manager or Export-VM. This only works for Hyper-V versions 2012 R2 and later.
- Copy the file manually.
I’m not going to change my mind that a backup application is the best way to get that copy. But, I’m done beating that horse in this article.
Export is the second-best solution. The biggest problem with that is that it exports the entire virtual machine, which might be undesirable for some reason or another. It also locks the virtual machine. That won’t necessarily be a bad thing, especially if all you’re doing is getting a copy, but maybe it’s a showstopper for whatever you’re trying to accomplish.
That leaves us with option 3, which I will illustrate in this article. But first, I’m going to try to talk you out of it.
You Really Shouldn’t Manually Copy the Disks of a Live Virtual Machine
Manually copying a live VHD/X file isn’t the greatest idea. The best that you can hope for is that your copy will be “crash consistent“. The copy will only contain whatever data was within the VHD/X file at the moment of the copy. Any in-flight I/Os will be completely lost if you’re lucky or partially completed if you’re not. Databases will probably be in very bad shape. I’m sure that whatever reason that you have for wanting to do this is very good, but the old adage, “Because you can do a thing, it does not follow that you must do a thing,” is applicable. Please, think of the data.
OK, the guilt trip is over.
Just remember that if you attach the copied disk to a virtual machine and start it up, the volume will be marked as dirty and your operating system is going to want to run a repair pass on it.
Manually Copying a Hyper-V Disk the Dangerous Way
That header is a bit scarier than the reality. Most importantly, you’re not going to hurt your virtual machine doing this. I tested this several times and did not have any data loss or corruption issues. I was very careful not to try this process with a disk that housed a database because I was fairly certain that would break my perfect streak that way.
Use robocopy in restartable mode:
robocopy /z "C:LocalVMsVirtual Hard Disks" Z:Backup server1.
The format is:
robocopy /z SOURCE_FOLDER DESTINATION_FOLDER VHDX_FILENAME
It is important that you do not use a trailing slash on the folder names! If you want to copy multiple files, just enter them with a space between each.
Pros of the robocopy method:
- It’s easy to remember
- It works on anything that your account can reach — local storage, CSVs, SMB shares, whatever
- Is “good enough”
Cons of the robocopy method:
- Restartable mode (specified by the /z switch) is sssssllllllllooooooooow especially over networks
- There is no guarantee of data integrity. But, there’s no real guarantee of data integrity when manually copying a live VHD/X anyway, so that probably doesn’t matter.
- I doubt that anyone will ever give you support if you use this method
For basic file storage VHD/X files, this is probably the best of these bad methods to use. I would avoid it for frequently written VHD/X files.
Manually Copying a Hyper-V Disk the Safer Way
A somewhat safer method is to invoke VSS. It’s more involved, though. The following is a sample — do not copy/paste!
vssadmin create shadow /for=C: mklink /d C:vssvolume \?GLOBALROOTDeviceHarddiskVolumeShadowCopy26 xcopy "C:vssvolumeLocalVMsVirtual Hard Disksserver1.vhdx" Z:Backup rmdir C:vssvolume vssadmin delete shadows /shadow={e47b5da0-0ae1-415b-b604-e94ff1913586}
This is going to need somewhat more explanation than the robocopy method. We’ll take it line-by-line.
The first line tells VSSADMIN to create a shadow copy for the C: volume. The VHD/X file that I’m targeting lives on C:. Substitute your own drive here. The shadow copy becomes a standard Windows volume.
vssadmin create shadow /for=C:
The second line creates a symbolic link to the newly created volume so that we can access its contents with the usual tools. You can discover what that lines contents should be via the output from the previous command.
We use “mklink.exe” to create the symbolic link. The /D switch lets it know that we’re going to make a directory link, not a file link. After that, we only need to tell it what to call the link (I used C:vssvolume) and then the target of our link. It is vitally important that you place a trailing slash on the target or your symbolic link will not work.
mklink /d C:vssvolume \?GLOBALROOTDeviceHarddiskVolumeShadowCopy26
Next, we copy the file out of the shadow copy to our destination. I used XCOPY because I like XCOPY (and because it allows for tab completion of the file name, which robocopy does not). You can use any tool that you like.
xcopy "C:vssvolumeLocalVMsVirtual Hard Disksserver1.vhdx" Z:Backup
That’s all for the file. You can copy anything else out of the shadow copy that you want.
We need to clean up after ourselves. Do not leave shadow copies lying around. It’s bad for your system’s health. The first step is to destroy the symbolic link:
rmdir c:vssvolume
The last thing is to delete the shadow. There are multiple ways to do this, but my preferred way is to delete the exact shadow copy that we made. If you look at the output of your vssadmin create shadow command, it has all of the information that you need. Just look at the Shadow Copy ID line (it’s directly above the red box in my screen shot). Since VSSADMIN was nice enough to place all of that on one line, you can copy/paste it into the deletion command.
vssadmin delete shadows /shadow={e47b5da0-0ae1-415b-b604-e94ff1913586}
You’ll be prompted to confirm that you want to delete the shadow. Press [Y] and you’re all finished! If you want to see other ways to remove VSS shadows, type vssadmin delete shadows without any other parameters. It will show you all of the ways to use that command.
Yes, this works for Cluster Shared Volumes. Get a shadow of C: as shown above and copy from the shadow’s ClusterStorage folder. I would take caution to only perform this from the node that owns the CSV.
Pros of the VSSADMIN method:
- It’s completely safe to use, if you do it right.
- It’s not entirely perfect, but some quiescing of data is done. The copied volume is still dirty, though.
- Faster when copying to a network destination than robocopy in restartable mode.
- Works for local disks and CSVs. Won’t work for SMB 3 from the Hyper-V host side.
Cons of the VSSADMIN method:
- Tough to remember (but this blog article should live for a long time, and that’s what Favorites and Bookmarks are for)
- If you don’t clean up, you could cause your system to have problems. For instance, you might prevent your actual backup software from running later on in the same evening.
- May not work at all if another VSS snapshot exists
- May have problems when third-party and hardware VSS providers are in use
If the integrity of the copied data is important and/or changing frequently, you’ll likely get better results from the VSSADMIN method than the robocopy method. It’s still not as good as the other techniques that I promised not to harp on you about.