Sync Files to Remote Server

I am going to use Google Cloud Platform (GCP) as remote server to sync files.

Contents

  1. Generate SSH public key and import in GCP
  2. Sync files using command line
  3. Sync files using IntelliJ IDEA Plugin

1. Generate SSH public key and import in GCP

Download and install Google Cloud SDK


◎ Add a rule to GCP firewall

$ gcloud compute firewall-rules create chunming --allow tcp:22 --source-ranges 0.0.0.0/0
Creating firewall...done.                                                                                                                                                  
NAME      NETWORK  DIRECTION  PRIORITY  ALLOW   DENY
chunming  default  INGRESS    1000      tcp:22

◎ Generate public/private keys on your local computer

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/chunming/.ssh/id_rsa): 
/Users/chunming/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/chunming/.ssh/id_rsa.
Your public key has been saved in /Users/chunming/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:XJJS... chunming@Chun-MingdeMacBook-Pro.local
The key's randomart image is:
+---[RSA 2048]----+
|B+ o.            |
|+.o. o * + +     |
|= .  *  * o +    |
| o . o = + *     |
|  . . o E @ .    |
|       .   X .   |
|        = B *    |
|       . o o .   |
|                 |
+----[SHA256]-----+
Now we have keys and know where they are stored. Let's print public key out and copy.
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAACB....0JslKH6A5+x9b chunming@Chun-MingdeMacBook-Pro.local

◎ Import public key in GCP

Compute EngineMetadataSSH keysEditAdd item →  Paste public keySave



◎ Test connection

$ ssh your_username@REMOTE_IP

2. Sync files using command line

◎ gcloud

gcloud command line is an option to transfer files between GCP instance and local computer. See more usages of `Transfer Files` in Official guide. The following example copies a file from your local computer to the home directory on the instance.
gcloud compute scp [LOCAL_FILE_PATH] [INSTANCE_NAME]:~/
The following example recursively copies a directory from your instance to your local computer.
gcloud compute scp --recurse [INSTANCE_NAME]:[REMOTE_DIR] [LOCAL_DIR]

◎ rsync

We can also use rsync command to sync files in terminal.
Type rsync --help for more information
$ rsync [flags] [local path] [user]@[remote server]:[remote path]

◎ Example

we can sync files using this syntax. Really simple!
$ rsync -avh ./test.py chunming@REMOTE_IP:/home/chunming/sync_dir
building file list ... done
test.py

sent 1.29K bytes  received 42 bytes  295.56 bytes/sec
total size is 1.16K  speedup is 0.87

3. Sync files using IntelliJ IDEA Plugin

The plugin I use is called Source Synchronizer. This is a useful plugin allowing IntelliJ lovers to sync files to remote server. It's only few steps to setup and good to go. I take PyCharm IDE as example.

◎ Install Plugin

PreferencePlugins → Browse Repositories... → Search Source Synchronizer → Install → Restart IDE.

◎ Configure remote server

ToolsSource Sync



◎ Click `` to add a new connection name
Host: <your remote server IP>
Root path: <remote path that you are going to sync>
Check `Use SSH key`: indicate the path where we have the public/private keys.
Check `with passphrase`
Username/Passphrase: <your account and password of local computer>
Click `OK` to apply configuration

◎ Choose connection.

 Right click on the projectProject Connection Configuration → Choose added <NAME>OK


◎ Sync Files

Right Click on selected filesSync selected files to remote target → Done 



References


Share:

0 意見:

張貼留言