Poor.Backup.System.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#
# Created by: lucas.cueff[at]lucas-cueff.com
#
# v0.1 :
# - manage configuration settings file and environment
# - add or remove files to backup definition
# - log backup
# - compress backup to a zip file
#
#'(c) 2018-2019 lucas-cueff.com - Distributed under Artistic Licence 2.0 (https://opensource.org/licenses/artistic-license-2.0).'

<#
    .SYNOPSIS
    simple powershell module to backup file or folder using Powershell Core
 
    .DESCRIPTION
    simple powershell module to backup file or folder using Powershell Core
     
    .EXAMPLE
    C:\PS> import-module Poor.Backup.System.psm1
#>

Function Start-PoorBackup {
    <#
        .SYNOPSIS
        Start a 'poor' backup task
 
        .DESCRIPTION
        Start a 'poor' backup task
             
        .PARAMETER Facets
        -NoProgressBar switch
        Remove graphical progress bar for copy and compression
         
        .OUTPUTS
        System.IO.DirectoryInfo
        System.IO.FileInfo
 
        .EXAMPLE
        Start a PoorBackup
        C:\PS> Start-PoorBackup
 
        .EXAMPLE
        Start a PoorBackup without progress bar
        C:\PS> Start-PoorBackup -NoProgressBar
    #>

    [cmdletbinding()]
    Param (
        [switch]$NoProgressBar
    )
    process {
        $script:ItemID = $null
        $script:ItemPercent = $null
        if ($NoProgressBar) {
            $tmpProgressPreference = $global:progressPreference
            $global:progressPreference = 'SilentlyContinue'
        }
        if (!($global:PoorBackupSettings.LogDir -and $global:PoorBackupSettings.TargetBackupDir -and $global:PoorBackupSettings.ItemsToBackup)) {
            throw "PoorBackupSettings does not exist. Please use Set-PoorBackupSettings cmdlet to generate it properly or use Import-PoorBackupSettings to load XML settings into memory"
        }
        $ticks = (get-date).Ticks
        $logFileName = join-path $global:PoorBackupSettings.LogDir "PoorBackupSystem_$($ticks).log"
        $BackupFolder = join-path $global:PoorBackupSettings.TargetBackupDir $ticks
        if (!(test-path $BackupFolder)) {
            new-item $BackupFolder -Force -Type Directory | out-null
        }
        Write-PoorBackupLog -Message "==> PoorBackupSystem Starting <==" -Path $logFileName
        Write-PoorBackupLog -Message "Enumarating current back session settings :" -Path $logFileName
        Write-PoorBackupLog -Message "- Target Host Backup directory : $($BackupFolder)" -Path $logFileName
        Write-PoorBackupLog -Message "- Content to backup : $($global:PoorBackupSettings.ItemsToBackup -join ",")" -Path $logFileName
        Write-PoorBackupLog -Message "Starting Real Poor Backup : " -Path $logFileName
        foreach ($item in $global:PoorBackupSettings.ItemsToBackup) {
            $script:ItemID = $script:ItemID + 1
            $script:ItemPercent = $script:ItemID/$global:PoorBackupSettings.ItemsToBackup.count*100 
            if ($script:ItemPercent -lt 100) {
                Write-Progress -Activity "Backuping Poorly Items" -Status "Backing up in Progress:" -CurrentOperation $item -PercentComplete ($script:ItemPercent) -Id 0
            } else {
                Write-Progress -Activity "Backuping Poorly Items" -Status "End of Backup" -CurrentOperation $item -PercentComplete $script:ItemPercent -Id 0 -Completed
            }
            Write-PoorBackupLog -Message "- backup $($item) to $($BackupFolder)..." -Path $logFileName
            if ($item.contains(":")) {
                $targetBackupPath = join-path $BackupFolder ($item.split(":"))[1]
            } else {
                $targetBackupPath = join-path $BackupFolder $item
            }
            Write-PoorBackupLog -Message "full backup target destination folder $($targetBackupPath)" -Path $logFileName
            if (!(test-path $item)) {
                Write-PoorBackupLog -Message "$($item) not found - item will be skipped" -Path $logFileName -Level Warn
            } else {  
                if (!(test-path (split-path $targetBackupPath))) {
                    New-Item (split-path $targetBackupPath) -Force -Type Directory | out-null
                }
                try {
                    Copy-Item $item -Destination (split-path $targetBackupPath) -Recurse -Force | out-null
                } catch {
                    Write-PoorBackupLog -Message "not able to backup $($item) - Error Type: $($_.Exception.GetType().FullName) - Error Message: $($_.Exception.Message)" -Path $logFileName -Level Error
                }
            }
        }
        if ($global:PoorBackupSettings.CompressBackup) {
            try {
                Compress-Archive -Path $BackupFolder -DestinationPath (join-path (split-path $BackupFolder) "$($ticks).zip") -CompressionLevel Optimal -Force | out-null
            } catch {
                Write-PoorBackupLog -Message "not able to archive backup file $((join-path (split-path $BackupFolder) "$($ticks).zip")) - Error Type: $($_.Exception.GetType().FullName) - Error Message: $($_.Exception.Message)" -Path $logFileName -Level Error
            }
            try {
                remove-item $BackupFolder -Force -Recurse | out-null
            } catch {
                Write-PoorBackupLog -Message "not able to remove backup folder $($BackupFolder) after compression - Error Type: $($_.Exception.GetType().FullName) - Error Message: $($_.Exception.Message)" -Path $logFileName -Level Error
            }
        }
        Write-PoorBackupLog -Message "==> PoorBackupSystem Closing <==" -Path $logFileName
        Write-PoorBackupLog -Message "There is no PoorRestoreSystem, DoItYourSelfRestoreSystem ;-)" -Path $logFileName
        if ($NoProgressBar) {
            $global:progressPreference = $tmpProgressPreference
        }
        get-childitem $Global:PoorBackupSettings.TargetBackupDir
    }
}
Function Import-PoorBackupSettings {
    <#
        .SYNOPSIS
        Import PoorBackup Settings file into current PowerShell environment
 
        .DESCRIPTION
        Import PoorBackup Settings file into current PowerShell environment as global variable PoorBackupSettings
         
        .PARAMETER SettingsFile
        -SettingsFile string {full path to target xml file name, directory must exist}
        default value : $home\".PoorBackup\settings.xml"
        full path to custom target xml settings file
         
        .OUTPUTS
        Deserialized.System.Management.Automation.PSCustomObject
         
        .EXAMPLE
        Import PoorBackup settings using default file and path
        C:\PS> Import-PoorBackupSettings
         
        .EXAMPLE
        Import PoorBackup settings using a specific config file
        C:\PS> Import-PoorBackupSettings -SettingsFile C:\tmp\mybackupstuff.xml
    #>

    [cmdletbinding()]
    Param (
      [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
      [ValidateScript({test-path "$($_)"})]
        [string]$SettingsFile = (join-path $home ".PoorBackup\settings.xml")
    )
    process {
        if (!(test-path $SettingsFile)) {
            throw "$($SettingsFile) does not exist - Please use Set-PoorBackupSettings first to create your configuration file"
        }
        try {
            $global:PoorBackupSettings = Import-Clixml $SettingsFile
        } catch {
            write-error -message "Error Type: $($_.Exception.GetType().FullName)"
            write-error -message "Error Message: $($_.Exception.Message)"
        }
        if (!($global:PoorBackupSettings.LogDir -and $global:PoorBackupSettings.TargetBackupDir -and $global:PoorBackupSettings.ItemsToBackup)) {
            throw "XML file not valid. Please use Set-PoorBackupSettings cmdlet to generate it properly"
        } else {
            $global:PoorBackupSettings
        }
    }
}
Function Set-PoorBackupSettings {
    <#
        .SYNOPSIS
        Set PoorBackup settings
 
        .DESCRIPTION
        Set PoorBackup settings (as global variable and external file) : Log directory, target backup directory, file and folder to backup, backup compression
         
        .PARAMETER LogDir
        -LogDir string {full path to target log directory, directory must exist}
        mandatory
        full path to target log directory
 
        .PARAMETER TargetBackupDir
        -TargetBackupDir string {full path to target backup directory, directory must exist}
        mandatory
        full path to target backup directory
 
        .PARAMETER SettingsFile
        -SettingsFile string {full path to target settings xml file, directory must exist}
        default value : $home\".PoorBackup\settings.xml"
        full path to target settings xml file
 
        .PARAMETER CompressBackup
        -CompressBackup switch
        Enable Backup as a zip file archive
 
        .PARAMETER ItemsToBackup
        -ItemsToBackup array of string
        mandatory
        List of items (file or folder) to backup
 
        .OUTPUTS
        System.IO.DirectoryInfo
        System.IO.FileInfo
         
        .EXAMPLE
        Backup "C:\Users\adm\Documents\GitHub","C:\Users\adm\Documents\Gitlab\","C:\Users\adm\Documents\file.ext" to C:\Backup with compression and log operations to c:\logs
        C:\PS> Set-PoorBackupSettings -LogDir c:\logs -TargetBackupDir C:\Backup -ItemsToBackup @("C:\Users\adm\Documents\GitHub","C:\Users\adm\Documents\Gitlab\","C:\Users\adm\Documents\file.ext") -CompressBackup
    #>

    [cmdletbinding()]
    Param (
      [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$false)]
      [ValidateScript({test-path "$($_)"})]
        [string]$LogDir,
      [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$false)]
      [ValidateScript({test-path "$($_)"})]
        [string]$TargetBackupDir,
      [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$false)]
      [ValidateScript({test-path "$($_)"})]
        [System.Collections.ArrayList]$ItemsToBackup,
      [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
        [switch]$CompressBackup,
      [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
        [ValidateScript({test-path (split-path "$($_)")})]
        [string]$SettingsFile = (join-path $home ".PoorBackup\settings.xml")
    )
    process {
        if ($global:PoorBackupSettings) {
            $global:PoorBackupSettings = $null
        }
        if (!(test-path (join-path $home ".PoorBackup"))) {
            New-Item -Path (join-path $home ".PoorBackup") -Force -Type Directory | out-null
        }
        if (!(test-path (join-path $LogDir "PoorBackup"))) {
            New-Item (join-path $LogDir "PoorBackup") -Force -Type Directory | out-null
        }
        $LogDir = (join-path $LogDir "PoorBackup")
        if (!(test-path (join-path $TargetBackupDir "PoorBackup"))) {
            New-Item (join-path $TargetBackupDir "PoorBackup") -Force -Type Directory | out-null
        }
        $TargetBackupDir = (join-path $TargetBackupDir "PoorBackup")
        $global:PoorBackupSettings = New-Object psobject -Property @{
            LogDir = $LogDir
            TargetBackupDir = $TargetBackupDir
            ItemsToBackup = $ItemsToBackup
            CompressBackup = $CompressBackup
            SettingsFile = $SettingsFile
        }
        Export-Clixml -path "$($SettingsFile)" -InputObject $global:PoorBackupSettings -Force
        Get-Item "$($SettingsFile)"
    }
}
Function Add-ItemToPoorBackup {
    <#
        .SYNOPSIS
        Add new item to backup list
 
        .DESCRIPTION
        Add new item (file or folder) to backup list
         
        .PARAMETER BackupItem
        -BackupItem String or System.IO
        mandatory
        new item to add to backup list
 
        .OUTPUTS
        System.String
         
        .EXAMPLE
        Add folder c:\important to backup list
        C:\PS> Add-ItemToPoorBackup -BackupItem "c:\important"
 
        .EXAMPLE
        Add folder c:\important to backup list (pipe mode)
        C:\PS> get-item "c:\important" | Add-ItemToPoorBackup
    #>

    [cmdletbinding()]
    Param (
      [Parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
      [ValidateScript({($_ -is [System.IO.DirectoryInfo]) -or ($_ -is [System.IO.FileInfo]) -or ($_ -is [System.String])})]
        $BackupItem
    )
    process {
        if (!($global:PoorBackupSettings.LogDir -and $global:PoorBackupSettings.TargetBackupDir -and $global:PoorBackupSettings.ItemsToBackup)) {
            throw "PoorBackupSettings does not exist. Please use Set-PoorBackupSettings cmdlet to generate it properly or use Import-PoorBackupSettings to load XML settings into memory"
        }
        $BackupItemType = $BackupItem | Get-Member | Select-Object -ExpandProperty TypeName -Unique
        switch ($BackupItemType) {
            System.IO.FileInfo {
                if ($BackupItem.fullname) {
                    if ($global:PoorBackupSettings.ItemsToBackup -notcontains $BackupItem.fullname) {
                        $global:PoorBackupSettings.ItemsToBackup.add($BackupItem.fullname)
                    }
                }
            }
            System.IO.DirectoryInfo {
                if ($BackupItem.fullname) {
                    if ($global:PoorBackupSettings.ItemsToBackup -notcontains $BackupItem.fullname) {
                        $global:PoorBackupSettings.ItemsToBackup.add($BackupItem.fullname)
                    }
                }
            }
            System.String {
                if ($global:PoorBackupSettings.ItemsToBackup -notcontains $BackupItem)  {
                    $global:PoorBackupSettings.ItemsToBackup.add($BackupItem)
                }
            }
        }
        $global:PoorBackupSettings.ItemsToBackup
    }
}
Function Remove-ItemFromPoorBackup {
    <#
        .SYNOPSIS
        Remove new item from backup list
 
        .DESCRIPTION
        Remove new item (file or folder) from backup list
         
        .PARAMETER BackupItem
        -BackupItem String or System.IO
        mandatory
        current item to remove from backup list
 
        .OUTPUTS
        System.String
         
        .EXAMPLE
        Remove folder c:\important from backup list
        C:\PS> Remove-ItemFromPoorBackup -BackupItem "c:\important"
 
        .EXAMPLE
        Remove folder c:\important from backup list (pipe mode)
        C:\PS> get-item "c:\important" | Remove-ItemFromPoorBackup
    #>

    [cmdletbinding()]
    Param (
      [Parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
      [ValidateScript({($_ -is [System.IO.DirectoryInfo]) -or ($_ -is [System.IO.FileInfo]) -or ($_ -is [System.String])})]
        $BackupItem
    )
    Process {
        if (!($global:PoorBackupSettings.LogDir -and $global:PoorBackupSettings.TargetBackupDir -and $global:PoorBackupSettings.ItemsToBackup)) {
            throw "PoorBackupSettings does not exist. Please use Set-PoorBackupSettings cmdlet to generate it properly or use Import-PoorBackupSettings to load XML settings into memory"
        }
        $BackupItemType = $BackupItem | Get-Member | Select-Object -ExpandProperty TypeName -Unique
        switch ($BackupItemType) {
            System.IO.FileInfo {
                if ($BackupItem.fullname) {
                    if ($global:PoorBackupSettings.ItemsToBackup -contains $BackupItem.fullname) {
                        $global:PoorBackupSettings.ItemsToBackup.remove($BackupItem.fullname)
                    }
                }
            }
            System.IO.DirectoryInfo {
                if ($BackupItem.fullname) {
                    if ($global:PoorBackupSettings.ItemsToBackup -contains $BackupItem.fullname) {
                        $global:PoorBackupSettings.ItemsToBackup.remove($BackupItem.fullname)
                    }
                }
            }
            System.String {
                if ($global:PoorBackupSettings.ItemsToBackup -contains $BackupItem)  {
                    $global:PoorBackupSettings.ItemsToBackup.remove($BackupItem)
                }
            }
        }
        $global:PoorBackupSettings.ItemsToBackup
    }
}
function Write-PoorBackupLog {
    <#
        .SYNOPSIS
        Write to log file
 
        .DESCRIPTION
        Write to log file
         
        .PARAMETER Message
        -Message string
        mandatory
        string to write to log file
 
        .PARAMETER Path
        -Path string {full path to target log file}
        mandatory
        full path to log file to write in
 
        .PARAMETER Level
        -Level string {"Error","Warn","Info"}
        default value : Info
        Type of information to log
         
        .OUTPUTS
        none
         
        .EXAMPLE
        Write "Hello world" as information to "c:\logs\test.log"
        C:\PS> Write-PoorBackupLog -Message "Hello world" -Path "c:\logs\test.log"
         
        .EXAMPLE
        Write "Bad world" as error to "c:\logs\test.log"
        C:\PS> Write-PoorBackupLog -Message "Bad world" -Path "c:\logs\test.log" -Level Error
    #>
 
    [CmdletBinding()] 
    Param ( 
        [Parameter(Mandatory=$true, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$true)] 
        [ValidateNotNullOrEmpty()]
            [string]$Message, 
        [Parameter(Mandatory=$true)] 
            [string]$Path, 
        [Parameter(Mandatory=$false)] 
        [ValidateSet("Error","Warn","Info")] 
            [string]$Level="Info"
    ) 
    Process { 
        if (!(Test-Path $Path)) {
            Write-Verbose "Creating $($Path)." 
            New-Item $Path -Force -Type File | out-null
        } 
        $FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss" 
        try {
            "$($FormattedDate) $($Level.ToUpper()): $($Message)" | Out-File -FilePath $Path -Append
            Write-Debug "$($FormattedDate) $($Level.ToUpper()): $($Message)"
        } catch {
            write-error -message "Error Type: $($_.Exception.GetType().FullName)"
            write-error -message "Error Message: $($_.Exception.Message)"
        }
    } 
}

Export-ModuleMember Start-PoorBackup, Set-PoorBackupSettings, Add-ItemToPoorBackup, Remove-ItemFromPoorBackup, Import-PoorBackupSettings, Write-PoorBackupLog