WSL

How to Backup and Restore Linux Distro on WSL (Windows Subsystem for Linux)?

· 4 min read
WSL - Back and Restore

Nobody can deny the importance of backup & restore process for any environment. Specially, when multi-steps are involved in setup and configuration and we keep modifying the environment with time. Basically, if anything goes wrong for any reason, we don't want to end up wasting time by setting up the same environment from scratch. We should be able to start from the step it failed. In this article, we'll see the process to take backup of environment running on Windows Subsystem for Linux (WSL) and restore using the same backup.

Video:

Backup Linux Distro on WSL

  • Run the following command in Windows Command Prompt to find the list of installed Linux distributions.
C:\Users\AwsJunkie>wsl -l -q
Ubuntu

WSL Command Usage Information:

Usage: wsl.exe [Argument] [Options...] [CommandLine]

    --list, -l [Options]
        Lists distributions.

        Options:
            --all
                List all distributions, including distributions that are
                currently being installed or uninstalled.

            --running
                List only distributions that are currently running.

            --quiet, -q
                Only show distribution names.

            --verbose, -v
                Show detailed information about all distributions.

            --online, -o
                Displays a list of available distributions for install with 'wsl --install'.
  • Copy the name of the Linux distribution (e.g. "Ubuntu" in above example) to be backed-up.

  • Execute wsl --export <Distro> <FileName> in Command Prompt. To create the back-up in a location different from the current working directory of Command Prompt, then use fully quilified path as shown in the below example.

C:\Users\AwsJunkie>wsl --export Ubuntu C:\backup\wsl\Ubuntu-2022-09-02.tar

WSL Command Usage Information:

Usage: wsl.exe [Argument] [Options...] [CommandLine]

Arguments for managing distributions in Windows Subsystem for Linux:

    --export <Distro> <FileName>
        Exports the distribution to a tar file.
        The filename can be - for standard output.
  • Once executed successfully, navigate to the directory and confirm.

wsl --export  Distro FileName

Restore Linux Distro on WSL

  • Run wsl --import <Distro> <InstallLocation> <FileName> [Options] to restore the Linux Distro from the back taken earlier.
C:\Users\AwsJunkie>wsl --import MyUbuntuEnv C:\installed\env\wsl C:\backup\wsl\Ubuntu-2022-09-02.tar

WSL Command Usage Information:

Usage: wsl.exe [Argument] [Options...] [CommandLine]

Arguments for managing distributions in Windows Subsystem for Linux:

    --import <Distro> <InstallLocation> <FileName> [Options]
        Imports the specified tar file as a new distribution.
        The filename can be - for standard input.

        Options:
            --version <Version>
  • Once executed successfully, navigate to the installation location. There should be one newly created Hard Disk Image File.

wsl import Distro InstallLocation FileName Options

Run following command to confirm that it is installed properly.

C:\Users\AwsJunkie>wsl --list --verbose
  NAME           STATE           VERSION
* Ubuntu         Stopped         2
  MyUbuntuEnv    Stopped         2

To run the restored distribution (e.g. "MyUbuntuEnv") execute wsl -d <Distro>

C:\Users\AwsJunkie>wsl -d MyUbuntuEnv
Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.10.102.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sat Sep  3 01:01:25 EDT 2022

  System load:  0.0                Processes:             8
  Usage of /:   0.4% of 250.98GB   Users logged in:       0
  Memory usage: 3%                 IPv4 address for eth0: 123.11.xx.xx
  Swap usage:   0%

0 updates can be installed immediately.
0 of these updates are security updates.


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


This message is shown once once a day. To disable it please create the
/root/.hushlogin file.
root@Laptop:/mnt/c/Users/AwsJunkie#

Now if you check again, STATE will be shown as "Running".

C:\Users\AwsJunkie>wsl -l -v
  NAME           STATE           VERSION
* Ubuntu         Stopped         2
  MyUbuntuEnv    Running         2

In case if you are curious, uninstallation is just one command away. wsl --unregister <Distro> will unregisters the distribution and deletes the root filesystem.

C:\Users\AwsJunkie>wsl --unregister MyUbuntuEnv
Unregistering...

C:\Users\AwsJunkie>wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Stopped         2
How to Uninstall or Remove Linux Distro Installed on WSL (Windows Subsystem for Linux)?
Uninstallation of redundant or unused Linux Distros on Windows Subsystem for Linux (WSL) is an important activity to reclaim space and maintain hygiene of the system. It is just one command away. Let’s see. * Run the following command in Windows Command Prompt to find the list of installed Linux…

I usually take the first backup immediately after installation of WSL Distro (basic configurations and upgrade). And then I take subsequent multiple redundant backups before  any major change/upgrade or new installation within my development environment. It is very quick, easy. Storage is cheap too. Believe me, it may prove as a life saver in our bad days.

Please let me know in the comment section below how important is back-up for you? Thanks.

References