Regular backups are a good thing. You never know when something bad is going to happen and having a backup of the data on your PC could save more than a few headaches.
If you're dabbling with the Windows Subsystem for Linux (WSL), it might not have crossed your mind. WSL is incredibly easy to set up, and if you destroy an installation it's always fast to get going again. But you can, and probably should, take regular backups if you're using it a lot.
Here we'll show you how to export your entire Linux distribution into a file on your Windows 10 PC for safe keeping.
How to backup a WSL distribution
The neat thing about doing this is that you only need a single command in PowerShell to choose which of your distributions you want to export and the file name you want to give it.
There are a couple of things you might want to do before you begin, though.
wsl -l -vto get a full list of the distribution names. This is important because you need to match exactly, and if you have "Ubuntu-20.04" you will need to use that. Just "Ubuntu" won't work.
- Use the
cdcommand to navigate into the directory you want to save your export to. It will save to whatever directory you're at the command line in, which by default will be your users folder.
Once you're happy with the above, the export command follows this template:
wsl --export (distribution) (filename.tar)
So, to export Ubuntu 20.04 the command would be:
wsl --export Ubuntu-20.04 ubuntubackup.tar
Make sure to add the .tar extension as well to your filename otherwise you may run into some issues.
It only takes a few seconds to run and then you'll be able to see the exported backup in whichever folder you saved it to.
Importing a previously exported .tar file
If you're using the export feature to backup one of your WSL Linux distributions then there's a good chance you might also want to use it at some point.
Fortunately, should the moment arise, there is an equally simple command template to import your .tar files to a specified install location.
wsl --import (distribution) (install location) (file location and filename)
So it might look something like:
wsl --import Ubuntu-20.04 C:\Users\MyPC\AppData\Local\Packages\Ubuntu C:\Users\MyPC\Documents\ubuntubackup.tar
To check it imported correctly, run the
wsl --list command and you should now see your newly imported distribution.