Move Vagrant virtual machine, keep everything (database, virtual hosts etc..)
I have a lot of Vagrant instances and I decided I’m going to reinstall my Windows, but I didn’t want to mess with reconfiguring all of my servers, import the databases, virtual hosts, other fine tunes etc..
I’ve searched through the whole internet for step-by-step instructions about how should I export the machines with 100% insurance about I’ll get back exactly the same vagrant machine like before the reinstall.
I’ve found tons of articles about I should use `vagrant package` but after reading a couple of articles it seemed my database and virtual host files will be lost.
Finally, I decided I’ll backup every file that related to VirtualBox or Vagrant, reinstall Windows and I’ll find out what should I do.
The solution
Export (Backup)
1. Export your virtual machine
Open VirtualBox and enter into the File
option to choice Export Appliance...
You will then get an assistance window to help you generate the image.
- Select the VM to export
- Enter the output file path and name
- You can choose a format, which I always leave the default OVF 1.
source: https://askubuntu.com/questions/588426/how-to-export-and-import-virtualbox-vm-images
2. Backup your vagrant folder
Copy your vagrant folder (the folder where you use the `vagrant up`, `vagrant halt` etc.. commands) with the Vagrantfile and .vagrant folder to a safe place.
And we’re done with the backup part ;)
Import (Restore)
1. Import your VirtualBox machine
Open VirtualBox and enter into the File
option to choice Import
You will then get an assistance window to help you load the image.
Enter the path to the file that you have previously exported
I left disabled the Reinitialize the MAC address of all the network cards option
Press Import
and done!
source: https://askubuntu.com/questions/588426/how-to-export-and-import-virtualbox-vm-images
2. Get the ID of the imported machine
Open command line (cmd):
Navigate to your VirtualBox folder (usually here: `c:\Program Files\Oracle\VirtualBox`):
cd “c:\Program Files\Oracle\VirtualBox”
And get the list of the virtual machines:
VBoxManage list vms
You will see something like:
Look for the name of your virtual machine and copy its id without the { }
Move your backup vagrant folder to its new or original place.
Navigate to:
your_vagrant_folder\.vagrant\machines\default\virtualbox\
Open the file with name `id`, and change the id to the id that you copied from the virtual machine list window before.
Save the file.
You can start your vagrant machine with `vagrant up`. If you did everything well you should see that in VirtualBox GUI your imported VirtualBox machine has been started.
If you didn’t change the id a new/clean machine will be downloaded. Not a big deal, you can change the id anytime and delete the unnecessary virtual machine from the VirtualBox
You can continue using your original vagrant machine on your clean windows/new laptop :)