How to sync two folders between two Google VM instances?

Let’s say your first VM instance is WEB1 and the second VM instance is WEB2. In the WEB1 VM instance, there is one folder called “uploads” at /var/www/html/wp-content/ and you want to transfer all the files and folders inside it to WEB2 at /var/www/html/wp-content so for that you can use the rsync command.

First, check if the rsync is installed in the WEB1 VM instance by running

rsync --version

Then check who you are, by running the command

whoami

Let’s say the output is WHOAMIOUTPUT. Now view the content of the public key

cat /home/WHOAMIOUTPUT/.ssh/id_rsa.pub

If no key is set then add the SSH key in the WEB1 VM instance just by running the following command:

ssh-keygen

Run the following command to see the SSH public key

cat /home/WHOAMIOUTPUT/.ssh/id_rsa.pub

Copy the output, it starts with ssh-rsa ….

SSH public key

and add it to the Metadata of the Google Cloud.

Go to Google Cloud>> Select the project >> Compute Engine>> Metadata >> SSH KEYS tab edit it and enter the SSH public key that you copy from above at the end. If your WEB2 VM instance is in a different project then you need to select a different project.

Metadata Google Cloud Platform

Now find the IP of the WEB2 VM instance by going to Project >> Compute Engine >> VM instance and get the IP address of WEB2 VM, let’s say the IP is 11.22.33.44, then your command will be like below:

sudo rsync -e  "ssh -i /home/WHOAMIOUTPUT/.ssh/id_rsa" -avzh /FROM_FOLDER_Path WHOAMIOUTPUT@11.22.33.44:/TO_FOLDER_PATH

When you have to move from the WEB1 var/www/html/wp-content/uploads folder to the WEB2 var/www/html/wp-content/ then below is an example command.

sudo rsync -e  "ssh -i /home/WHOAMIOUTPUT/.ssh/id_rsa" -avzh /var/www/html/wp-content/uploads WHOAMIOUTPUT@11.22.33.44:/var/www/html/wp-content/

In this way, you can sync two folders between two Google VM instances by which you may have removed the export-import processes. Please let us know if you know of any other easy way.

Previous article40 cool final year college projects for students in 2023
Next article12+ Artificial Intelligence ideas that you can use for your eCommerce website

LEAVE A REPLY

Please enter your comment!
Please enter your name here