Backup Hack for WSL

Have you ever wanted to backup files / folders in your WSL2 instance, but your Windows system backup software can't see your WSL files? That's what I encountered and here's my work around (hack) for it…

It's true that instead of what I suggest below you can just export your entire WSL instance. However, then you'd have to restore the entire instance as well, which is overkill if all you need to do is restore a few files from say your website project. While this export / import functionality is an awesome feature of WSL, we need something a bit more granular. That's where what I'm doing comes in.

Enter Robocopy

First of all, just the name robocopy sounds cool, so if nothing else it's worth doing this just so you can frequently say “robocopy” in conversation with your friends and colleagues. Robocopy, the full name being “Robust File Copy”, is simply a command line tool (I recommend using PowerShell 7 for your Windows command line) that has apparently been around quite some time. Visit the robocopy documentation to learn all the commands and see how powerful it is. While Windows command line has long had a bad rep, it's probably unwarranted, because tools like this are out there. Anyway, now to the specific command I'm using to make the magic happen:

robocopy /mir \\wsl.localhost\Ubuntu\home\onetrev\sites\ C:\Ubuntu-Backup\websites\

The above command copies all the files and folders (including subdirectories) from the source directory and copies them to the folder specified in the destination parameter. It also gives you a lot of interesting details when it's done copying. More important to note is the /mir option. I use this because this is what enables the copying of subfolders as well as the automatic deletion on the destination side of files no longer found in the source. This is key for backing up files because you don't want files left behind that you've deleted at the source.

Scheduling The Task

Now if just want to run this command manually once in a while, then you're good to go and you can leave this page right now… However, if you want to have this robocopy command run to a schedule so you don't miss backing up any files, then you'd better stick around.

You might be thinking, I was at least, that this is easy to just setup this command as a scheduled task for however often you want your backup to run. But you and I are wrong. I'm definitely not a Windows IT expert, but apparently Task Scheduler can't access your WSL drive this way, or something like that. So what you have to do is create a very simple batch file that creates a network drive, runs your robocopy commands you want, using that network drive, then remove the network drive. It should look something like this:

net use u: \\wsl.localhost\Ubuntu
robocopy /mir u:\home\onetrev\sites\ C:\Ubuntu-Backup\websites\
net use u: /delete

Now with that batch file saved somewhere you can use Task Scheduler to run that script on whatever schedule you want, and boom you have whatever files you want from your WSL instance all copied over to your Windows machine on a schedule. Hooray, problem solved! Of course, you now need to include these files in whatever backup system / service you are using on your Windows machine. Personally I'm using Arq Backup and loving the power and options it offers.

© 2024 Creative Logic Tech Solutions.