Question
· Jul 8, 2019

Purge backups thorugh task manager

I'm currenlty searching the developers coummnity , but thought I asked this question while  I search.  How can I use the task schedulrer to purge backups? Do i have to create my own class? Examples on doing this?

Discussion (3)1
Log in or sign up to continue

Assuming you're talking about the online backup function within ensemble/HS/etc - I use a task that will run a purge based on the age of the file, and then runs the backup. The order of the two methods is important if you set your retention period to 0, as you'll end up deleting the backup you just made (I'm neither confirming or denying if this happened to me).

Class Live.Schedule.BackupPurge Extends %SYS.Task.BackupAllDatabases{

Parameter TaskName = "Backup With Purge";

Property Daystokeep As %Integer(VALUELIST = ",0,1,2,3,4,5") [ InitialExpression = "1" ];

Method OnTask() As %Status{
    //Call PurgeBackup Method, Return Status
    Set tsc = ..PurgeBackups(..Device,..Daystokeep)
    Set tsc = ..RunBackup()
    Quit tsc
}

Method PurgeBackups(Directory As %String, DaysToKeep As %Integer) As %Status{
    // Calculate the oldest date to keep files on or after
    set BeforeThisDate = $zdt($h-DaysToKeep_",0",3)

    // Gather the list of files in the specified directory
    set rs=##class(%ResultSet).%New("%File:FileSet")
    do rs.Execute(Directory,"*.cbk","DateModified")

    // Step through the files in DateModified order
    while rs.Next() {
        set DateModified=rs.Get("DateModified")
        if BeforeThisDate]DateModified {
            // Delete the file
            set Name=rs.Get("Name")
            do ##class(%File).Delete(Name)
        }
        // Stop when we get to files with last modified dates on or after our delete date
        if DateModified]BeforeThisDate 
        set tSC = 1
    }
    quit tSC
}

Method RunBackup() As %Status{
    d $zu(5,"%SYS")
    Set jobbackup = 0
    Set quietflag = 1
    Set Device = ..Device
    Set tSC = ##class(Backup.General).StartTask("FullAllDatabases", jobbackup, quietflag, Device, "0")
    Quit tSC
 }

}

The downside to this is you will end up with an extra backup file in your backup location if you run the backup manually as the purge is based on the file age. Not a massive problem unless you're storing in a location with a finite amount of disk space.

You can schedule the following task which removes any file from a directory, based on its age, using Python:

Class admin.purge Extends %SYS.Task.Definition
{

Property Directory As %String(MAXLEN = 2000) [ InitialExpression = "/usr/irissys/mgr/Backup" ];

Property DaysToKeep As %Integer(VALUELIST = ",0,1,2,3,4,5") [ InitialExpression = "1" ];

Method OnTask() As %Status
{
    set sc = $$$OK
    Try {
        do ..purge(..Directory,..DaysToKeep)
    }
    Catch ex {
        Set sc=ex.AsStatus()
    }
    return sc
}

ClassMethod purge(path As %String, daysToKeep As %Integer) As %Status [ Language = python ]
{
import iris
import os
import time
from datetime import datetime, timedelta

event = "[TASK PURGE OLD BACKUP FILES]"

class FileDeletionError(Exception):
    """Custom exception for file deletion errors."""
    pass

def delete_old_files(path, days_limit):
    limit_date = datetime.now() - timedelta(days=int(days_limit))

    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isfile(file_path):
            creation_date = datetime.fromtimestamp(os.path.getctime(file_path))
            if creation_date < limit_date:
                try:
                    os.remove(file_path)
                    iris.cls("%SYS.System").WriteToConsoleLog(f"{event} Deleted: {str(file_path)}")
                except PermissionError:
                    raise FileDeletionError(f"Permission error: Unable to delete {file}")
                except FileNotFoundError:
                    raise FileDeletionError(f"File not found: {file}")
                except Exception as e:
                    raise FileDeletionError(f"Unexpected error while deleting {file}: {str(e)}")

try:
    iris.cls("%SYS.System").WriteToConsoleLog(f"{event} Executing task to delete backup files from directory {path} created more than {str(daysToKeep)} days ago")
    days_limit = daysToKeep
    delete_old_files(path, days_limit)
except FileDeletionError as e:
    iris.cls("%SYS.System").WriteToConsoleLog(f"{event} Error during deletion: {str(e)}",0,1)
except Exception as e:
    iris.cls("%SYS.System").WriteToConsoleLog(f"{event} Unexpected error: {str(e)}",0,1)
}

}

The task logs its actions in messages.log :

01/13/25-18:21:00:549 (35722) 0 [Utility.Event] [TASK PURGE OLD BACKUP FILES] Executing task to delete backup files from directory /usr/irissys/mgr/Backup created more than 0 days ago
01/13/25-18:21:00:550 (35722) 0 [Utility.Event] [TASK PURGE OLD BACKUP FILES] Deleted: /usr/irissys/mgr/Backup/FullAllDatabases_20250113_009.cbk
01/13/25-18:21:00:550 (35722) 0 [Utility.Event] [TASK PURGE OLD BACKUP FILES] Deleted: /usr/irissys/mgr/Backup/FullAllDatabases_20250113_008.log
01/13/25-18:21:00:598 (35722) 0 [Utility.Event] [TASK PURGE OLD BACKUP FILES] Deleted: /usr/irissys/mgr/Backup/FullAllDatabases_20250113_008.cbk
01/13/25-18:21:00:598 (35722) 0 [Utility.Event] [TASK PURGE OLD BACKUP FILES] Deleted: /usr/irissys/mgr/Backup/FullAllDatabases_20250113_009.log