- Fixed 4 broken markdown links (bad relative paths in See Also sections) - Corrected n8n port binding to 127.0.0.1:5678 (matches actual deployment) - Updated SnapRAID article with actual majorhome paths (/majorRAID, disk1-3) - Converted 67 Obsidian wikilinks to relative markdown links or plain text - Added YAML frontmatter to 35 articles missing it entirely - Completed frontmatter on 8 articles with missing fields Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
87 lines
2.9 KiB
Markdown
87 lines
2.9 KiB
Markdown
---
|
||
title: WSL2 Backup via PowerShell Scheduled Task
|
||
domain: linux
|
||
category: distro-specific
|
||
tags:
|
||
- wsl2
|
||
- windows
|
||
- backup
|
||
- powershell
|
||
- majorrig
|
||
status: published
|
||
created: '2026-03-16'
|
||
updated: '2026-03-16'
|
||
---
|
||
|
||
# WSL2 Backup via PowerShell Scheduled Task
|
||
|
||
WSL2 distributions are stored as a VHDX file on disk. Unlike traditional VMs, there's no built-in snapshot or backup mechanism. This article covers a simple weekly backup strategy using `wsl --export` and a PowerShell scheduled task.
|
||
|
||
## The Short Answer
|
||
|
||
Save this as `C:\Users\majli\Scripts\backup-wsl.ps1` and register it as a weekly scheduled task.
|
||
|
||
## Backup Script
|
||
|
||
```powershell
|
||
$BackupDir = "D:\WSL\Backups"
|
||
$Date = Get-Date -Format "yyyy-MM-dd"
|
||
$BackupFile = "$BackupDir\FedoraLinux-43-$Date.tar"
|
||
$MaxBackups = 3
|
||
|
||
New-Item -ItemType Directory -Force -Path $BackupDir | Out-Null
|
||
|
||
# Must shut down WSL first — export fails if VHDX is locked
|
||
Write-Host "Shutting down WSL2..."
|
||
wsl --shutdown
|
||
Start-Sleep -Seconds 5
|
||
|
||
Write-Host "Backing up FedoraLinux-43 to $BackupFile..."
|
||
wsl --export FedoraLinux-43 $BackupFile
|
||
|
||
if ($LASTEXITCODE -eq 0) {
|
||
Write-Host "Backup complete: $BackupFile"
|
||
Get-ChildItem "$BackupDir\FedoraLinux-43-*.tar" |
|
||
Sort-Object LastWriteTime -Descending |
|
||
Select-Object -Skip $MaxBackups |
|
||
Remove-Item -Force
|
||
Write-Host "Cleanup done. Keeping last $MaxBackups backups."
|
||
} else {
|
||
Write-Host "ERROR: Backup failed!"
|
||
}
|
||
```
|
||
|
||
## Register the Scheduled Task
|
||
|
||
Run in PowerShell as Administrator:
|
||
|
||
```powershell
|
||
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" `
|
||
-Argument "-NonInteractive -File C:\Users\majli\Scripts\backup-wsl.ps1"
|
||
$Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 2am
|
||
$Settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -RunOnlyIfNetworkAvailable:$false
|
||
Register-ScheduledTask -TaskName "WSL2 Backup - FedoraLinux43" `
|
||
-Action $Action -Trigger $Trigger -Settings $Settings `
|
||
-RunLevel Highest -Force
|
||
```
|
||
|
||
## Restore from Backup
|
||
|
||
```powershell
|
||
wsl --unregister FedoraLinux-43
|
||
wsl --import FedoraLinux-43 D:\WSL\Fedora43 D:\WSL\Backups\FedoraLinux-43-YYYY-MM-DD.tar
|
||
```
|
||
|
||
Then fix the default user — after import WSL resets to root. See [WSL2 Instance Migration](wsl2-instance-migration-fedora43.md) for the `/etc/wsl.conf` fix.
|
||
|
||
## Gotchas
|
||
|
||
- **`wsl --export` fails with `ERROR_SHARING_VIOLATION` if WSL is running.** The script includes `wsl --shutdown` before export to handle this. Any active WSL sessions will be terminated — schedule the task for a time when WSL is idle (2am works well).
|
||
- **Backblaze picks up D:\WSL\Backups\ automatically** if D: drive is in scope — provides offsite backup without extra config.
|
||
- **Each backup tar is ~500MB–1GB** depending on what's installed. Keep MaxBackups at 3 to balance retention vs disk usage.
|
||
|
||
## See Also
|
||
|
||
- [WSL2 Instance Migration](wsl2-instance-migration-fedora43.md)
|
||
- [WSL2 Training Environment Rebuild](wsl2-rebuild-fedora43-training-env.md)
|