-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSBackup.psm1
119 lines (94 loc) · 2.98 KB
/
PSBackup.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
<#
.SYNOPSIS
Backs up files and directories to a specified location.
.DESCRIPTION
Use function Backup-FilesFolders to backup source directories/files to a destination.
.PARAMETER Sources
Specify the source directory or file to be backed up (Can be multiple).
.PARAMETER SourceFile
Specify a text file containing a list of directories to be backed up.
.PARAMETER Destination
Specify the destination folder to save archived files/folders.
.PARAMETER Compress
Archived files/folders will be saved in a Zip compressed archive.
.PARAMETER Debuger
Debug mode.
.EXAMPLE
Backup single directory and save as compress archive.
PS C:\> Backup-FilesFolders -Sources "C:\TestDirectory" -Destination "D:\BackupDirectory" -Compress
.EXAMPLE
Backup multiple directories listed in text file and compress archive.
PS C:\> Backup-FilesFolders -SourceFile "C:\sourcelist.txt" -Destination "D:\BackupDirectory" -Compress
.NOTES
Author: Tony Cavella
Blog : https://revokehq.github.io
.LINK
https://github.com/revokehq/psbackup
.LINK
Backup-Utility
#>
Function Backup-FilesFolders
{
[CmdletBinding()]
Param
(
#Mode options
[parameter()]
[switch]$Debuger,
[parameter()]
[String[]]$Sources,
[parameter(Mandatory)]
[String]$Destination,
[parameter()]
[Alias('Zip')]
[Switch]$Compress,
[parameter()]
[String]$SourceFile
)
Begin
{
If($PSBoundParameters['Debuger'])
{
$DebugPreference = "Continue"
} #End If $PSBoundParameters['Debuger']
Write-Debug "Checking for administrative privileges."
$User = [Security.Principal.WindowsIdentity]::GetCurrent()
$Role = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
If(!$Role)
{
Write-Warning "To perform some operations you must run an elevated Windows PowerShell console."
} #End If !$Role
Write-Debug "Setting script variables."
[string]$Filename = "backup_$(Get-Date -f yyyy-MM-dd)"
Write-Debug "Checking for valid desination."
$DestinationCheck = $(Test-Path -Path $Destination)
If(!$DestinationCheck)
{
Write-Warning "Destination does not exist, creating directory."
New-Item -ItemType "directory" -Path "$Destination"
} #End If !$DesinationCheck
If($PSBoundParameters.ContainsKey['SourceFile'])
{
[string[]]$Sources = $(Get-Content -Path "$SourceFile")
}
}
Process
{
Write-Verbose "Creating backup directory."
New-Item -ItemType "directory" -Path "$Destination\$Filename"
Write-Verbose "Copying files to backup directory."
ForEach ($Source in $Sources)
{
Copy-Item -Path "$Source" -Destination "$Destination\$Filename" -Recurse
}
If ($Compress)
{
$ZipFilename = "$Destination\$Filename.zip"
Write-Verbose "Adding files to Zip archive."
Compress-Archive -Path "$Destination\$Filename" -DestinationPath "$ZipFilename"
Write-Verbose "Cleaning up extra files."
Remove-Item -Path "$Destination\$Filename" -Recurse
}
}
End{}
}