-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup-Files.ps1
More file actions
115 lines (88 loc) · 2.8 KB
/
Backup-Files.ps1
File metadata and controls
115 lines (88 loc) · 2.8 KB
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
<#
.SYNOPSIS
Backs up specific files in the current directory.
.DESCRIPTION
Creates a zip archive containing copies of specific files
in the current database.
The name of the zip archive is specified by the Name parameter,
the back up file name will be tagged with the value
specified by the Tag parameter and suffied with the current date.
.PARAMETER Name
System.String. Value that the back up archive's file name should start with.
.PARAMETER Tag
System.String.Value that the back up archive's file name will be tagged with.
.PARAMETER Files
System.String[]. The names of the files to include in the back up archive.
.PARAMETER BackupPath
System.String. The directory to save the back up to.
.INPUTS
None
.OUTPUTS
None
.EXAMPLE
PS> .\Backup-Files.ps1
.EXAMPLE
PS> .\Backup-Files.ps1 -Name MyFilesBackup -Tag V1 -Files File1.Docx,File2.txt -BackupPath Backups
.NOTES
Author: Jonathan Panning <jpann [at] impostr-labs.com>
Created on: 04-01-2024
Last updated: 04-01-2024
.LINK
https://github.com/jpann/PowerShell
#>
#requires -version 4
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false,
HelpMessage = 'Enter name of the back up.')]
[ValidateNotNullOrEmpty()]
[string]
$Name,
[Parameter(Mandatory=$true,
HelpMessage = 'Enter tag for the back up.')]
[ValidateNotNullOrEmpty()]
[string]
$Tag,
[Parameter(Mandatory=$false,
HelpMessage = 'Enter file names to back up.')]
[string[]]
$Files,
# You can manually set this to explicit file names if you
# wish to be able to just run this in Explorer.
# $Files = @('MyDocument_V1.docx'),
[Parameter(Mandatory=$false,
HelpMessage = 'Enter the path to the back up directory.')]
[ValidateNotNullOrEmpty()]
[string]
$BackupPath = 'Backups'
)
begin {
}
process {
cd $PSScriptRoot
try {
if ($null -eq $Name -or $Name -eq "") {
$Name = Split-Path -Path $pwd -Leaf
$Name = $Name.Replace(" ", "_")
}
if ($null -ne $Tag -and $Tag -ne "") {
$Tag = $Tag.Replace(" ", "_")
}
$backupDir = Join-Path $PSScriptRoot $BackupPath
$backupFileName = "{0}-{1}_{2}.zip" -f $Name, $Tag, $((Get-Date).ToString('MM-dd-yyyy_hh.mmtt'))
$backupFilePath = Join-Path $backupDir $backupFileName
if (-not(Test-Path -Path $backupDir -PathType Container))
{
New-Item -ErrorAction Ignore -ItemType Directory -Path $backupDir
}
$compress = @{
Path = $Files
CompressionLevel = "Optimal"
DestinationPath = $backupFilePath
}
Compress-Archive @compress
} catch {
write-error "Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
}