backup_to_server.psm1 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Custom backup script
  2. function backup_to_server {
  3. param(
  4. [switch]$skipappdir,
  5. [switch]$noupload,
  6. [switch]$skipoutlook)
  7. . (Join-Path $PSScriptRoot handle_exclude_lists.ps1)
  8. # Backup folder
  9. $backup_dir = "$HOME"
  10. if (!(Test-Path $backup_dir) ) { mkdir $backup_dir }
  11. # Make sure we have an authentication key
  12. $key_file = "$HOME\.ssh\bethany_putty.ppk"
  13. if (!(Test-Path $key_file) ) { write-host "Unable to find a key for connecting to the server"; return }
  14. # Setup to use 7zip and add WinSCP to the path
  15. $env:Path += ";C:\Program Files\7-Zip;C:\Program Files (x86)\WinSCP"
  16. if (! (Test-Path 'C:\Program Files\7-Zip\7z.exe')) {
  17. Write-Error "Unable to locate 7-zip program file"
  18. exit 1
  19. }
  20. Set-Alias sz 'C:\Program Files\7-Zip\7z.exe'
  21. # Create a compressed version of the App Dir
  22. if (!($skipappdir)) {
  23. # Create the main backup file
  24. $backup_file = "$backup_dir\appdir.7z"
  25. # Make sure we start in the home folder
  26. Set-Location $HOME
  27. # Load the exclude list
  28. $exclude = parse_appdata_exclude
  29. # Generate the compressed backup
  30. Invoke-Command -ScriptBlock { sz a -bb0 -t7z $args[0] $args[1] "$HOME\AppData"
  31. } -ArgumentList @($exclude, $backup_file)
  32. }
  33. if (!($noupload)) {
  34. # Create a timestamp file to confirm when the last backup was done
  35. Set-Content -Value (Get-Date) -Path ("$backup_dir\updated.txt")
  36. Set-Location $HOME
  37. # Config file
  38. $config_file = Join-Path $env:TEMP backup_script.txt
  39. Set-Content $config_file "open sftp://bethany@gandalf/ -hostkey=""ssh-ed25519 256 55:2d:b5:25:58:19:5d:cd:14:39:36:ec:c9:7c:ee:08"" -privatekey=""$key_file"""
  40. Add-Content $config_file "option batch continue"
  41. # Sync to server
  42. $remote_directory = "/data2/backups/bethany/{0}/Profiles/" -f ( $env:COMPUTERNAME )
  43. $local_directory = "c:\Users"
  44. $mask = parse_sync_exclude
  45. $config = "synchronize remote -delete -filemask=""$mask"" ""$local_directory"" $remote_directory"
  46. Add-Content $config_file "$config"
  47. # If requested, sync the outlook data files as well
  48. if (!($skipoutlook)) {
  49. $local = "$HOME\AppData\Local\Microsoft\Outlook\*.ost"
  50. $remote = "/data2/backups/bethany/{0}/" -f $env:COMPUTERNAME
  51. $config = "put -resume ""$local"" $remote"
  52. Add-Content $config_file $config
  53. }
  54. # Complete the config file and execute
  55. Add-Content $config_file "exit"
  56. WinSCP.com /script=$config_file
  57. }
  58. }
  59. Export-ModuleMember backup_to_server