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

29

16

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?

emipac

Posted 2017-01-08T15:52:13.643

Reputation: 293

I would imagine, its as simply as backing up, %USERPROFILE%/AppData/Local/lxss. Have you tried that? – Ramhound – 2017-01-08T21:20:36.687

Yes, i tried, but it's functional only partly with a lot of errors. – emipac – 2017-01-09T11:59:35.670

Answers

28

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.

Bob

Posted 2017-01-08T15:52:13.643

Reputation: 51 526

wsl --import can also import tar.gz files. Those commands internally launches %SystemRoot%\System32\lxss\tools\bsdtar ELF64 binary. – Biswapriyo – 2019-05-12T12:28:46.053

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 – 2019-10-31T16:43:46.433

I think this is the best solution and should be accepted as the answer. – user1602 – 2020-01-26T06:53:12.530

16

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.

Richard Turner

Posted 2017-01-08T15:52:13.643

Reputation: 1 233

2

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.

Jaime

Posted 2017-01-08T15:52:13.643

Reputation: 1 187

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 – 2018-11-29T23:08:56.893

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

Rajesh Thampi

Posted 2017-01-08T15:52:13.643

Reputation: 101