12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- # Custom backup script
- function backup_to_server {
- param(
- [switch]$skipappdir,
- [switch]$noupload,
- [switch]$skipoutlook)
- . (Join-Path $PSScriptRoot handle_exclude_lists.ps1)
- # Backup folder
- $backup_dir = "$HOME"
- if (!(Test-Path $backup_dir) ) { mkdir $backup_dir }
- # Make sure we have an authentication key
- $key_file = "$HOME\.ssh\bethany_putty.ppk"
- if (!(Test-Path $key_file) ) { write-host "Unable to find a key for connecting to the server"; return }
- # Setup to use 7zip and add WinSCP to the path
- $env:Path += ";C:\Program Files\7-Zip;C:\Program Files (x86)\WinSCP"
- if (! (Test-Path 'C:\Program Files\7-Zip\7z.exe')) {
- Write-Error "Unable to locate 7-zip program file"
- exit 1
- }
- Set-Alias sz 'C:\Program Files\7-Zip\7z.exe'
- # Create a compressed version of the App Dir
- if (!($skipappdir)) {
- # Create the main backup file
- $backup_file = "$backup_dir\appdir.7z"
- # Make sure we start in the home folder
- Set-Location $HOME
- # Load the exclude list
- $exclude = parse_appdata_exclude
- # Generate the compressed backup
- Invoke-Command -ScriptBlock { sz a -bb0 -t7z $args[0] $args[1] "$HOME\AppData"
- } -ArgumentList @($exclude, $backup_file)
- }
- if (!($noupload)) {
- # Create a timestamp file to confirm when the last backup was done
- Set-Content -Value (Get-Date) -Path ("$backup_dir\updated.txt")
- Set-Location $HOME
- # Config file
- $config_file = Join-Path $env:TEMP backup_script.txt
- 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"""
- Add-Content $config_file "option batch continue"
-
- # Sync to server
- $remote_directory = "/data2/backups/bethany/{0}/Profiles/" -f ( $env:COMPUTERNAME )
- $local_directory = "c:\Users"
- $mask = parse_sync_exclude
- $config = "synchronize remote -delete -filemask=""$mask"" ""$local_directory"" $remote_directory"
- Add-Content $config_file "$config"
-
- # If requested, sync the outlook data files as well
- if (!($skipoutlook)) {
- $local = "$HOME\AppData\Local\Microsoft\Outlook\*.ost"
- $remote = "/data2/backups/bethany/{0}/" -f $env:COMPUTERNAME
- $config = "put -resume ""$local"" $remote"
- Add-Content $config_file $config
- }
- # Complete the config file and execute
- Add-Content $config_file "exit"
- WinSCP.com /script=$config_file
- }
- }
- Export-ModuleMember backup_to_server
|