WSL2 Backup and Restore

Prerequisites

wsl --list  

Result:

Windows Subsystem for Linux Distributions:  
Ubuntu20.04LTS (Default)  
Debian  

We will work with Ubuntu20.04LTS

Shutwown WSL

wsl --shutdown  

Export (Backup) image

Template:

wsl --export <Image Name> <Path to exportable .tar image file>  

An actual command in my case:

wsl --export Ubuntu20.04LTS c:\back\Ubuntu20.04LTS.tar  

Import (Restore) image

Import image

Template:

wsl --import <Image Name> <Directory where you want to store the imported image> <Path to exported .tar image file>  

An actual command in my case:

wsl --import Ubuntu20.04LTS c:\WSL_Dir_With_Read_Write_Permissions\Ubuntu20.04LTS c:\back\Ubuntu20.04LTS.tar  

Distro will be started automatically with a root prompt

Set default user

  1. Execute awk -F: '/\/home/ {printf "%s:%s\n",$1,$3}' /etc/passwd and find decimal ID of required user. 1000 in my case (line my_user_name:1000)
  2. Enter exit command
  3. Ensure you are in CMD without Admin privileges and execute wsl --shutdown command
  4. Launch 'regedit'
  5. Go to HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss
  6. Find your distro ID and go to an appropriate subkey
  7. Ensure value of name 'DefaultUid' and of type 'REG_DWORD' with value data 1000 with base Decimal
  8. Close Regedit

Set default distro

wsl --setdefault Ubuntu20.04LTS  
wsl --shutdown  

Start WSL:

wsl  

Ubuntu 20.04 LTS will start with your chosen user prompt

Sources

1, 2, 3