Rsync – Set Up Environment on WSL to Mirror Changes from PC to VPS

backuprsyncserversshwindows-subsystem-for-linux

I have an Ubuntu 16.04.2 DigitalOcean VPS to which I usually SSH with Putty and make changes in there, but I recently found out I can make a duplicate of this server in my PC (Win10 home with WSL), do the changes here locally, and then mirror them (while SSH tunneled) via a program called rsync, if everything went good. The main benefit here is a comfortable, primary layer of backup.

If it is possible to use rsync in the WSL beta, how is it done from the moment rsync is installed, for example, if I raise up a server environment at my Win10 home WSL with this code:

apt-get update -y && apt-get upgrade -y
apt-get install tree zip unzip make php-zip php-curl php-xml php-gd
apt-get install fail2ban
apt-get install lamp-server^ -y
a2enmod rewrite
sed -i 's/post_max_size \= .M/post_max_size \= 200M/g' /etc/php/7.0/apache2/php.ini # regex dot instead of 2 or 8.
sed -i 's/upload_max_filesize \= .M/upload_max_filesize \= 200M/g' /etc/php/7.0/apache2/php.ini

cat <<-'LAMPENV' >> /etc/apache2/apache2.conf

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
LAMPENV

systemctl restart apache2.service

and I then bring up the websites on my PC locally with localhost/site_name, how do I mirror it with rsync to the VPS?

I ask this question mainly to see if what I described so far is enough – if I don't miss anything.

In an answer, please review the way I described above as a basis for the rsync action and say if something missing, and detail how you would do so with rsync in WSL afterwards.

Best Answer

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 my-user-name@77.71.11.10:/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:

Related Question