-
Notifications
You must be signed in to change notification settings - Fork 0
/
CleanBackups.ps1
66 lines (46 loc) · 2.02 KB
/
CleanBackups.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
#set the maximum age of files you wish to retain
$threshold = 120
#converts the threshold number to a negative value
$adjusted_threshold = -$threshold
$date = Get-Date -Format MM-dd-yyyy
$filelist = @()
$targetfolder = "<Path to wherever you keep your backup files>"
$ignore = ("List of","Directories","you","want to","ignore")
function Get-TimeStamp {
return Get-Date -Format HH:mm:ss
}
$start = Get-TimeStamp
$time = Get-TimeStamp
Write-host "$time - $date - Started Run. Working in $targetfolder"
#get a list of all subdirectories
$rootdir = Get-ChildItem -Path $targetfolder -Directory
#for each directory in the Rootdir, we need to parse for deletable files. In this case, we only care about TSBAK files.
FOREACH ($directory in $rootdir) {
#if the directory name is NOT in the ignore list, then get a list of all TSBAK files.
IF (-not ( $ignore.Contains($directory.name))) {
$folder=$directory.name
$workingdir = "$targetfolder\$folder"
Write-Host "Working in $workingdir"
#find all of the TSBAK files in a single directory.
$files = Get-ChildItem -Path $workingdir -File *.tsbak | Select-Object directoryname,name,length,CreationTime,LastAccessTime
#This is clunky, but we need to get a complete list of files from which we can select deletable files.
$filelist += $files
} ELSE {
Write-Host "Ignoring $directory"
}
}
$candidates = $filelist | Where-Object{ $_.LastAccessTime -le (Get-Date).AddDays($adjusted_threshold) }
$candidates | Export-Csv -Path "<Explicit path to wherever you want to keep your logs for this process>"
FOREACH ($file in $Candidates) {
$path = $file.DirectoryName
$filename = $file.Name
$explicit = "$path\$filename"
$time = Get-TimeStamp
Write-Host "$time - Deleting $filename"
Remove-Item -Path $explicit
}
$work = $candidates.Count
$finish = Get-TimeStamp
$runtime = New-TimeSpan -Start $start -End $finish
$time = Get-Timestamp
Write-Output "$time - $date - Run took $runtime. Deleted $work files."