How to raise up a Rsync environment on WSL to mirror changes from PC to VPS?
First make sure you are able to connect over SSH from the remote machine to the local one. For this purpose you must copy your private key in the directory /root/.ssh
and it has enough restrictions - sudo chmod 400 /root/.ssh/id_rsa
. Also you can create /root/.ssh/config
file as this:
$ sudo cat /root/.ssh/config
Host my-localhost-name
HostName 77.71.11.10
IdentityFile ~/.ssh/id_rsa
User my-user-name
Port 22122
Now you can use rsync
from the remote machine in this way:
sudo rsync -avzp -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress my-localhost-name:/var/www/<my-folder>/ /var/www/<my-folder>/
In case you don't have /root/.ssh/config
file the command should be:
sudo rsync -avzp -e "ssh -i /root/.ssh/id_rsa -p 22122 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress [email protected]:/var/www/<my-folder>/ /var/www/<my-folder>/
Where:
-a
,--archive
is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). The only exception to the above equivalence is when --files-from is specified, in which case -r is not implied.-v
,--verbose
increases the amount of information you are given during the transfer. By default, rsync works silently.-z
,--compress
- compress file data during the transfer.-p
,--perms
causes the receiving rsync to set the destination permissions to be the same as the source permissions. (See also the --chmod option for a way to modify what rsync considers to be the source permissions.)-
-e
,--rsh=COMMAND
allows you to choose an alternative remote shell program to use for communication between the local and remote copies of rsync...Also you can add here additional options to the
ssh
command. The options used in the above example-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
are useful to keep Rsync quiet and not prompting everytime you connect to a new server. --progress
show progress during transfer.
A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a trailing
/
on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name", but in both cases the attributes of the containing directory are transferred to the containing directory on the destination. In other words, each of the following commands copies the files in the same way, including their setting of the attributes of/dest/foo
:rsync -av /src/foo /dest rsync -av /src/foo/ /dest/foo
Sources:
man rsync
- Ubuntu Documentation: SSH/OpenSSH/Keys
- How To Copy Files With Rsync Over SSH