Hello to all my dear friends!
I have a laptop and a desktop. Laptop is used for my official and personal purpose and has Linux installed. Desktop has Windows installed and is used by my parents. And I have a requirement to backup files of Windows system through my Linux laptop. rsync is used to backup Linux files and folders. Therefore I wanted to use the same on Windows. And I found a way to do this.
Steps to Prepare Windows for Backup
Windows comes with Windows Subsystem for Linux (WSL). WSL allows to run Linux environment on Windows machine without needing a separate virtual machine or dual boot.
- To install WSL, run
wsl --install
in powershell.
I had default WSL environment Ubuntu already installed. You can also launch Ubuntu app from Start menu.
2. Then in Ubuntu, install openssh server and configure it to run on a custom port let’s say port 212.
3. Then, it is required to do some port forwarding and enabling firewall to allow Linux to enter and ssh to Windows machine (WSL)
Open Powershell as Administrator and run
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=212 connectaddress=172.21.108.123 connectport=212
netsh interface portproxy show v4tov4
This enables to listen to port 212 on Windows machine and then forwards this port to port 212 inside Ubuntu (IP 172.21.108.123)
From the same Poweshell, run
netsh advfirewall firewall add rule name=”Open Port 212 for WSL2” dir=in action=allow protocol=TCP localport=212
This firewall rule allows incoming connection on port 212.
Rsync to Backup Files from Windows on Linux
By default all the drives in Windows are mounted under /mnt
folder inside Ubuntu (WSL). Therefore to backup from Linux, run the following rysnc command on your Linux system.
rsync -e "ssh -p 212" -auv --progress --delete hg@192.168.1.5:/mnt/f/ /run/media/hg/Expansion/Misc/
rsync uses custom ssh port 212 to connect to Windows machine. Windows machine has the IP 192.168.1.5 and then it forwards connection from 192.168.1.5:212 to 172.21.108.123:212 inside the Ubuntu (WSL).
File Permission Problem
You may encounter permission denied while backing up because some files or folders do not get the read permission. To fix that, update wsl.conf inside Ubuntu and then re run.
Inside Ubuntu on Windows, edit wsl.conf
$ cat /etc/wsl.conf
[boot]
systemd=true
[automount]
enabled = true
options = "metadata,uid=1000,gid=1000,umask=022,fmask=011"
Then, shutdown running WSL machine and re run it. From Powershell, run:
wsl --shutdown
And now start the Ubuntu app.
This ensures that directories are mounted with permission 0755 and files with permission 0766. More details on wsl.conf can be found here.
Despite this, permission problem may still be encountered. Here is a Github link that highlights the same problem.
See this
DESKTOP-2K6U7ED:/mnt/f$ find | grep Permission
find: ‘./LotusCardStudio/Artwork’: Permission denied
find: ‘./LotusCardStudio/Blog’: Permission denied
If I list the files the very first time, then it shows:
DESKTOP-2K6U7ED:/mnt/f$ ls -l
total 8
d--x--x--x 1 hg hg 4096 Jun 23 2019 ApexOutlook
See the folder permission “–x–x–x”, it does not allow to read and thereby rsync will fail.
On the other hand listing the files gain will fix the permission:
DESKTOP-2K6U7ED:/mnt/f$ ls -l
total 8
drwxr-xr-x 1 hg hg 4096 Jun 23 2019 ApexOutlook
The permission is now “rwxr-xr-x”. This is a strange behavior and I don’t know why is this.
There is a workaround that finds all directories and list the files. This makes all files and folders readable. Run the following command inside Ubuntu.
find -type d -print0 | xargs -0 ls -l
This command has to be run multiple times until any permission denied error is shown. It will help if there is not huge data but will pose a problem if data is huge. Therefore, I have come up with a better way to do the backup without such problem. I have written about that in this post How to Backup Windows from Linux? – Part 2