-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStart-Backup.ps1
82 lines (67 loc) · 2.97 KB
/
Start-Backup.ps1
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
function Start-Backup
{
param (
[Parameter(Mandatory=$true, ParameterSetName='Backup')]
[ValidateScript({Test-Path $_ -pathtype container})]$SourceDIR,
[Parameter(ParameterSetName='Backup', Mandatory=$true)]$BackupDir="$env:TEMP\CopyBackup",
[Parameter(ParameterSetName='Backup', Mandatory=$false)][regex]$FileMask=".*",
[Parameter(ParameterSetName='Backup', Mandatory=$false)][switch]$RemoveOld,
[Parameter(ParameterSetName='Backup', Mandatory=$false)][int]$DaystoRetain='365')
try {$ScriptDir = Split-Path -parent $MyInvocation.MyScriptName
Import-Module $ScriptDir\Powerlogger\Powerlogger.psm1 -MinimumVersion 1.8} catch{Import-Module Powerlogger -MinimumVersion 1.8}
if ((Get-Module -Name PowerLogger).Version.Major -ne 1){throw "Power Logger MAJOR version has incrimented since it was added to this script Errors may OCCUR"}
Start-Logging -ScriptVersion "1.4" -LoggingLevel DEBUG -OutputTerminal $true -TerminalLevel DEBUG
#region Param validation
$timeformat = 'yyyyMMdd-hhmmss'
$CopyDate = Get-Date -Format $timeformat
if (!$(Test-Path -Path $BackupDir))
{
mkdir $BackupDir
Write-PLDebug -Message "Created Directory $DestinationDIR"
}
$SourceTree = Get-ChildItem $SourceDIR -Recurse
$RootDIR = $SourceDIR
$RootLength = $RootDIR.Length
#endregion
if($RemoveOld.IsPresent)
{
Write-PLInfo -Message "Checking old backups"
$Backups = Get-ChildItem -Path $BackupDir
Write-PLVerbose -Message "Found $($Backups.Count) Backups in Directory"
foreach($backup in $Backups)
{
Write-PLDebug -Message "Verifying that $($backup.Name) is within Date"
try{if([datetime]::ParseExact($Backup.name,$timeformat,$null) -lt (Get-Date).AddDays("-$DaystoRetain"))
{
Write-PLInfo -Message "Removing $($backup.Name) as it is older than $DaystoRetain Days" -emailline
Remove-Item $Backup.fullname -Force -Recurse
}
}catch
{
Write-PLVerbose -Message "Not Checking $($backup.Name) Due to Not being in proper format"
}
} # End of Foreach Loop
} # End of remove old
$Sourcefiles = @($SourceTree | Where-Object {$_.mode -ne 'd----'})
foreach($file in $SourceFiles)
{
#Backup file name file name + Date/time
$BackupFilePath = "$BackupDir\$CopyDate"
$FileName = $file.fullName
if($FileName -notmatch $FileMask)
{
Write-PLDebug -Message "$FileName doesnt match $FileMask"
continue
}
Write-PLVerbose -Message "Backing up $($file.name) to $BackupFilePath"
if (!$(Test-path -path $BackupFIlePath))
{mkdir $BackupFilePath | Out-Null}
Copy-Item $FileName $BackupFilePath -Force -ErrorVariable MyError
if ($MyError -ne $null)
{
Write-PLInfo -Message "ERROR: Unable to Backup file $FileName due to $($Myerror.exception)" -emailline
Write-Error -Message "ERROR: Unable to Backup file $FileName due to $($Myerror.exception)"
continue
}
}
}