85

My problem is that I have to reset or reinstall windows because of some problems but I spent a lot of time to configure WSL for development and don't want to lose it.

Is there any way of backing up the whole WSL with it's settings and installed modules, plugins, users etc and restore it after resetting or reinstalling windows? I searched a lot this topic but i couldn't find any useful information.

Otherwise if there's no way to backup, do windows 10 reset will delete WSL totally?

0

4 Answers 4

103

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.

7
  • 1
    wsl --import can also import tar.gz files. Those commands internally launches %SystemRoot%\System32\lxss\tools\bsdtar ELF64 binary.
    – Biswapriyo
    May 12, 2019 at 12:28
  • 1
    anecdata, to help those of a nervous disposition: wsl --export may take longer than you might expect. On my machine, it sat for roughly 10 minutes having written only a few megabytes, then finally wrote out a 3.2 GB tar file.
    – scruss
    Oct 31, 2019 at 16:43
  • WSL2 has some serious RAM hogging tendencies, but this was just... Exporting a reasonably old distro made RAM usage shoot up to 29 GB and slowed my machine to a crawl! May 31, 2020 at 17:54
  • 1
    @DividedByZero Actually, there's a workaround for that issue. Please refer to github.com/microsoft/WSL/issues/4166#issuecomment-526725261
    – David Jung
    Jul 29, 2020 at 3:37
  • 2
    apparently it kills the current running wsl when you run this
    – AmanicA
    Sep 1, 2020 at 22:27
22

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.

4

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.

1
  • Thank you! - Yuk7's wsdl launcher kept failing on launch, and I didn't see the lxRunOffline issue. This was the only place I found that documented the correct tar command that fixed my wsldl issue.
    – AndrewD
    Nov 29, 2018 at 23:08
0

While Jaime's answer is perfect, somehow the /tmp folder is not recreated when launcher.exe is used for installing the tar ball (I am specifically talking about moving the WSL installation from one machine to another).

So one must create /tmp and assign required rights.

The one approach that worked for me is:

https://www.cyberciti.biz/faq/mysqld-innodb-error-unable-to-create-temporary-file/

# chown root:root /tmp
# chmod 1777 /tmp
## test it ##
# /etc/init.d/mysqld start

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.