Rsync on GCP Compute Engine: When You Can’t Run Your Code Locally (Network Issues)
On my quest for learning some new technologies, I’ve stumbled into the issue of connecting to my server/instance/database from my local machine due to network issues. The way around this would be to run my code from a virtual machine on the same network, but getting my code onto the machine seemed like a frustrating process until I discovered Rsync. Rsync is a command-line application that allows you to easily transfer and synchronize files across computers or virtual machines.
Create the VM
I’ll be using Google Cloud Platform for this, but this technique should apply to any VM. I’ll create a VM and the main setting that matters is the network. You’ll want this to be the same network that your other resources are on. This will most likely just be default.
I hit create and wait for the instantiation.
Install rsync and setup SSH
SSH into your VM through the cloud console for these next steps.
Install rsync on your Ubuntu VM by running the following:
sudo apt-get install rsync
Add the public key you’re going to use to connect to the VM (I’m using
~/.ssh/id_rsa.pub) to the VM’s
~/.ssh/authorized_keys file. You can create a new directory for your code if you’d like as well.
vi ~/.ssh/authorized_keys # paste the key from your local machine in here
Set up your local computer with rsync
Now, back to your local machine. Install rsync on this as well. I’m on a Mac so I use Homebrew, but use your preferred command line installer.
brew install rsync
Now, you can go into your code directory and run rsync to get your code from your local computer to your VM.
rsync -a $PWD/ [YOUR_IP_ADDRESS]:code_goes_here/
If you have an issue with permissions or public key being denied, try SSHing to the VM to make sure that goes okay. You can add the specific public key and username to the SSH command to see if that helps. If that does the trick, add those to your
~/.ssh/config file so rsync will automatically use them when connecting.
Run your code
Finally, check back on your VM and your code should be there! Run the code and if you need to make changes, edit your local files, rsync and head back to your VM.
ls # Check that your code is there.
mvn compile exec:java