|
| 1 | +################################################################################ |
| 2 | +# BackupShareFileAccount.ps1 |
| 3 | +# |
| 4 | +# Download an entire ShareFile account to a local directory. |
| 5 | +# |
| 6 | +# |
| 7 | +# Local Directory Structure: |
| 8 | +# |
| 9 | +# Local Directory |
| 10 | +# │ log-<date>.txt |
| 11 | +# │ account-<date>.csv |
| 12 | +# │ employees-<date>.csv |
| 13 | +# │ sharedfolders-<date>.csv |
| 14 | +# │ |
| 15 | +# └───Users |
| 16 | +# │ │ |
| 17 | +# │ └───user@email.com |
| 18 | +# │ │ 'Home Folder Name' |
| 19 | +# │ └─ ... |
| 20 | +# │ |
| 21 | +# └───Shared Folders |
| 22 | +# │ folder |
| 23 | +# │ ... |
| 24 | +# |
| 25 | +# |
| 26 | +# Requirements: |
| 27 | +# PowerShell 5 |
| 28 | +# The latest PowerShell SDK for ShareFile: https://github.com/citrix/ShareFile-PowerShell |
| 29 | +# A Superuser for the ShareFile account: https://support.citrix.com/article/CTX208527 |
| 30 | +# |
| 31 | +# Usage: |
| 32 | +# .\BackupShareFileAccount.ps1; Backup-ShareFileAccount -Type Shared -Path <LOCAL BACKUP PATH> -Email <ADMIN EMAIL> -Subdomain <ACCOUNT SUBDOMAIN> |
| 33 | +# |
| 34 | +# Type: Full, Users |
| 35 | +# Path: The local path to backup to |
| 36 | +# Email: A superuser account email |
| 37 | +# Subdomain: The account subdomain |
| 38 | +# |
| 39 | + |
| 40 | +Import-Module ShareFile |
| 41 | +#Import-Module ShareFile-Core # For PowerShell 7+ |
| 42 | +$global:BackupPath = $null |
| 43 | +$global:SFClient = $null |
| 44 | + |
| 45 | +function Backup-ShareFileAccount { |
| 46 | + [CmdletBinding(DefaultParameterSetName = "RequiredSet")] |
| 47 | + param( |
| 48 | + [Parameter(Mandatory,ParameterSetName = "RequiredSet")] |
| 49 | + [ValidateSet("Shared","Users","Full")] |
| 50 | + [string]$Type, |
| 51 | + [Parameter(Mandatory,ParameterSetName = "RequiredSet")] |
| 52 | + [string]$Path, |
| 53 | + [Parameter(Mandatory,ParameterSetName = "RequiredSet")] |
| 54 | + [string]$Email, |
| 55 | + [Parameter(Mandatory,ParameterSetName = "RequiredSet")] |
| 56 | + [string]$Subdomain |
| 57 | + ) |
| 58 | + |
| 59 | + $global:BackupPath = $Path |
| 60 | + $SFClientPath = $Path + "\" + $Email + ".sfps" |
| 61 | + |
| 62 | + if ([System.IO.File]::Exists($SFClientPath)) { |
| 63 | + Write-Host $SFClientPath " was found." -BackgroundColor Green -ForegroundColor DarkBlue |
| 64 | + $global:SFClient = Get-SFClient -Name $SFClientPath |
| 65 | + # Check validity |
| 66 | + if($?){ |
| 67 | + $(Send-SfRequest $SFClient -Entity Accounts) 2>&1 | out-null |
| 68 | + } else { |
| 69 | + Write-Host "Saved client file is invalid. Please sign in." -BackgroundColor Yellow -ForegroundColor DarkBlue |
| 70 | + New-SfClient -Name $SFClientPath -Account $Subdomain |
| 71 | + $global:SFClient = Get-SFClient -Name $SFClientPath |
| 72 | + } |
| 73 | + } else { |
| 74 | + Write-Host $SFClientPath " was NOT found. Please sign in." -BackgroundColor Yellow -ForegroundColor DarkBlue |
| 75 | + New-SfClient -Name $SFClientPath -Account $Subdomain |
| 76 | + $global:SFClient = Get-SFClient -Name $SFClientPath |
| 77 | + } |
| 78 | + |
| 79 | + Log "A $Type backup was requested by $Email." |
| 80 | + $AccountInfo = Send-SfRequest -Client $SFClient -Method GET -Entity Accounts -select "CompanyName,subdomain,Id" | Select-Object -Property CompanyName,subdomain,Id |
| 81 | + $AccountInfo | Export-Csv -Path $BackupPath/account-$("{0:yyyyMMdd}" -f (Get-Date)).csv -NoTypeInformation |
| 82 | + |
| 83 | + switch ($Type) { |
| 84 | + "Shared" { Backup-SharedFolders -SFClient $SFClient } |
| 85 | + "Users" { Backup-Users -SFClient $SFClient } |
| 86 | + "Full" { Backup-Full -SFClient $SFClient } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function Backup-Full { |
| 91 | + param( |
| 92 | + [Parameter(Mandatory)] |
| 93 | + [ShareFile.Api.Powershell.PSShareFileClient]$SFClient |
| 94 | + ) |
| 95 | + |
| 96 | + Log "Initiated Backup-Full function." |
| 97 | + Backup-Users -SFClient $SFClient |
| 98 | + Backup-SharedFolders -SFClient $SFClient |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +function Backup-SharedFolders { |
| 103 | + param( |
| 104 | + [Parameter(Mandatory)] |
| 105 | + [ShareFile.Api.Powershell.PSShareFileClient]$SFClient |
| 106 | + ) |
| 107 | + |
| 108 | + Log "Initiated Backup-SharedFolders function." |
| 109 | + $sharedfolders = Send-SfRequest -Client $SFClient -Method GET -Entity Items -Id "allshared" -Expand "Children" | Select-object -ExpandProperty Children |
| 110 | + $sharedfolders | Select-Object -Property Name,Id | Export-Csv -Path $BackupPath/sharedfolders-$("{0:yyyyMMdd}" -f (Get-Date)).csv -NoTypeInformation |
| 111 | + |
| 112 | + # Local Folder Structure and Download |
| 113 | + $SharedPath = "$BackupPath/Shared Folders" |
| 114 | + if (!(Test-Path $SharedPath)) { New-Item -Path $SharedPath -Type Directory } |
| 115 | + foreach ($sharedfolder in $sharedfolders) { |
| 116 | + $Root = "$($sharedfolder.Id):/" + "$($sharedfolder.Name)" |
| 117 | + $LocalPath = "$SharedPath/" |
| 118 | + Backup-ChildItems -Root $Root -LocalPath $LocalPath -DriveId $($sharedfolder.Id) -SFClient $SFClient |
| 119 | + } |
| 120 | + |
| 121 | + Log "Shared Folders backup completed." |
| 122 | +} |
| 123 | + |
| 124 | +function Backup-Users { |
| 125 | + param( |
| 126 | + [Parameter(Mandatory)] |
| 127 | + [ShareFile.Api.Powershell.PSShareFileClient]$SFClient |
| 128 | + ) |
| 129 | + |
| 130 | + Log "Initiated Backup-Users function." |
| 131 | + $Employees = Send-SfRequest -Client $SFClient -Method GET -Entity Accounts/Employees -select "Id,Email" |
| 132 | + $Employees | Select-Object -Property Email,Id | Export-Csv -Path $BackupPath/employees-$("{0:yyyyMMdd}" -f (Get-Date)).csv -NoTypeInformation |
| 133 | + |
| 134 | + # Local Folder Structure & Download |
| 135 | + $UsersPath = "$BackupPath/Users" |
| 136 | + if (!(Test-Path $UsersPath)) { New-Item -Path $UsersPath -Type Directory } |
| 137 | + foreach ($Employee in $Employees) { |
| 138 | + if (!(Test-Path "$UsersPath/$($Employee.Email)")) { New-Item -Path "$UsersPath/$($Employee.Email)" -Type Directory } |
| 139 | + $Root = "$($Employee.Id):/" + (Send-SfRequest $SFClient -Entity "Users($($Employee.Id))/HomeFolder").Name |
| 140 | + $LocalPath = "$UsersPath/$($Employee.Email)/" |
| 141 | + Backup-ChildItems -Root $Root -LocalPath $LocalPath -DriveId $($Employee.Id) -SFClient $SFClient |
| 142 | + } |
| 143 | + |
| 144 | + Log "User backup completed." |
| 145 | +} |
| 146 | + |
| 147 | +function Backup-ChildItems { |
| 148 | + param( |
| 149 | + [Parameter(Mandatory)] |
| 150 | + [ShareFile.Api.Powershell.PSShareFileClient]$SFClient, |
| 151 | + [Parameter(Mandatory)] |
| 152 | + [string]$Root, |
| 153 | + [Parameter(Mandatory)] |
| 154 | + [string]$LocalPath, |
| 155 | + [Parameter(Mandatory)] |
| 156 | + [string]$DriveId |
| 157 | + ) |
| 158 | + New-PSDrive -Name $DriveId -PSProvider ShareFile -Client $SFClient -Root "/" |
| 159 | + Copy-SfItem -Path $Root -Destination $LocalPath |
| 160 | + Remove-PSDrive $DriveId |
| 161 | +} |
| 162 | + |
| 163 | +function Log { |
| 164 | + param( |
| 165 | + [Parameter(Mandatory=$true)][String]$msg |
| 166 | + ) |
| 167 | + $LogFileDate = "{0:yyyyMMdd}" -f (Get-Date) |
| 168 | + $LogTime = "[{0:MM/dd/yy} {0:HH:mm:ss}] " -f (Get-Date) |
| 169 | + Add-Content $BackupPath/log-$LogFileDate.txt ($LogTime + $msg) |
| 170 | +} |
0 commit comments