HOW TO SYNC VAGRANT DIRECTORIES WITH HOST MACHINE (WINDOWS)
π£πΎπππππΎπ½ πππππ πΌπΊπππΊ
Before I begin, please accept my apologies for the difficulties I've had in documenting my learning on my blog, which is primarily due to difficulties balancing school and personal learning. Now I see an opportunity to document and share something I recently learned about: How to Synchronize Vagrant Directories with Host Machine
First I need to ask this question:
The obvious reason for this is so that we can recover our critical files or scripts if the host machine is destroyed. This is why it is critical to understand how to do it.
PREREQUITE:
- Knowing Basic Shell Commands
- A Provisioned Vagrant Linux machine
- Git Bash or Visual Studio code
So we begin:
To do this, I changed my directory to where my vagrant machine is located in my system, so I can edit my vagrant file. I am using the terminal of my Visual Studio Code to do this.
Since I have different machines in my folder, I would be selecting one and log into that machine. To see the status of the machines in my folder, I input the command vagrant global-status
I would be making use of the master machine which I already have running. if your machine is powered off for instance, to power it up and login, you would need to first run vagrant up
or vagrant up machine-name
for instance, I would run vagrant up master
to powerup my machine. From the image below, you would notice that I have already powered off the machine previously with the command vagrant halt
Then to log in to the machine, I used the command vagrant ssh master
to do this.
CREATING A CUSTOM SYNC VAGRANT DIRECTORY TO HOST MACHINE
On the vagrant machine, there is already a sync directory to the host machine which we can find when we run the command cd /vagrant
... So if we create a folder in that directory, it would automatically save in the directory of our host machine.
The folder at the left side of the VS code is the directory of my host machine. You would see that after I enter into the vagrant directory, I would find the same folders and items on my host machine also in the vagrant machine.
To prove this, I would run the command ls
to show all the folders and files in the vagrant directory.
Note: The .vagrant directory in the host machine is a hidden directory which can only be viewed if the command ls -a
is run
In this /vagrant
directory, any file or folder we create in the vagrant machine would automatically be saved on the host machine as well.
But what if we want to save it somewhere else, maybe a custom directory of our choice, how do we do it? To do this we need to edit the Vagrantfile and put in the config line below in it.
config.vm.synced_folder "/path/host/dir", "/path/vm/dir"
The /path/host/dir is the directory location on your host machine where you would like your files to be sync to, while the /path/vm/dir is the directory location on your Virtual Machine where you would want the host machine to sync to.
So I would create a new folder on my desktop and then sync that folder to my virtual machine. In the Image below, I create a folder on my desktop and named it starstrings01 scritps.
Then I open the folder to copy the file path: C:\Users\Gifted-Dev\Desktop\starstrings01 scripts
... Then edit the file path by removing the C: and changing the backwards slashs (\)
to forward slashes (/)
to make it: /Users/Gifted-Dev/Desktop/starstrings01 scripts
Then I would be making use of a custom directory for my virtual machine to be: /opt/scripts
Therefore the config line I would be adding to my vagrantfile is:
config.vm.synced_folder "/Users/Gifted-Dev/Desktop/starstrings01 scripts", "/opt/scripts"
Which has been added in line 22 of my Vagrantfile
Obviously, your vagrantfile may not look like mine, you only have to uncomment this line in your vagrantfile and make necessary changes to your preferred path.
After changes has been made, reload your vagrant machine to save changes by using the vagrant reload
command. While it is being loaded, your sync directory path would be shown in the logs. After then you ssh (log in) into your machine.
After you have logged in, then you can change the directory into your new file path....So when I changed the directory to /opt/scripts
, the path is empty also because the path is new directory and we haven't created any file in it.
If I create any file in the vagrant machine, automatically it would upload the upload on the host machine. You can see in the image below.
In the image above, I tried to create 5 files named DevOps.sh. After the file was created in the vagrant machine, it was automatically uploaded in my custom directory i.e starstrings01 scripts folder.
From the above images you see that you have finally been able to create a custom home directory to sync with your vagrant machine.
SUMMARY
The whole idea of this topic is on how to create a custom directory on your host machine to be able to backup your files if in any case your vagrant machine gets destroyed. And during the course of this lesson we have discussed:
- How to start up your Vagrant machine
- How to check the global Status of your vagrant machines
- How to stop your vagrant machine from running
- How to SSH into a Vagrant machine
- How to check for hidden folders in your vagrant machine
- Finally, how to create a custom directory on your host machine to sync with your vagrant machine.
WHO IS STARSTRINGS01
Designed by @ grisvisa
Starstrings01, also known as Giftedhands, attends the Federal University of Agriculture in Abeokuta to study Mechatronics Engineering. He is a lover of the hive, a guitarist from Nigeria, and a student.
His ambition on Hive is to be more than just an ordinary blogger; he wants to be someone with a purpose. That's why he started the newbies initiative @newbies-hive to help guide and support newbies. Please follow the @newbies-hive curation trail by clicking here.
He tries to juggle education with being active on the chain, but his love and passion for Hive keep him on track..
All gifs included are powered by Tenor
Congratulations @starstrings01! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)
Your next target is to reach 15500 replies.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Check out the last post from @hivebuzz:
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.Β
Β
Dear @starstrings01,
Your support for the current HiveSQL proposal (#138) is much appreciated but the proposal will expire soon!
May I ask you to review and support the new proposal so HiveSQL can stay free to use for the community?
You can support the new proposal (#247) on Peakd, Ecency, Hive.blog or using HiveSigner.
Thank you!