Difference between revisions of "HOW-TO: Backup or Move WSL2 Distro"

From Tayledras
Jump to: navigation, search
(Created page with "<span style="font-size: 21px;">Move WSL to another drive</span> ==<a href="https://www.jskap.com/blog/install-move-wsl-distro-from-c-drive-to-another-drive/#Move-WSL-to-anothe...")
 
(Backup and Restore WSL2 Distros)
 
(36 intermediate revisions by the same user not shown)
Line 1: Line 1:
<span style="font-size: 21px;">Move WSL to another drive</span>
+
[[File:Wsl-move.png|right|middle|thumb|Moving a WSL2 distro using lxrunoffline]]
==<a href="https://www.jskap.com/blog/install-move-wsl-distro-from-c-drive-to-another-drive/#Move-WSL-to-another-drive" aria-label="Move WSL to another drive permalink" class="anchor after"><svg aria-hidden="true" focusable="false" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>==
+
[[File:WSL2-multi-instance.png|right|middle|thumb|Running multiple distros and instances under WSL2 with Windows Terminal]]
  
* Install<span></span><code class="language-text">LxRunOffline</code><span></span>tool.<span></span><code class="language-text">choco install lxrunoffline</code>
+
== References ==
* <code class="language-text">lxrunoffline list</code><span></span>List all the installed distros on your system. I get output like this.
 
  
<pre class="language-bash"><code class="language-bash">Microsoft Windows <span class="token punctuation">[</span>Version <span class="token number">10.0</span>.19041.207<span class="token punctuation">]</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token number">2020</span> Microsoft Corporation. All rights reserved.D:<span class="token punctuation">\</span>work<span class="token operator">></span>lxrunoffline listUbuntu-18.04docker-desktopdocker-desktop-dataD:<span class="token punctuation">\</span>work<span class="token operator">></span></code></pre>
+
* https://www.jskap.com/notes/how-to-move-wsl2-disto/
* <code class="language-text">lxrunoffline move -n Ubuntu-18.04 -d G:\wsl\</code><span></span>. Move your distro to another drive. My distro name here is<span></span><code class="language-text">Ubuntu-18.04</code>. I am moving it to<span></span><code class="language-text">G drive</code><span></span>and<span></span><code class="language-text">wsl</code><span></span>folder inside it. It would take some time to finish moving.
+
* https://www.jskap.com/blog/install-move-wsl-distro-from-c-drive-to-another-drive/
You can use the same move command to move your distro from default location to a different location inside C Drive.
+
* https://www.howtogeek.com/426562/how-to-export-and-import-your-linux-systems-on-windows-10/
 +
 
 +
== Related ==
 +
* [[Windows Terminal - settings.json]]
 +
* [[HOW-TO: Rename WSL Distro Name]]
 +
 
 +
==Backup and Restore WSL2 Distros==
 +
<syntaxhighlight lang="Shell">
 +
## Listing all WSL2 Distros using Windows Powershell running as Administrator
 +
PS C:\WINDOWS\system32> wsl --list -v
 +
  NAME                  STATE          VERSION
 +
* Ubuntu                Running        2
 +
  Fedora                Running        2
 +
  CentOS                Running        2
 +
  docker-desktop        Running        2
 +
  docker-desktop-data    Running        2
 +
 
 +
## Shutdown all WSL2 Distros using Windows Powershell running as Administrator
 +
PS C:\WINDOWS\system32> wsl --shutdown
 +
 
 +
## Listing all WSL2 Distros using Windows Powershell running as Administrator
 +
PS C:\WINDOWS\system32> wsl --list -v
 +
  NAME                  STATE          VERSION
 +
* Ubuntu                Stopped        2
 +
  Fedora                Stopped        2
 +
  CentOS                Stopped        2
 +
  docker-desktop        Stopped        2
 +
  docker-desktop-data    Stopped        2
 +
 
 +
## Export using Windows Powershell running as Administrator
 +
## EXAMPLE: wsl --export distro_name file_name.tar
 +
PS C:\WINDOWS\system32> wsl --export Ubuntu C:\Shared\WSL2\Backups\Ubuntu.tar
 +
PS C:\WINDOWS\system32> wsl --export Fedora C:\Shared\WSL2\Backups\Fedora.tar
 +
PS C:\WINDOWS\system32> wsl --export CentOS C:\Shared\WSL2\Backups\CentOS.tar
 +
 
 +
## Remove old WSL instances
 +
wsl --unregister Ubuntu
 +
wsl --unregister Fedora
 +
wsl --unregister CentOS
 +
 
 +
## Import using Windows Powershell running as Administrator
 +
## EXAMPLE: wsl --import distro_name install_location file_name.tar
 +
wsl --import Ubuntu C:\Shared\WSL2\Ubuntu C:\Shared\WSL2\Backups\Ubuntu.tar
 +
wsl --import Fedora C:\Shared\WSL2\Fedora C:\Shared\WSL2\Backups\Fedora.tar
 +
wsl --import CentOS C:\Shared\WSL2\CentOS C:\Shared\WSL2\Backups\CentOS.tar
 +
 
 +
## Listing backups under /Shared/WSL2/Backups
 +
Shared/WSL2/Backups on ☁️  (us-east-1) on ☁️ [redacted]
 +
❯ ls -lah
 +
  rwxrwxrwx  1  root  root    4 KiB  Sun Aug 15 06:22:16 2021    ./
 +
  rwxrwxrwx  1  root  root    4 KiB  Mon Jun 28 07:06:34 2021    ../
 +
  rwxrwxrwx  1  root  root  553 B    Thu Aug 12 16:04:20 2021    backup-wsl2-distros.ps1
 +
  rwxrwxrwx  1  root  root    2 GiB  Thu Aug 12 15:53:59 2021    CentOS.tar
 +
  rwxrwxrwx  1  root  root  557 B    Mon Oct 19 15:10:28 2020    fedora.png
 +
  rwxrwxrwx  1  root  root    4 GiB  Thu Aug 12 15:53:00 2021    Fedora.tar
 +
  rwxrwxrwx  1  root  root  787 B    Mon Oct 19 15:09:44 2020    redhat.png
 +
  rwxrwxrwx  1  root  root  838 B    Thu Aug 12 16:04:09 2021    restore-wsl2-distros.ps1
 +
  rwxrwxrwx  1  root  root    20 KiB  Sun Jun 27 22:01:44 2021    settings.json
 +
  rwxrwxrwx  1  root  root  560 B    Mon Oct 19 15:11:24 2020    ubuntu.png
 +
  rwxrwxrwx  1  root  root    12 GiB  Thu Aug 12 15:51:57 2021    Ubuntu.tar
 +
</syntaxhighlight>
 +
 
 +
== Move WSL to Another Location ==
 +
 
 +
=== Installing LxRunOffline [DEPRECATED with `wsl` replacing `lxrunoffline` functions] ===
 +
 
 +
* Install '''LxRunOffline''' via [https://chocolatey.org/ '''Choco''' ''(Package Manager for Windows)'']
 +
 
 +
<syntaxhighlight lang="Shell">
 +
## From within your Windows PowerShell running as Administrator
 +
PS C:\WINDOWS\system32> choco install lxrunoffline
 +
 
 +
Chocolatey v0.10.15
 +
Installing the following packages:
 +
lxrunoffline
 +
By installing you accept licenses for the packages.
 +
Progress: Downloading lxrunoffline 3.5.0... 100%
 +
 
 +
lxrunoffline v3.5.0 [Approved]
 +
lxrunoffline package files install completed. Performing other installation steps.
 +
The package lxrunoffline wants to run 'chocolateyInstall.ps1'.
 +
Note: If you don't run this script, the installation will fail.
 +
Note: To confirm automatically next time, use '-y' or consider:
 +
choco feature enable -n allowGlobalConfirmation
 +
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): A
 +
 
 +
Progress: 100% - Running                                                                                              Microsoft.Dism.Commands.ImageObject
 +
Downloading lxrunoffline
 +
  from 'https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip'
 +
Progress: 100% - Completed download of C:\Users\kenfo\AppData\Local\Temp\lxrunoffline\3.5.0\LxRunOffline-v3.5.0-msvc.zip (2.54 MB).
 +
Download of LxRunOffline-v3.5.0-msvc.zip (2.54 MB) completed.
 +
Hashes match.
 +
Extracting C:\Users\kenfo\AppData\Local\Temp\lxrunoffline\3.5.0\LxRunOffline-v3.5.0-msvc.zip to C:\tools\lxrunoffline...
 +
C:\tools\lxrunoffline
 +
PATH environment variable does not have C:\tools\lxrunoffline in it. Adding...
 +
Environment Vars (like PATH) have changed. Close/reopen your shell to
 +
see the changes (or in powershell/cmd.exe just type `refreshenv`).
 +
The install of lxrunoffline was successful.
 +
  Software installed to 'C:\tools\lxrunoffline'
 +
 
 +
Chocolatey installed 1/1 packages.
 +
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
 +
</syntaxhighlight>
 +
 
 +
=== Listing WSL2 Distros using LxRunOffline [DEPRECATED with `wsl` replacing `lxrunoffline` functions] ===
 +
 
 +
* '''lxrunoffline list''' - List all the installed distros on your system.
 +
 
 +
<syntaxhighlight lang="bash">
 +
## From within your Windows Command Prompt (cmd.exe)
 +
Microsoft Windows [Version 10.0.19042.572]
 +
(c) 2020 Microsoft Corporation. All rights reserved.
 +
 
 +
C:\Users\kenfo>lxrunoffline list
 +
Ubuntu-20.04
 +
 
 +
C:\Users\kenfo>
 +
</syntaxhighlight>
 +
 
 +
=== Moving Distro Locations using LxRunOffline [DEPRECATED with `wsl` replacing `lxrunoffline` functions] ===
 +
 
 +
<syntaxhighlight lang="bash">
 +
## From within Windows PowerShell as Administrator
 +
wsl --shutdown
 +
 
 +
## From within the Windows Command Prommpt (cmd.exe)
 +
lxrunoffline move -n Ubuntu-20.04 -d M:\Shared\WSL2\ubuntu .
 +
</syntaxhighlight>
 +
 
 +
=== Moving Distro Locations using wsl ===
 +
* Example for moving from internal/default to M:\Shared\WSL\Ubuntu
 +
 
 +
<syntaxhighlight lang="bash">
 +
## From within Windows PowerShell as Administrator
 +
 
 +
## Prep shared drive (used for backup, restore, and storage)
 +
cd M:\Shared\
 +
mkdir -p WSL2\Ubuntu
 +
cd M:\Shared\WSL2\Ubuntu
 +
 
 +
## Export and Unregister (if desired) from WSL
 +
wsl --export Ubuntu ubuntu.tar
 +
wsl --unregister Ubuntu
 +
 
 +
## Import Distro Name to Target from Backup
 +
wsl --import Ubuntu Ubuntu ubuntu.tar
 +
</syntaxhighlight>
 +
 
 +
* Move your distro to another drive. My distro name here is '''Ubuntu'''. I am moving it to '''M:''' drive and '''\Shared\WSL2''' folder inside it. ''(This will take some time to complete, '''lxrunoffline''' doesn't provide feedback during the move.)''
 +
* You can use the same '''move''' command to move your distro from default location to a different location inside C Drive.
 +
 
 +
[[Category:HOW-TO]]
 +
[[Category:Windows_Subsystem_for_Linux_(WSL)]]
 +
[[Category:Windows]]

Latest revision as of 05:15, 15 August 2021

Moving a WSL2 distro using lxrunoffline
Running multiple distros and instances under WSL2 with Windows Terminal

References

Related

Backup and Restore WSL2 Distros

## Listing all WSL2 Distros using Windows Powershell running as Administrator
PS C:\WINDOWS\system32> wsl --list -v
  NAME                   STATE           VERSION
* Ubuntu                 Running         2
  Fedora                 Running         2
  CentOS                 Running         2
  docker-desktop         Running         2
  docker-desktop-data    Running         2

## Shutdown all WSL2 Distros using Windows Powershell running as Administrator
PS C:\WINDOWS\system32> wsl --shutdown

## Listing all WSL2 Distros using Windows Powershell running as Administrator
PS C:\WINDOWS\system32> wsl --list -v
  NAME                   STATE           VERSION
* Ubuntu                 Stopped         2
  Fedora                 Stopped         2
  CentOS                 Stopped         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2

## Export using Windows Powershell running as Administrator
## EXAMPLE: wsl --export distro_name file_name.tar
PS C:\WINDOWS\system32> wsl --export Ubuntu C:\Shared\WSL2\Backups\Ubuntu.tar
PS C:\WINDOWS\system32> wsl --export Fedora C:\Shared\WSL2\Backups\Fedora.tar
PS C:\WINDOWS\system32> wsl --export CentOS C:\Shared\WSL2\Backups\CentOS.tar

## Remove old WSL instances
wsl --unregister Ubuntu
wsl --unregister Fedora
wsl --unregister CentOS

## Import using Windows Powershell running as Administrator
## EXAMPLE: wsl --import distro_name install_location file_name.tar
wsl --import Ubuntu C:\Shared\WSL2\Ubuntu C:\Shared\WSL2\Backups\Ubuntu.tar
wsl --import Fedora C:\Shared\WSL2\Fedora C:\Shared\WSL2\Backups\Fedora.tar
wsl --import CentOS C:\Shared\WSL2\CentOS C:\Shared\WSL2\Backups\CentOS.tar

## Listing backups under /Shared/WSL2/Backups
Shared/WSL2/Backups on ☁️  (us-east-1) on ☁️ [redacted]
❯ ls -lah
  rwxrwxrwx  1  root  root     4 KiB  Sun Aug 15 06:22:16 2021    ./
  rwxrwxrwx  1  root  root     4 KiB  Mon Jun 28 07:06:34 2021    ../
  rwxrwxrwx  1  root  root   553 B    Thu Aug 12 16:04:20 2021    backup-wsl2-distros.ps1
  rwxrwxrwx  1  root  root     2 GiB  Thu Aug 12 15:53:59 2021    CentOS.tar
  rwxrwxrwx  1  root  root   557 B    Mon Oct 19 15:10:28 2020    fedora.png
  rwxrwxrwx  1  root  root     4 GiB  Thu Aug 12 15:53:00 2021    Fedora.tar
  rwxrwxrwx  1  root  root   787 B    Mon Oct 19 15:09:44 2020    redhat.png
  rwxrwxrwx  1  root  root   838 B    Thu Aug 12 16:04:09 2021    restore-wsl2-distros.ps1
  rwxrwxrwx  1  root  root    20 KiB  Sun Jun 27 22:01:44 2021    settings.json
  rwxrwxrwx  1  root  root   560 B    Mon Oct 19 15:11:24 2020    ubuntu.png
  rwxrwxrwx  1  root  root    12 GiB  Thu Aug 12 15:51:57 2021    Ubuntu.tar

Move WSL to Another Location

Installing LxRunOffline [DEPRECATED with `wsl` replacing `lxrunoffline` functions]

## From within your Windows PowerShell running as Administrator
PS C:\WINDOWS\system32> choco install lxrunoffline

Chocolatey v0.10.15
Installing the following packages:
lxrunoffline
By installing you accept licenses for the packages.
Progress: Downloading lxrunoffline 3.5.0... 100%

lxrunoffline v3.5.0 [Approved]
lxrunoffline package files install completed. Performing other installation steps.
The package lxrunoffline wants to run 'chocolateyInstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): A

Progress: 100% - Running                                                                                               Microsoft.Dism.Commands.ImageObject
Downloading lxrunoffline
  from 'https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip'
Progress: 100% - Completed download of C:\Users\kenfo\AppData\Local\Temp\lxrunoffline\3.5.0\LxRunOffline-v3.5.0-msvc.zip (2.54 MB).
Download of LxRunOffline-v3.5.0-msvc.zip (2.54 MB) completed.
Hashes match.
Extracting C:\Users\kenfo\AppData\Local\Temp\lxrunoffline\3.5.0\LxRunOffline-v3.5.0-msvc.zip to C:\tools\lxrunoffline...
C:\tools\lxrunoffline
PATH environment variable does not have C:\tools\lxrunoffline in it. Adding...
Environment Vars (like PATH) have changed. Close/reopen your shell to
 see the changes (or in powershell/cmd.exe just type `refreshenv`).
 The install of lxrunoffline was successful.
  Software installed to 'C:\tools\lxrunoffline'

Chocolatey installed 1/1 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Listing WSL2 Distros using LxRunOffline [DEPRECATED with `wsl` replacing `lxrunoffline` functions]

  • lxrunoffline list - List all the installed distros on your system.
## From within your Windows Command Prompt (cmd.exe)
Microsoft Windows [Version 10.0.19042.572]
(c) 2020 Microsoft Corporation. All rights reserved.

C:\Users\kenfo>lxrunoffline list
Ubuntu-20.04

C:\Users\kenfo>

Moving Distro Locations using LxRunOffline [DEPRECATED with `wsl` replacing `lxrunoffline` functions]

## From within Windows PowerShell as Administrator
wsl --shutdown

## From within the Windows Command Prommpt (cmd.exe)
lxrunoffline move -n Ubuntu-20.04 -d M:\Shared\WSL2\ubuntu .

Moving Distro Locations using wsl

  • Example for moving from internal/default to M:\Shared\WSL\Ubuntu
## From within Windows PowerShell as Administrator

## Prep shared drive (used for backup, restore, and storage)
cd M:\Shared\
mkdir -p WSL2\Ubuntu
cd M:\Shared\WSL2\Ubuntu

## Export and Unregister (if desired) from WSL
wsl --export Ubuntu ubuntu.tar
wsl --unregister Ubuntu

## Import Distro Name to Target from Backup
wsl --import Ubuntu Ubuntu ubuntu.tar
  • Move your distro to another drive. My distro name here is Ubuntu. I am moving it to M: drive and \Shared\WSL2 folder inside it. (This will take some time to complete, lxrunoffline doesn't provide feedback during the move.)
  • You can use the same move command to move your distro from default location to a different location inside C Drive.