backup_to_server.psm1 3.5 KB

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