Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

62 lignes
2.5KB

  1. . .\config.ps1
  2. . .\secrets.ps1
  3. # download restic
  4. if(-not (Test-Path $ResticExe)) {
  5. $url = $null
  6. if([Environment]::Is64BitOperatingSystem){
  7. $url = "https://github.com/restic/restic/releases/download/v0.11.0/restic_0.11.0_windows_amd64.zip"
  8. }
  9. else {
  10. $url = "https://github.com/restic/restic/releases/download/v0.11.0/restic_0.11.0_windows_386.zip"
  11. }
  12. $output = Join-Path $InstallPath "restic.zip"
  13. Invoke-WebRequest -Uri $url -OutFile $output
  14. Expand-Archive -LiteralPath $output $InstallPath
  15. Remove-Item $output
  16. Get-ChildItem *.exe | Rename-Item -NewName $ExeName
  17. }
  18. # Create log directory if it doesn't exit
  19. if(-not (Test-Path $LogPath)) {
  20. New-Item -ItemType Directory -Force -Path $LogPath | Out-Null
  21. Write-Output "[[Init]] Repository successfully initialized."
  22. }
  23. # Create the local exclude file
  24. if(-not (Test-Path $LocalExcludeFile)) {
  25. New-Item -Type File -Path $LocalExcludeFile | Out-Null
  26. }
  27. # Initialize the restic repository
  28. & $ResticExe --verbose init
  29. if($?) {
  30. Write-Output "[[Init]] Repository successfully initialized."
  31. }
  32. else {
  33. Write-Warning "[[Init]] Repository initialization failed. Check errors and resolve."
  34. }
  35. # Scheduled Windows Task Scheduler to run the backup
  36. $backup_task_name = "Restic Backup"
  37. $backup_task = Get-ScheduledTask $backup_task_name -ErrorAction SilentlyContinue
  38. if($null -eq $backup_task) {
  39. try {
  40. $task_action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NonInteractive -NoLogo -NoProfile -Command ".\backup.ps1; exit $LASTEXITCODE"' -WorkingDirectory $InstallPath
  41. $task_user = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -RunLevel Highest
  42. $task_settings = New-ScheduledTaskSettingsSet -RestartCount 4 -RestartInterval (New-TimeSpan -Minutes 15) -ExecutionTimeLimit (New-TimeSpan -Days 3) -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -MultipleInstances IgnoreNew -IdleDuration 0 -IdleWaitTimeout 0 -StartWhenAvailable -RestartOnIdle
  43. $task_trigger = New-ScheduledTaskTrigger -Daily -At 4:00am
  44. Register-ScheduledTask $backup_task_name -Action $task_action -Principal $task_user -Settings $task_settings -Trigger $task_trigger | Out-Null
  45. Write-Output "[[Scheduler]] Backup task scheduled."
  46. }
  47. catch {
  48. Write-Warning "[[Scheduler]] Scheduling failed."
  49. }
  50. }
  51. else {
  52. Write-Warning "[[Scheduler]] Backup task not scheduled: there is already a task with the name '$backup_task_name'."
  53. }