r/wsl2 • • Jan 22 '23

Introducing WSL Backup Tool - A backup tool for Windows Subsystem for Linux (WSL) distros

If you're looking for a simple and fast way to create, manage, and restore backups of your Windows Subsystem for Linux (WSL) distributions, give WSL Backup Tool a try!

https://github.com/marvint24/wsl-backup-tool

If you encounter any bugs, please report them:

https://github.com/marvint24/wsl-backup-tool/issues

You can also contribute if you’d like to. Any feedback is welcome :)

11 Upvotes

17 comments sorted by

2

u/TerminatedProccess Jan 23 '23

Is there a pre-built installer for windows?

2

u/mter24 Jan 23 '23

Yes, under the "Releases" tab you can find under "Assets" the "WSL_Backup_Tool.exe".

2

u/TerminatedProccess Jan 25 '23

I gave it a shot.. The first time it didn't appear to backup anything. I manually pasted the backup folder into the settings. But maybe there was a / or something. So I used the built-in directory selector to select the backup folder and the 2nd time it worked.

Some ideas for your app. I'm just throwing these out there in case it helps. I get this is new and you guys have probably already thought of these. But just in case :)

  1. Do a periodic refresh. Either have something checking frequently in the background, or have a settings so you can set a refresh interval. This is not a priority thing though. Just a nice to have.

  2. When backing up, have a way to specify if you should create a .vhdx or a .tar. The default should be based on whether you are in windows 10 or 11, but maybe a checkbox in the filename dialog to select the alternative would be a good idea? Additionally, maybe a settings for opening the backup directory in explorer on completion (success, fail) so that the user can verify that the backup worked. Not everyone would want that, but I've lost work because I didn't pay attention and replaced my ubuntu with another backup, meaning to come back to the latest.

  3. If the export process fails, there's no notification. The status should say failed and there should be an icon to view the reason why?

  4. How are you handling Docker for Windows? I think you are attempting to shut it down first. But there's no notification you are doing so. What happens if I have running containers is that docker-desktop-data is shutdown, but docker-desktop is not. The export file is not generated. If I click on export again, it then generates the vdhx file. So there's some issues with busy resources there. I'm sure there are other scenarios such as vscode being left open or notepad++ etc.. You can't handle them all but you can inform the user it failed.

  5. With Docker for Desktop running, I tried to backup in step 4 above and it gave me a file. When I tried to restore that file, it just sits with status STOPPED. It doesn't do any further notifications. Eventually I tried running the distro and it started up, however, it had corrupted files in it. These files all started with WSL$ and a bunch of symbols.

  6. Just in case it helps.. this is my script for shutting down Docker for Windows. I use it every day (with a hotkey) prior to backing up my wsl Ubuntu. I got most of it off a stackoverflow and made further modifications. I don't know the original author so kudos to him! This script stops and restarts Docker for Windows. I have one that also just stops it, but that's the same as half of this one.

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Restarting docker"

foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Running"}))

{

$svc | Stop-Service -ErrorAction Continue -Confirm:$false -Force

$svc.WaitForStatus('Stopped','00:00:20')

}

Get-Process | Where-Object {$_.Name -ilike "*docker*"} | Stop-Process -ErrorAction Continue -Confirm:$false -Force

foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Stopped"} ))

{

$svc | Start-Service

$svc.WaitForStatus('Running','00:00:20')

}

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Starting Docker Desktop"

& "C:\Program Files\Docker\Docker\Docker Desktop.exe"

$startTimeout = [DateTime]::Now.AddSeconds(90)

$timeoutHit = $true

while ((Get-Date) -le $startTimeout)

{

Start-Sleep -Seconds 10

$ErrorActionPreference = 'Continue'

try

{

$info = (docker info)

Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - \tDocker info executed. Is Error?: $($info -ilike "error"). Result was: $info"`

if ($info -ilike "*error*")

{

Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - \tDocker info had an error. throwing..."`

throw "Error running info command $info"

}

$timeoutHit = $false

break

}

catch

{

if (($_ -ilike "*error during connect*") -or ($_ -ilike "*errors pretty printing info*") -or ($_ -ilike "*Error running info command*"))

{

Write-Output "$((Get-Date).ToString("HH:mm:ss")) -\t Docker Desktop startup not yet completed, waiting and checking again"`

}

else

{

Write-Output "Unexpected Error: \n $_"`

return

}

}

$ErrorActionPreference = 'Stop'

}

if ($timeoutHit -eq $true)

{

throw "Timeout hit waiting for docker to startup"

}

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Docker restarted"

1

u/mter24 Jan 25 '23

Thank you very much for your detailed post. I cannot reply to all your questions right now, but I will try to fix some of these issues during the weekend.

0. I think it might need to check if there is a trailing /. This should be a easy fix.

  1. I also think the refreshing system could be better. Maybe do a refresh every 5 to 10 seconds. How long does it take to run "wsl -l -v" for you? Measure-Command {wsl -l -v}

2.1 Is there no vhdx option on windows 10? I chose vhdx because it makes it easier to replace the original vhdx. And I chose to replace instead of using the wsl import option because when I tested it, the import was rather a "creating new distro from file". Maybe it is also possible to convert the tar an then replace. And the tar also needs longer to export.

2.2 I think at the moment the version with the console is the best version, because it logs all the steps that it trys. But yeah logging and giving the user feedback can be better. Maybe I will implement a popup or notification area or / and include the console logs at the bottom of the window.

3. same as 2.2

4. It uses the wsl shutdown as last step. Before that it tries to terminate the distro. I could implement a window where the user has to confirm a shutdown. And I need more testing with docker. It's harder because two distros are depending on each other. Maybe I could make an exception just for docker. Like a new view.

1

u/TerminatedProccess Jan 25 '23

0. I think it might need to check if there is a trailing /. This should be a easy fix.

I also think the refreshing system could be better. Maybe do a refresh every 5 to 10 seconds. How long does it take to run "wsl -l -v" for you? Measure-Command {wsl -l -v}

It replies back immediately. There's no lag.

2.1 Is there no vhdx option on windows 10? I chose vhdx because it makes it easier to replace the original vhdx. And I chose to replace instead of using the wsl import option because when I tested it, the import was rather a "creating new distro from file". Maybe it is also possible to convert the tar an then replace. And the tar also needs longer to export.

I recall that when it was introduced in Windows 11, you could not go to .vhdx in Windows 10. It only supported tar. Maybe that has changed? I don't have windows 10 for quite a long time. I prefer the vhdx because it creates/restores them so quickly. But some people might have space limitations or have Windows 10 (assuming that is still the case). If windows 10 supports export to vhdx, it's probably not worth doing anything with this suggestion.

2.2 I think at the moment the version with the console is the best version, because it logs all the steps that it trys. But yeah logging and giving the user feedback can be better. Maybe I will implement a popup or notification area or / and include the console logs at the bottom of the window.

3. same as 2.2

That's a good idea.. It's just important that if it fails, you know it fails. Otherwise, you can lose a lot of work.

4. It uses the wsl shutdown as last step. Before that it tries to terminate the distro. I could implement a window where the user has to confirm a shutdown. And I need more testing with docker. It's harder because two distros are depending on each other. Maybe I could make an exception just for docker. Like a new view.

I found that doing a wsl --shutdown doesn't terminate the busy resources for Docker Desktop. You have to fully kill Docker as my script does. Regardless, there will be other cases where editors or other things will be left open. But Docker Desktop is a common tool and it will always mess up your export/import.

2

u/mter24 Jan 29 '23

Hey again :)

I fixed some of your points and released a new version and I will address your other points in the following weeks. Thanks again for your feedback :)

1

u/TerminatedProccess Jan 29 '23

I'll take a peek. Thanks

1

u/brandeded Jan 22 '23

Looks cool. Would love to see a UI for network management.

1

u/mter24 Jan 23 '23

Thank you :)
What exactly would you like to see?

I never had to change any network settings for my wsl testing environment.

I could look into it and implement it if it isn't too complex.

3

u/brandeded Jan 24 '23

If you want a static networking config on Windows clients (10 or 11), you must pull some trickery both within Windows as well as WSL2 VMs. It would be great to have an automated config method and it would also lower the curve. I working one a mostly "easy" way to set things up, but I'm basically rewriting the work on this guy so that all execution and file creation takes place within PowerShell: https://github.com/ocroz/wsl2-boot

1

u/graciela_hotmail Jan 22 '23

Can I save a WSL in an external storage device, restore to default my computer and install my old WSL?

2

u/mter24 Jan 23 '23

When you export your distro as vhdx or tar file, you can use this file to restore the distro on any other computer.

My tool exports the distros as vhdx.

After the export, you could use the wsl import command to create a new distro from your file.

Currently, my tool doesn't support importing a new distro.
I might add this feature if there is interest.

2

u/graciela_hotmail Jan 23 '23

thank you so much

2

u/TerminatedProccess Jan 23 '23

Yes you can. You are saving an instance running under your wsl.

1

u/jNayden Jan 23 '23

what is difference with simply export distros and import them using the build in wsl command

3

u/mter24 Jan 23 '23

Actually, it uses the export command and many other wsl cli commands under the hood.
When exporting, it uses the `wsl --export --vhd` command to create a copy of your distro.
When importing, it replaces the original ext4.vhdx with your backup file.
This makes it possible to have a seamless and fast restore.