How to backup (WSL) Linux Subsystem for Windows 10, before system reset or reinstall?

Windows 10 v1903 includes a built-in WSL export/import command. Specifically, wsl --export, which produces a tar and wsl --import, which imports a previously exported tar. You can then move this file between computers.

You can also specify stdin/stdout with -, which should allow you to directly transfer to another machine and import using a remote shell like ssh.


How to backup?

Any number of ways, but one common practice is to use tar to create an archive of the files you want to backup (in order to re-install everything when you recreate your environment). Writing a simple backup.sh script should make it easier to repeatedly backup on a regular schedule.

What to backup?

Most of your configuration is stored in Linux .___ config files, often in your ~/ folder. You may also want to backup your /etc/ssh/ssh_config and other similar system config files, though do be careful with complex config files that contain instance-specific keys/data/etc.

You can generate your apt package list using dpkg-query -f '${binary:Package}\n' -W and could echo into a file which you can also backup.

Where to backup to?

You can then copy these tar archives off to a separate location. We recommend copying into a Windows-accessible folder (e.g. /mnt/c/backups) from which you can copy it to OneDrive, DropBox, external HDD, FTP somewhere else, etc.

Update 2020-02-18 Update

In Windows 10 1903, we shipped an updated wsl.exe tool that includes the ability to -export/-import the contents of a distro to/from an external archive, greatly simplifying the process of backing up and/or moving distros between machines!

HTH.


To backup and restore a complete WSL linux distribution, you may try to (1) create a .tar.gz with your filesystem and (2) restore the copy using some installer tools available in the Internet:

  • LxRunOffline has options to duplicate and install custom linux distributions. You may install a distro from a .tar.gz file.
  • WSL-DistroLauncher allows you to install a distribution from a rootfs.tar.gz.
  • WSLInstall, another linux installer for WSL.

Creating the backup

Based on a issue report, you may create a backup using the tar in the WSL. Note that you must ignore some folders (e.g. /mnt) in the copy.

# cd /
# tar vzcpf /mnt/c/tmp/ubuntu_`date +%Y%m%d_%H`.tar.gz --exclude=/proc --exclude=/dev --exclude=/mnt --exclude=/media --exclude=/lost+found  --exclude=/tmp --exclude=/sys  --exclude=/run / > /mnt/c/tmp/ubuntu_`date +%Y%m%d_%H`.log 2> /mnt/c/tmp/ubuntu_`date +%Y%m%d_%H`.error

You must create, or move later, the backup file into a valid /mnt/c subfolder. In the example, the files are created into the /mnt/c/tmp.


Restoring using lxRunOffline

Using lxRunOffline, you can install the resulting tar as a new distribution in WSL using the Windows command line.

# lxrunoffline install -n <distro name> -d <installation folder> -f <file>
C:\wsl> lxrunoffline install -n mybackup -d c:\wsl\mybackup -f c:\tmp\ubuntu_20180729_00.tar.gz

To run the backup, you may use the same lxRunOffline

# lxrunoffline run -n <distro name> -w
C:\wsl> lxrunoffline run -n mybackup  -w

Restoring using a DistroLauncher

There are many launchers for linux distributions based on the Microsoft example. I think you may try the Yuk7 version.

You must download a distribution file and the launcher.exe. To use the same distribution file mentioned above, you must rename the backup file to rootfs.tar.gz and the launcher to the distribution name you want. Later you must run the launcher as an Administrator (I got errors running it as a normal user)

c:\wsl> ren launcher.exe mybackup.exe
c:\wsl> ren .\ubuntu_20180729_00.tar.gz rootfs.tar.gz
c:\wsl> .\mybackup.exe

The first time you run the launcher, it installs and run the distribution. The next time, it runs the linux distribution.