Kkart
Kkart

Reputation: 357

Vagrant: How to sync folder from guest back to host?

Why, because it is difficult to work and edit code via legacy editors in the guest. The whole purpose of vagrant is to be more easily for the developer, right:)?

As such I please someone to guide me in this situation:

On the VM repo is in /home/vagrant/src. I want it to be visible/editable in ../src in the host.

I read the docs and putted this in the Vagranfile:

config.vm.synced_folder '../src', '/home/vagrant/src'

This "works" except that it overwrites all contents from /home/vagrant/src with those from ../src which is empty.

Any workaround? I considered the possibility to clone the repo via git(publicly available via github) and sync the folder to the VM but this does not feel right and lose production configs too.

Upvotes: 22

Views: 15084

Answers (8)

Denn
Denn

Reputation: 823

Vagrant Has plugins to help with copying of files and folders between the Guest and Host OS.

Install vagrant-vbguest and vagrant-scp:

$ vagrant plugin install vagrant-vbguest
$ vagrant plugin install vagrant-scp

To copy a file or a directory between Vagrant host and guest VM you need to know the id or the name of a guest VM.

This information can be found in the output of the following command:

$ vagrant global-status

Copy file or directory from Vagrant host machine to guest VM:

$ vagrant scp <some_local_file_or_dir> <vm_name>:<some_path_on_vm>

Copy file or directory from guest VM to Vagrant host machine:

$ vagrant scp <vm_name>:<some_file_or_dir_on_vm> <some_local_path>

Upvotes: 1

madebydavid
madebydavid

Reputation: 6517

It sounds like you want to share a folder from the guest machine with the host machine which is kind of the opposite of what the normal shared folders do.

Have you thought about provisioning NFS on the guest, and then using vagrant-triggers to mount the share from the guest onto the host.

First start by installing vagrant-triggers:

vagrant plugin install vagrant-triggers

Then have a Vagrantfile like this:

# where you want to mount the guest's shared folder on the host
host_mount_dir = "/home/david/vagrant-home"

Vagrant.configure(2) do |config|
    config.vm.box = "debian/jessie64"

    # forward the NFS port    
    config.vm.network "forwarded_port", guest: 2049, host: 8049

    # configure NFS on guest
    config.vm.provision "shell", inline: <<SCRIPT
apt-get install -y nfs-kernel-server nfs-common
host_ip="$(netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10)"
echo "/home/vagrant       $host_ip(rw,sync,no_subtree_check)" > /etc/exports
exportfs -a
SCRIPT

    # after the machine is up, mount the NFS share
    config.trigger.after :up do
        # mkake the dir if it doesnt exist
        run "mkdir -p " + host_mount_dir
        # we need the users pass to mount (make sure you are a sudoer!)'
        puts "Please enter your password so that we can run mount"
        run "sudo mount -t nfs -o port=8049 localhost:/home/vagrant #{host_mount_dir}" 
    end

    # before halting, unmount the share
    config.trigger.before :halt do
        # we need the users pass again
        puts "Please enter your password so that we can run mount"
        run "sudo umount #{host_mount_dir}"
    end 

end

This is an unusual setup, but I tested it now with an Ubuntu host and a Debian guest and it seems to be working - YMMV.

Upvotes: 5

cn0047
cn0047

Reputation: 17051

My Vagrantfile looks like this:

Vagrant.configure(2) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network :forwarded_port, guest: 22, host: 10170, id: "ssh"
  config.vm.provision :shell, path: "VagrantProvision.sh"
  config.vm.synced_folder "./", "/var/beeGame"
end

As you can see here I have the same statement like yours! Here my repo (you can try it like example), and it works for me! I use PhpStorm to open this repo and edit files and all updates automatically and sync from my host machine (my laptop) to guest machine (virtual).

In your case, you're doing all right, and vagrant has to overwrites all contents from /home/vagrant/src with those from ../src. It is right behavior!!! You need put your code into src dir on your host machine (your laptop) not on your guest machine (virtual)!

Upvotes: 6

tonejito
tonejito

Reputation: 319

I used the following hack to rsync the folder from guest to host:

  • First, get the SSH configuration for the box and save it on ssh_config

$ vagrant ssh-config > ssh_config

  • Then use rsync the guest folder back to the host with the given configuration

$ rsync -avH -e "ssh -F ./ssh_config" default:/vagrant/ ./

It worked for me and I hope it works for you!

Upvotes: 3

hurturk
hurturk

Reputation: 5454

You can use rsync with Vagrant, it has built-in support. Here is the link to the documentation. While it is trivial for rsync to ignore excessive files at target (probably the default behavior) you can still sync accurately by protecting your target files using protect filter.

Unlike mounting, rsync has to be triggered manually. However, you can still use Vagrant's rsync-auto feature documented here for a simple filesystem watch.

Upvotes: 3

sensorario
sensorario

Reputation: 21600

I work with vagrant with this structure:

Vagrantfile
MyCode.php

and Vagrantfile contains:

config.vm.synced_folder ".", "/vagrant"

that means that MyCode.php is visible inside vagrant machine in /vagrant folder. I can edit that file both from vagrant machine (the guest) and from my machine (the host).

Upvotes: 0

John Bachir
John Bachir

Reputation: 22711

I've determined that the issue isn't that config.vm.synced_folder '../src', '/home/vagrant/src' overwrites /home/vagrant/src. The problem is that home/vagrant/src becomes a new different type of directory in the filesystem which is empty. So, one simply has to repopulate it after setting up the shared directory. So...

  1. on guest: sudo mv home/vagrant/src home/vagrant/src-back
  2. halt guest
  3. add new synced folder to Vagrantfile: config.vm.synced_folder '../src', '/home/vagrant/src'
  4. start guest
  5. on guest: sudo mv home/vagrant/src-back/* home/vagrant/src-back/

And things should work well going forward.

Upvotes: 5

Rajind Ruparathna
Rajind Ruparathna

Reputation: 2255

Seems like you are looking for two way sync. If my understanding is correct this answer will help you. Please note that I have not yet tested these. Seems like the key is to use virtualbox sync type.

type (string) - The type of synced folder. If this is not specified, Vagrant will automatically choose the best synced folder option for your environment. Otherwise, you can specify a specific type such as "nfs".

config.vm.synced_folder '../src', '/home/vagrant/src', type: "virtualbox"

Then again according to this doc,

If you are using the Vagrant VirtualBox provider, then VirtualBox shared folders are the default synced folder type. These synced folders use the VirtualBox shared folder system to sync file changes from the guest to the host and vice versa.

Therefore I'm not sure why your default type is not virtualbox. Anyway this is worth a shot I guess. Further going into docs there seems to be a bug in virtual box type explained in docs. Please follow the steps here if you are going to use virtual box sync type.

Upvotes: 11

Related Questions