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 (2)0
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.