Góc IT - Blog công nghệ
wsl backup 98

Sao lưu và khôi phục Windows Subsystem for Linux (WSL)

Making a backup of your WSL distros is a smart idea and can help you move instances between machines as well. Here’s how you do it.

Taking regular backups of your computer is a smart thing to do. Whether you use Windows, Mac, Linux, or Chrome OS, you can never be too careful. The same applies to the Windows Subsystem for Linux (WSL) which runs on top of but is somewhat independent of Windows.

WSL instances are easy to set up, but without a backup, you’ll have to redo everything manually if something goes wrong. WSL backups are also an easy way to transfer your environment between machines.

Fortunately, it’s a very straightforward process to back up a Linux distro on WSL, as well as restore it if you need to.

Nội dung bài viết show

How to back up a Windows Subsystem for Linux (WSL) distro

wsl import 71

As WSL isn’t Linux running on bare metal, we don’t need to do anything within the distro itself to instigate a backup. Instead, the operation is entirely run from PowerShell.

First, run the command wsl -l -v 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”.

When you have this information, the following command will export a full backup of your chosen distro. Before entering it, cd into the directory your want to save the backup to. Whichever directory you’re at on the command line is the one your backup will save to by default.

wsl --export (distribution) (filename.tar)

For example, to back up Debian would look something like this:

wsl --export Debian debianbackup.tar

Alternatively, instead of using cd to get into the correct directory, you can specify the file location and filename as part of the export process. For example:

wsl --export Ubuntu c:\users\richard\desktop\ubuntumay27.tar

That’s it for the backup process. The file you have now created can be used either on that same PC or another 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

wsl backup 98

If you’re exporting there’s a good chance you’ll want to be able to use that backed up distro at some point. In WSL that is accomplished using the import command. We’re assuming you have used the –degregister command to remove your current instance. If you still have it installed, importing from a backup won’t overwrite, and using the same distro name will create a conflict.

It’s a little longer than the export command, as you need to specify where you want it to install as well as the file you want to use. The import command follows this template:

wsl --import (distribution) (install location) (file location and filename)

So using one of the example exports from above, you would end up with something like this:

wsl --import Ubuntu c:\wsl c:\users\richard\desktop\ubuntumay27.tar

After a moment your distro will be installed and ready to go. The beauty of using export and import is that you can have the same environment set up on multiple machines in a matter of moments. Your users and passwords will be retained, as will anything you installed through the package manager.

If you want to confirm it is installed correctly before closing the PowerShell window, simply run wsl -l -v again and you should see your newly imported distro.

Add comment