Repository navigation
Expand file tree
/
Copy pathfileBackup.ps1
More file actions
102 lines (84 loc) · 3.88 KB
/
Copy pathfileBackup.ps1
File metadata and controls
102 lines (84 loc) · 3.88 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
param(
[string]$dirsToScan = "C:\Users\vm305\Desktop\Test", #default value when param not specified
[string]$backupDir = "C:\Users\vm305\Desktop\Test2" #default value when param not specified
)
$Logfile = "C:\Users\vm305\Desktop\Logs\$($MyInvocation.MyCommand.Name).log"
#$dirsToScan = "C:\Users\vmaster\Desktop\Test"
#$backupDir = "C:\Users\vmaster\Desktop\Test2"
$filesToBackup = @()
$filesInBackup = @{}
#Hash_Array.Add("Key", "Value")
#Hash_Array.Key = "value" #edits item and if doesnt exist, then adds it
#Hash_Array.Remove("Key")
#Hash_Array."key" #finds the key/value pair
#Hash_Array.ContainsKey("key")
#Hash_Array.ContainsValue("Value") #used like: "*value*"
function LogWrite($logString)
{
Add-content $Logfile -value $logString
}
#hash function to scan the backup and add them to the hashtable
function getFileHash($fileName){
$hasher = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
$toHash = [System.Text.Encoding]::UTF8.GetBytes($fileName)
$hashByteArray = $hasher.ComputeHash($toHash)
foreach($byte in $hashByteArray){
$result += "{0:X2}" -f $byte
}
return $result;
}
LogWrite("-----------------------------------Start-----------------------------------")
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Script started")
#scan the backup directory and add the filenames to the hashtables
#Key-Value will be getFileHash and file.name
foreach($file in (Get-ChildItem $backupDir -Recurse)){
$filesInBackup.Add((getFileHash -fileName $file.Name),$file)
}
#check the dir to see if there are any files not in subdirectories
if((Get-ChildItem $dirsToScan -File).Count -gt 0){
foreach($file in (Get-ChildItem $dirsToScan -File)){
$filesToBackup += (Join-Path -Path $dirsToScan -ChildPath $file)
}
}
#scan each dir and add the file to the filesToMove array with the fullNmae
foreach($dir in ((Get-ChildItem $dirsToScan -Directory).FullName)) {
foreach($file in (Get-ChildItem $dir -File -Recurse)){
$filesToBackup += (Join-Path -Path $dir -ChildPath $file)
}
}
#if file in filesInBackup and (size is diff or lastwritetime is diff) then backup it up
#else if, file doesnt exist in backup, backup it up
#else skip
foreach($file in $filesToBackup){
#splitting it so we can compare just the file names and not the directory since they will be different
if ($filesInBackup.ContainsKey((getFileHash $file.Split("\")[-1])) -and `
((Get-Item $file).Length -ne ((Get-Item($filesInBackup.Item((getFileHash $file.Split("\")[-1]))).FullName).Length) -or `
(Get-Item $file).LastWriteTime -ne ((Get-Item($filesInBackup.Item((getFileHash $file.Split("\")[-1]))).FullName).LastWriteTime))){
Copy-Item $file -Destination $backupDir
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Copying item to backup since it was updated: " + $file.Split("\")[-1])
}
elseif($file.Split("\")[-1] -notin ($filesInBackup.Item((getFileHash $file.Split("\")[-1])))){
Copy-Item $file -Destination $backupDir
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Copying item to backup; not latest copy: " + $file.Split("\")[-1])
}
else{
continue
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Skipping item since already backed up and no changes were made: " + $file.Split("\")[-1])
}
}
#$filesToBackup.Clear()
#$filesInBackup.Clear()
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Script ended")
LogWrite("------------------------------------End------------------------------------")
<# old hash function that is very crappy
function getFileHash($fileName){
$index = -1
$nameToCharArr = [int[]][char[]]$fileName
$sum = 0
for ($i = 0; $i -lt $nameToCharArr.length-1; $i++){
$sum += $nameToCharArr[$i]
}
$index = $sum % 350 #change the mod
return $index
}
#>