Skip to content

Instantly share code, notes, and snippets.

@tonymet
Last active January 31, 2024 21:34
Show Gist options
  • Save tonymet/816290ca37eab78353c9b760d127ee31 to your computer and use it in GitHub Desktop.
Save tonymet/816290ca37eab78353c9b760d127ee31 to your computer and use it in GitHub Desktop.
backup wsl to onedrive
# backup wsl contents on a schedule
# set & truncate $logfile
$logfile="$HOME\backup-wsl.log.txt"
if ((Get-Item $logfile).length -gt 128000){
Remove-Item $logfile
}
$src="\\wsl.localhost\Debian\home\${env:Username}\sotion"
$dest=$(Join-Path ${env:OneDrive} "Documents\Sotion")
Write-EventLog -LogName Application -Source "Backup-WSL" -EventID 3001 -Message "Starting Backup"
$t0=(Get-Date)
robocopy $src $dest /W:1 /R:0 /E /xd node_modules /NFL /NDL /LOG+:$logfile
$exitcoderobo=$LASTEXITCODE
$t1=(Get-Date)
$delta =($t1-$t0)
$message="Finished Backup. Duration = $delta"
if($exitcoderobo -ne 0){
$message="ERROR: Finished Backup. Backup failed. Duration = $delta . Check $logfile for error"
}
Write-EventLog -LogName Application -Source "Backup-WSL" -EventID 3001 -Message $message
exit $exitcoderobo
New-EventLog -LogName Application -Source "Backup-WSL"
$time = New-ScheduledTaskTrigger -Daily -At 2pm
$user = (whoami)
$action = New-ScheduledTaskAction -Execute "${env:ProgramFiles}\PowerShell\7\pwsh.exe" -Argument "-noninteractive -nologo -noprofile $HOME\scripts\backup-wsl\backup-wsl.ps1"
Register-ScheduledTask -TaskName "Backup-WSL" -Trigger $Time -User $User -Action $action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment