Previous backups from N days are not being deleted successfully when VM name contains an underscore character on the VM name
Hello,
I tested your tool on latest published version (24.08.29) but when I execute with the following arguments:
.\Hyper-V-Backup.ps1 -BackupTo D:\HV_VM_BKPs\ -NoPerms -Keep 30 -Compress -L D:\scripts\logs\ -LogRotate 30
* BackupTo
* -NoPerms
* - Keep 30 (last 30 days backups)
* - Compress
* - L
* - LogRotate 30 (last 30 days logs)
then previous backups older than 30 days, are not being successfully removed.
I think it could be happening when my VM is called SRV_DOMOTICS and then the function that this part of the code (line 637) has some issue due to the RemoveFilePat string defined there. Do you thing this is something you could improve. When a VM has underscore character on it?
## Remove previous backup files older than X days. -Keep and -Compress switch are configured.
else {
Write-Log -Type Info -Evt "(VM:$Vm) Removing compressed backups older than: $History days"
## Remove previous compressed backups older than the configured number of days.
If ($ShortDate)
{
ReportRemove -RemoveDir $WorkDir -RemoveFilePat "-*-*-*.*" -RemoveDirOpt $false -RemoveHistory $History
}
else {
ReportRemove -RemoveDir $WorkDir -RemoveFilePat "-*-*-*_*-*-*.*" -RemoveDirOpt $false -RemoveHistory $History
}
Thanks,
Hello, thank you for this report - I'll see what I can do to provide a fix
I think I discovered what is the issue. At least for me.
If you copy the backups while you perform some testing then the CreationTime and LastWriteTime are going to be different.
You are using CreationTime of the backup file to perform the selection of how many files you would like to be included to be deleted.
I modified the script to use instead of CreationTime to use LastWriteTime. This is the output I got on my log file:
--- Running with the following config ---
Utility Version: 24.08.29
Hostname: SERVERW10.
Windows Version: 10.0.19045.
No. of VMs: 1.
VMs to backup:
SRV_DOMOTICA
Backup directory: D:\HV_VM_BKPs\.
-NoPerms switch: True.
-LowDisk switch: True.
-Compress switch: True.
Backups to keep: 30 days
Logs directory: D:\scripts\logs\.
---
2025-01-17 13:16:29 [INFO] Process started
2025-01-17 13:16:40 [INFO] (VM:SRV_DOMOTICA) has [4] CPU cores, [6GB] RAM, Storage: [Current Size = 27GB - Max Size = 80GB]
2025-01-17 13:17:05 [INFO] (VM:SRV_DOMOTICA) Removing compressed backups older than: 30 days
FileName CreationTime LastWriteTime
SRV_DOMOTICA-2024-11-15_15-22-57.zip 05/01/2025 16:15:09 15/11/2024 16:04:59
SRV_DOMOTICA-2024-11-15_17-34-45.zip 05/01/2025 16:15:23 15/11/2024 17:52:10
SRV_DOMOTICA-2024-11-15_22-01-18.zip 05/01/2025 16:15:37 15/11/2024 22:18:34
SRV_DOMOTICA-2024-11-22_22-01-17.zip 05/01/2025 16:15:51 22/11/2024 22:18:29
SRV_DOMOTICA-2024-11-29_22-01-19.zip 05/01/2025 16:16:06 29/11/2024 22:18:46
SRV_DOMOTICA-2024-12-06_22-01-15.zip 05/01/2025 16:16:21 06/12/2024 22:18:46
What do you think?