How to back up and restore a Windows Subsystem for Linux (WSL) distro¶
https://www.xda-developers.com/how-back-up-restore-wsl/
It's always a good idea to back up your PC occasionally. You never know when something will go wrong, so whether you're using Linux, Windows 11, or macOS, regular backups are a good thing. And if you're using the Windows Subsystem for Linux (WSL), it's also a good idea to back up your Linux instances.
Backing up WSL instances is very easy, and it can save you some time setting up WSL on a new PC or if you've reset your laptop and want to get back to a working state quickly. However, there is a small downside, as an imported WSL distribution isn't added to the Start menu like a distribution installed from the Microsoft Store would be. You'll always have to launch that distribution through Windows Terminal.
How to back up a Windows Subsystem for Linux (WSL) distro¶
Since the Windows Subsystem for Linux is like a virtual machine, backing up your data doesn't require you to do anything in Linux itself. All you need to use is Windows Terminal (or Windows PowerShell).
- Open Windows Terminal into a PowerShell profile (it should be the default behavior).
- Run the command below inside PowerShell to print a list of all currently installed Linux distros. It's important that you know the exact name to create a backup. For example, you may have Ubuntu-22.04 rather than just Ubuntu.
- To export a distribution, use the following command, replacing (distribution) and (file path) with the name of the distribution you want to backup and the path where you want to store the backup, respectively.
Replacing the placeholders with the correct information, you'll get something like this:
Keep in mind that the path you selected needs to exist. The command won't automatically create a folder for you.
- Wait a few minutes for the backup to finish.
That's it for the backup process. The file you have now created can be used either on that same PC or another one to set up an exact copy of your Linux distro. You do this using the import command in PowerShell.
How to import a WSL distro from a backup¶
Unregister the Linux distro (if needed)¶
If you're exporting your Linux distro, you probably want to restore it at some point. You can do this on any PC that already has WSL installed, but you can't have installed the same distro you're importing, else the process will fail. If you're trying to restore it on the same PC for whatever reason, and you haven't formatted your PC, you need to unregister it first. Since WSL automatically installs Ubuntu when you first set it up, you'll need to do this if you're importing an Ubuntu distro.
To remove the distribution from your PC, follow these steps:
- Right-click the distribution on your Start menu and select Uninstall.
- Open Windows Terminal and type in the following command:
- You'll see that the version you uninstalled is still listed. To remove it completely, enter the following command, replacing
with the name of the distro you want to remove:
You may want to wait a little bit for all the files to be properly removed and then reboot your PC to make sure all the files are properly removed.
Importing the backed-up distro¶
To import your Linux backup, you'll need to use the following command in Windows Terminal, replacing (distribution), (install location), and (file path) with the respective information.
So, using the same information we used for the import, the command would look like this:
After a moment, your distro will be restored. Again, it won't be added to your Start menu, but you can run the wsl -l command in Terminal to make sure it was installed.
Once it's imported, you can launch your Linux distribution through Windows Terminal. Just enter the following command, replacing (distribution) with the name of the distro you want to launch:
You don't need to start over every time with WSL
That's all there is to backing up and importing your Linux distros in WSL. It's a relatively painless process, as long as you don't care very much about launching Linux from the Start menu. But if you're using Linux, there's a good chance you don't care about using the Start menu that much anyway.