HOW TO SYNC VAGRANT DIRECTORIES WITH HOST MACHINE (WINDOWS)

20221204_040708_0000.jpg

π–£π–Ύπ—Œπ—‚π—€π—‡π–Ύπ–½ π—Žπ—Œπ—‚π—‡π—€ 𝖼𝖺𝗇𝗏𝖺


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:

Why do we need to Sync the Vagrant Directory with our Host Machine?

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.

Vagrantfile - machines - Visual Studio Code 12_3_2022 10_09_52 PM.png

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

Vagrantfile - machines - Visual Studio Code 12_3_2022 10_14_22 PM.png

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

Vagrantfile - machines - Visual Studio Code 12_3_2022 10_20_26 PM.png

Then to log in to the machine, I used the command vagrant ssh master to do this.

Vagrantfile - machines - Visual Studio Code 12_3_2022 10_24_07 PM.png

Vagrantfile - machines - Visual Studio Code 12_3_2022 10_27_19 PM.png

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.

Vagrantfile - machines - Visual Studio Code 12_3_2022 10_59_57 PM.png

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.

Vagrantfile - machines - Visual Studio Code 12_3_2022 11_04_29 PM.png

Note: The .vagrant directory in the host machine is a hidden directory which can only be viewed if the command ls -a is run

Vagrantfile - machines - Visual Studio Code 12_3_2022 11_07_45 PM.png

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.

Desktop 12_3_2022 11_24_33 PM.png

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

Vagrantfile - machines - Visual Studio Code 12_3_2022 11_36_36 PM.png

Vagrantfile - machines - Visual Studio Code 12_3_2022 11_39_40 PM.png

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.

Vagrantfile - machines - Visual Studio Code 12_3_2022 11_43_31 PM.png

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.

Vagrantfile - machines - Visual Studio Code 12_3_2022 11_47_07 PM.png

Vagrantfile - machines - Visual Studio Code 12_3_2022 11_50_49 PM.png

Get Started - starstrings01 scripts - Visual Studio Code 12_3_2022 11_58_30 PM.png

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.

Get Started - starstrings01 scripts - Visual Studio Code 12_4_2022 3_24_19 AM.png

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.

Get Started - starstrings01 scripts - Visual Studio Code 12_4_2022 3_28_34 AM.png

Get Started - starstrings01 scripts - Visual Studio Code 12_4_2022 3_30_25 AM.png

starstrings01 scripts 12_4_2022 3_31_41 AM.png

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


image.png
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


54TLbcUcnRm3sWQK3AJf6fuxkTxiKXRNCarffscTjF9JnBqLzj89NH5s1rKH2Cga4QvMPvvEcb4koTvuCHZXnviw8k7xAcqd9HsbFKjw6hj1Y72M87h86FHy9hno4Dmynf1K26XEe.png

IF YOU LOVE THIS POST, YOU CAN KINDLY SUPPORT IT WITH YOUR COMMENTS, REBLOG AND UPVOTE. IT WOULD BE MUCH APPRECIATED

54TLbcUcnRm3sWQK3AJf6fuxkTxiKXRNCarffscTjF9JnBqLzj89NH5s1rKH2Cga4QvMPvvEcb4koTvuCHZXnviw8k7xAcqd9HsbFKjw6hj1Y72M87h86FHy9hno4Dmynf1K26XEe.png

THANKS FOR VISITING MY BLOG πŸ˜‡πŸ’•



0
0
0.000
3 comments
avatar

Congratulations @starstrings01! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You got more than 15000 replies.
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:

HiveBuzz World Cup Contest - Round of 16 - Recap of Day 1
Our Hive Power Delegations to the November PUM Winners
HiveBuzz World Cup Contest - Recap of the last day of the group stage.
0
0
0.000
avatar

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.Β 
Β 

0
0
0.000
avatar

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!

0
0
0.000