Post

Como fazer backup e restore de distros no WSL2

Como fazer backup e restore de distros no WSL2

Como fazer backup e restore de distros no WSL2

Quando você usa o WSL2 para desenvolvimento, ter um fluxo repetível de backup/restore evita reinstalar ferramentas ou perder configuração. Exportar/importar é o jeito mais simples e confiável.

Veja um passo a passo enxuto para backup e restore das distros no WSL2.

Step 1: Backing Up Your WSL2 Distribution

O WSL2 guarda o sistema de arquivos em discos virtuais .vhdx, mas o jeito mais rápido de fazer backup é exportar a distro para um tarball com wsl --export.

Faça backup da distro:

  1. Open PowerShell or Command Prompt.
  2. Run the following command to export your WSL2 distribution to a .tar file:

    1
    
     wsl --export <DistributionName> <BackupFilePath>
    

    Replace <DistributionName> with the name of your WSL2 distribution (e.g., Ubuntu-22.04), and replace <BackupFilePath> with the path where you want to save the backup file.

    Example:

    1
    
     wsl --export Ubuntu-22.04 C:\WSLBackups\UbuntuBackup.tar
    

This command creates a tarball of your WSL2 distribution, containing all files, packages, and configurations.

Step 2: Restoring Your WSL2 Distribution

Restaure a distro a partir do .tar, na mesma máquina ou em outra:

  1. Open PowerShell or Command Prompt.
  2. Run the following command to import your WSL2 distribution from the backup .tar file:

    1
    
     wsl --import <NewDistributionName> <InstallLocation> <BackupFilePath>
    

    Replace <NewDistributionName> with the name you want for the restored distribution, <InstallLocation> with the folder where you want to install the distribution, and <BackupFilePath> with the path to your backup file.

    Example:

    1
    
     wsl --import Ubuntu-Restored C:\WSL\RestoredDistribution C:\WSLBackups\UbuntuBackup.tar
    

This will restore your WSL2 distribution from the backup file to the specified installation location.

Step 3: Verifying the Restore

Once the restore is complete, you can verify the restored WSL2 distribution by listing all available distributions with the following command:

1
wsl --list --verbose

You should see the newly restored distribution in the list, and you can start it by simply running:

1
wsl -d <NewDistributionName>

Example:

1
wsl -d Ubuntu-Restored

Step 4: Optional - Removing Old Distributions

If you want to clean up old distributions, you can use the wsl --unregister command to remove them. Be careful, as this will delete the distribution and all its data.

1
wsl --unregister <DistributionName>

Example:

1
wsl --unregister Ubuntu-22.04

This can be useful when you want to free up space after restoring a distribution.

Final Tips

  • Automation: You can automate the backup process by setting up a script to regularly export your WSL2 distribution and store it in a safe location, such as an external drive or cloud storage.
  • Storage Location: Make sure to store your backup .tar files in a secure location, especially if they contain sensitive data.
  • WSL Version: Keep in mind that these steps apply to WSL2. The wsl --export and wsl --import commands are specific to WSL2 distributions.

By following these steps, you can ensure that your WSL2 environment is safely backed up and can be restored whenever needed.


References:

Esta postagem está licenciada sob CC BY 4.0 pelo autor.