饾枺饾柧饾棇饾梻饾梹饾棁饾柧饾柦 饾棊饾棇饾梻饾棁饾梹 饾柤饾柡饾棁饾棌饾柡
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:
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.
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.
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:
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 images are mine except indicated otherwise
All gifs included are powered by Tenor