Backup with rsync on Windows (WSL)

rsync on windows with WSL

About this post

Accept reality and have a backup plan - Miley Cyrus

I can't count how many times I've lost files because I was too lazy to set up a proper backup. And every time, I told myself that this would not happen ever again.

At some point, I found the solution for macs and Linux computers: rsync , a tool that can synchronize the contents of two directories.

If you're here, it's because you're desperately looking for a solution to run rsync on windows, so you probably know already that rsync is:

  • efficient : it uses the size and modification times of the files to decide which files need to be transferred.
  • easy to use: one command is enough to synchronize a whole directory
  • versatile : for example, you can make backups to a remote machine, logging in as a different user.
  • safe : I never had any issue with it, and you can test what rsync will do with the dry run option before doing it.

The only drawback with this perfect backup tool is that it's not available on windows. There were a few solutions, like running rsync with cygwin. But I got some issues, for example when dealing with filenames with weird characters like the French accents.

Still, I wanted to be able to back up my precious collection of Georges Brassens, Serge Gainsbourg, and Renaud.

Another solution is to install a full Linux virtual machine on the windows host, but that's really using a sledgehammer to crack a nut, and a complete waste of time and resources, especially disk space.

But now is the future! it's finally possible to run a Linux distribution on Windows 10 using Windows Subsystem for Linux (WSL). And in this distribution, to run rsync.

In this post, you'll learn how to:

  • Enable WSL and install a Linux distribution of your choice
  • Use rsync to back up a folder on your windows PC to another folder on a different disk on the same PC

To do this, you must have Windows 10 installed on your PC.

Windows Subsystem for Linux (WSL)

The Windows Subsystem for Linux has been introduced in Windows 10. It lets developers run GNU/Linux environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a virtual machine.

It's really easy and the documentation is great! And there is no need for plagiarism, so I won't repeat the installation instructions here. But I just want to give you a bit of help to get started.

At some point, you will be asked to enable WSL by opening a powershell.

You can open the powershell by hitting the windows key or the start button, and by typing:

powershell

A terminal window opens. To enable WSL, you need to run powershell as an administrator. So in your powershell terminal, type:

Start-Process powershell -Verb runAs

This opens yet another powershell window where you have administrator rights. Now you can follow the instructions from microsoft .

I chose to install a Debian Linux distribution. You can do the same or choose ubuntu if you prefer, it will work as well. If you take another distro, some of my instructions below probably won't work, but you certainly know what you're doing.

After the initialization of your debian system , we can get started with rsync.

Install rsync

To install rsync, just do:

sudo apt install rsync openssh-client

Then, we're going to test it. Create two directories:

mkdir srcdir backup

Now, create two empty files in srcdir, and list the contents of this directory:

touch srcdir/file1 srcdir/file2
ls srcdir

To synchronize the srcdir and backup directories, type:

 rsync -av srcdir/ backup/
sending incremental file list
./
file1
file2

sent 176 bytes  received 57 bytes  466.00 bytes/sec
total size is 0  speedup is 0.00

Finally, list the destination directory, it contains the two files. Let's try to remove one of the files before running rsync again:

rm srcdir/file2
rsync -av srcdir/ backup/

We see that backup still contains file2. By default, rsync will not delete anything. If you want to delete, do:

rsync -av --delete srcdir/ backup/
sending incremental file list
deleting file2

sent 82 bytes  received 21 bytes  206.00 bytes/sec
total size is 0  speedup is 0.00

You should now have enough faith in rsync to do a backup of your data. Let's do it!

Mounting an external drive

DISCLAIMER: before attempting to run the commands below, I strongly advise to backup your data!

Ok just kidding ;-)

I assume that you want to backup to an external drive, so first connect the drive to your PC. You should be able to see the drive in Windows Explorer. In the Linux terminal, the mounted drives are in /mnt. List them by doing:

ls /mnt
c  d  f  i

These are the letters assigned to the partitions of my drives by Windows. C: is my system partition.

Probably, you will not find your external drive partitions here. It's mounted by windows, but not by the Linux machine. To mount it, do the following:

  • in Windows Explorer, find out which letter Windows assigned to the partition on your external drive. Let's say it's J:
  • then, create the mount point and mount the partition
sudo mkdir /mnt/j
sudo mount -t drvfs J: /mnt/j

You can now see the contents of the partition with

ls /mnt/j

and you can go the root of the partition with

cd /mnt/j

You can use cd to change directory and ls to list the contents of a directory.

Starting the backup

Let's assume you want to backup C:\data\ to J:\.

Just do the following. The -a option is always needed: it's what most people use, you can check the rsync manual for more information. The -v option gives you a verbose output. And the -n option is for dry run: nothing will happen, this is just a test. Finally, I'm using sudo because I got permission errors without it.

sudo rsync -avn /mnt/c/data/ /mnt/j/

And observe the output. If it looks good to you, rerun without the -n option.

And now?

In this post, you've learnt how to:

  • configure WSL, the Windows Subsystem for Linux, to run a debian machine
  • use rsync to back up your files to an external drive

Every time you want to do a backup, you should launch the Debian machine and run the rsync command.

It would be nice to automatize this so that the back up is done every time your start the PC. I don't know how to do this yet, but I'll make an article as soon as I find out.

Stay tuned!


Please let me know what you think in the comments! I’ll try and answer all questions.

And if you liked this article, you can subscribe to my newsletter to be notified of new posts (no more than one mail per week I promise.)

Back Home