# WSL Ubuntu 备份与还原简明教程
本文介绍如何使用 PowerShell 命令快速备份和还原 WSL 中的 Ubuntu 22.04 系统。
## 一、备份 WSL Ubuntu
### 操作步骤:
1. 打开 **PowerShell(管理员身份)**
2. 执行以下命令:
```powershell
# 添加日期标签,避免覆盖旧备份
$date = Get-Date -Format "yyyyMMdd-HHmm"
# 创建备份目录
New-Item -ItemType Directory -Force -Path D:\wsl-backup
# 导出 Ubuntu 系统
wsl --export Ubuntu-22.04 "D:\wsl-backup\ubuntu22-$date.tar"
```
**说明**:备份文件将保存至 `D:\wsl-backup\` 目录,文件名包含时间戳(如 `ubuntu22-20250111-1430.tar`)
## 二、还原 WSL Ubuntu
### 操作步骤:
1. **卸载现有实例**(可选):
```powershell
wsl --unregister Ubuntu-22.04
```
2. **创建安装目录并导入**:
```powershell
mkdir D:\wsl\ubuntu-new -Force
wsl --import Ubuntu-22.04 D:\wsl\ubuntu-new D:\wsl-backup\ubuntu22.tar
```
3. **验证安装**:
```powershell
wsl -l -v
```
4. **设置默认用户**:
```powershell
wsl -d Ubuntu-22.04 -u root
```
在 WSL 终端中设置 root 密码:
```bash
passwd
```
## 三、创建桌面快捷方式
执行以下 PowerShell 命令一键创建桌面快捷方式:
```powershell
$shortcutPath = "$env:USERPROFILE\Desktop\Ubuntu 22.04.lnk"
$targetPath = "C:\Windows\System32\wsl.exe"
$arguments = "-d Ubuntu-22.04"
$WshShell = New-Object -ComObject WScript.Shell
$shortcut = $WshShell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $targetPath
$shortcut.Arguments = $arguments
$shortcut.IconLocation = "C:\Windows\System32\wsl.exe"
$shortcut.Description = "Ubuntu 22.04 WSL"
$shortcut.WorkingDirectory = "%USERPROFILE%"
$shortcut.Save()
Write-Host "桌面快捷方式已创建" -ForegroundColor Green
```
## 注意事项
1. **备份前**:确保 WSL 实例已关闭,可执行 `wsl --shutdown`
2. **存储路径**:可自行修改 `D:\wsl-backup` 和 `D:\wsl\ubuntu-new` 为其他目录
3. **还原时**:如不需要保留旧系统,可跳过卸载步骤,直接导入为新实例名称
---
**备份建议**:定期备份重要数据,建议每月至少备份一次系统,并在系统更新后创建新备份。