Last active
October 29, 2015 10:23
-
-
Save mattfinch/9a45f97a516ee0709599 to your computer and use it in GitHub Desktop.
Script to keep an eye on FTP dropbox for backups + delete old ones.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AddLog ($msg, $psh) { | |
Add-Content C:\log.txt "`n[$([DateTime]::Now)]$msg" | |
if ($psh){ | |
Pushover $msg | |
} | |
} | |
function Pushover ($msg) { | |
$parameters = New-Object System.Collections.Specialized.NameValueCollection | |
$parameters.Add("token", "PushoverApiToken") | |
$parameters.Add("user", "PushoverUserKey") | |
$parameters.Add("message", "$msg") | |
$client = New-Object System.Net.WebClient | |
$client.UploadValues("https://api.pushover.net/1/messages.json", $parameters)| out-null | |
} | |
AddLog "--------------------------------------------------------------------------------" -psh $false | |
AddLog "maintainer-start" -psh $false | |
$dir = "PathToCheck" | |
$ext = "*.gz" | |
#check date of youngest backup | |
$latest = Get-ChildItem $dir -Include $ext -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 1 | |
AddLog "most recent file: $latest" -psh $false | |
if ((Get-Childitem $dir -Include $ext -Recurse | Where {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}).count -lt 1) { | |
AddLog "No backups for at least 24 hours." $true | |
} else { | |
AddLog "Latest backup successful." $true | |
} | |
#delete files older than 14 days | |
$Days = "14" | |
$LastWrite = (Get-Date).AddDays(-$Days) | |
$Files = Get-Childitem $dir -Include $ext -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} | |
if ($Files.Count -eq 0) { | |
AddLog "no old archives to delete" | |
} else { | |
foreach ($File in $Files) | |
{ | |
if ($File -ne $NULL) | |
{ | |
AddLog "deleting archive $File" -psh $false | |
Remove-Item $File.FullName | out-null | |
} | |
} | |
} | |
#delete any files not ending .gz | |
$Files = Get-Childitem $dir -Exclude $ext -Recurse | |
if ($Files.Count -ne 0) { | |
foreach ($File in $Files) | |
{ | |
if ($File -ne $NULL) | |
{ | |
AddLog "deleting invalid file $File" -psh $false | |
Remove-Item $File.FullName | out-null | |
} | |
} | |
} | |
AddLog "maintainer-finish" -psh $false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment