-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupWithRobo.ps1
More file actions
145 lines (108 loc) · 4.33 KB
/
BackupWithRobo.ps1
File metadata and controls
145 lines (108 loc) · 4.33 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
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
Param(
# first parameter is a switch if 0: second parameter is the destination directory
# if first para is 1 you can select the destination dir in a windows.forms form
# second paramter makes subdirectory in e.g. D:\Robo Backup\Second Parameter\<Current DateTime>\
# third para is source directory
#
# e.g. .\BackupWithRobo.ps1 0 Projekt_Win32 "D:\repo\win32"
# will backup to directory $mainbackupDir\ProjektWin32\20220113_150130\ e.g. D:\Robo Backup\ProjektWin32\20220113_150130\
# .\BackupWithRobo.ps1 1 PLACEHOLDER "D:\repo\win32" will open GUI to select dir
[Parameter(Mandatory=$True)]
[bool]$selectFolder=$False,
[Parameter(Mandatory=$True)]
[string]$PARAtargetDir,
[Parameter(Mandatory=$True)]
[string]$sourceDir
)
Write-Host $sourceDir
$registryplaceholder = "PLACEHOLDERFORDOUBLEESCAPE" # to not escape the backslash in e.g. \"C:\\"
$sourceDir = $sourceDir.Substring(0, $sourceDir.Length - $registryplaceholder.Length)
Write-Host $sourceDir
$mainbackupDir = "D:\Robo Backup\"
function GetDestFolderPath {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Global:selectedFolderSwitch = 0
$window = New-Object System.Windows.Forms.Form
$window.Width = 800
$window.Height = 400
$windowFolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(20,20)
$Label.Font = New-Object System.Drawing.Font("",16,[System.Drawing.FontStyle]::Regular)
$Label.Text = "selected folder path"
$Label.AutoSize = $True
$window.Controls.Add($Label)
$windowTextBox = New-Object System.Windows.Forms.TextBox
$windowButton = New-Object System.Windows.Forms.Button
$windowButton.Location = New-Object System.Drawing.Size(18,290)
$windowButton.Size = New-Object System.Drawing.Size(90,60)
$windowButton.Text = "OK"
$windowButton.Add_Click(
{
$window.Dispose()
Write-Host "okay button path: " $windowFolderBrowser.SelectedPath
})
$window.Controls.Add($windowButton)
$selectFolderButton = New-Object System.Windows.Forms.Button
$selectFolderButton.Location = New-Object System.Drawing.Size(18,90)
$selectFolderButton.Size = New-Object System.Drawing.Size(90,60)
$selectFolderButton.Text = "Select Folder"
$selectFolderButton.Add_Click({
Write-Host "Select Folder Button Clicked"
$result = $windowFolderBrowser.ShowDialog()
if($result -eq "OK")
{
Write-Host "selecting folder is okay: " $windowFolderBrowser.SelectedPath
#Write-Host $windowFolderBrowser.SelectedPath
$Label.Text = $windowFolderBrowser.SelectedPath
$Global:selectedFolderSwitch = 1
}
})
$window.Controls.Add($selectFolderButton)
[void]$window.ShowDialog()
# RETURN FROM HERE
if($Global:selectedFolderSwitch -eq 1)
{
Write-Host "returning (nonempty): " $windowFolderBrowser.SelectedPath
return $windowFolderBrowser.SelectedPath
}
else
{
Write-Host "returning empty string"
return ""
}
}
Write-Host "Backup of Directory: $sourceDir"
$datestring = get-date -UFormat “%Y%m%d_%H%M%S”
Write-Host "Backup to: $destDir"
$subdir = $sourceDir.Split('\')[-1] # create directory with source dir name
if ($subdir -eq ":") { $subdir="" } # for root drives e.g. "D:\"
Write-Host "create subdir in destination " $subdir
if($selectFolder -eq $True)
{
$a = GetDestFolderPath
Write-Host "selected Path is: " $a
if($a -eq ""){
Write-Host "no path selected: exiting here"
return
}
$destDir = "$a\$datestring\$subdir"
}
else
{
$destDir = "$mainbackupDir$PARAtargetDir\$datestring\$subdir"
}
# cleanup of sourceDir and destDir for D:\ cases where the backslash escapes the following arguments
if($sourceDir[-1] -eq '\')
{
$sourcedir = $sourceDir.Substring(0, $sourceDir.Length - 1)
}
if($destDir[-1] -eq '\')
{
$destDir = $destDir.Substring(0, $destDir.Length - 1)
}
Write-Host "BackUp Source: " $sourceDir
Write-Host "BackUp Destination: " $destDir
Write-Host "subdir: " $subdir
Start-Process -NoNewWindow -Wait -FilePath "C:\WINDOWS\system32\Robocopy.exe" -ArgumentList "`"$sourceDir`"","`"$destDir`"","/LOG:c:\PowerShellScripts\robolog.txt","/MIR /COPY:DAT /MT:32 /R:2 /W:1 /NFL /NDL /V /TEE"