How to delete backup and RSC files that have been stored for more than 1 month

I have tried deleting the rsc file and the backup file with a script like the following

/delete file [find type=‘backup’];
/delete file [find type=‘script’];

but I want to leave some files that have been saved for more than a month, and delete files that are more than a month old, do you have any suggestions?

I do not have suggestions, only solutions.

:global datetime2epoch do={
:local dtime [:tostr $1]
/system clock
:local cyear [get date] ; :if ($cyear ~ “…-..-..”) do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
:if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime “$dtime 00:00:00”}
:if ([:len $dtime] = 15) do={:set dtime “$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]”}
:if ([:len $dtime] = 14) do={:set dtime “$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]”}
:if ([:len $dtime] = 8) do={:set dtime “$[get date] $dtime”}
:if ([:tostr $1] = “”) do={:set dtime (“$[get date] $[get time]”)}
:local vdate [:pick $dtime 0 [:find $dtime " " -1]]
:local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
:local vgmt [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
:local arrm [:toarray “0,0,31,59,90,120,151,181,212,243,273,304,334”]
:local vdoff [:toarray “0,4,5,7,8,10”]
:local MM [:pick $vdate ($vdoff->2) ($vdoff->3)]
:local M [:tonum $MM]
:if ($vdate ~ “…/../…”) do={
:set vdoff [:toarray “7,11,1,3,4,6”]
:set M ([:find “xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC” [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
:if ($M>12) do={:set M ($M - 12)}
}
:local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
:local totd ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
:return (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}

{
/system clock
:local now [$datetime2epoch (“$[get date] $[get time]”)]
:local wait 2592000 ; # seconds, 1 month
:local fdate 0
/file
:foreach file in=[find where type~“backup|script”] do={
:set fdate [$datetime2epoch [get $file creation-time]]
:if ($fdate < ($now - $wait)) do={remove $file}
}
}

Hi !

Please, how could a counter be added to know the number of files that the cycle is going to delete?

{
:global datetime2epoch
/system clock
:local now   [$datetime2epoch ("$[get date] $[get time]")]
:local wait  2592000 ; # seconds, 1 month
:local fdate 0
:local countFiles 
/file
:foreach file in=[find where type~"backup|script" && name~"usb1"] do={
    :set fdate [$datetime2epoch [get $file creation-time]]
    :if ($sdate) do={
        :set countFiles [print count-only where .id=$file]
        # remove $file
    }
}
:put $countFiles
:log info "[ $countFiles ] backup files have been deleted"
}

Thanks.

{
/system clock
:local now [$datetime2epoch (“$[get date] $[get time]”)]
:local wait 2592000 ; # seconds, 1 month
:local fdate 0
:local removed 0/file
:foreach file in=[find where type~“backup|script”] do={
:set fdate [$datetime2epoch [get $file creation-time]]
:if ($fdate < ($now - $wait)) do={
# remove $file
:set removed ($removed + 1)}
}
:put $removed**:log info “[ $removed ] backup or script files have been removed”**}

Thanks Rex, it works fine.

Do you mean “have been saved for less than a month” or do you really want to keep some of the backups more than a month old?

Another option to save ones less than a month old would be to use the day of the month in the name of the file, then delete the matching file when starting the backup script.

1st of the month’s file Backup-01.rsc
2nd of the month’s file Backup-02.rsc

January’s files from the 29th to 31 still stick around until the end of March, but that wouldn’t be a big concern for myself.

When trying to use this code on RouterOS v7.16.1, I get the following error:

input does not match any value of value-name

I guess there were some internal changes for the /system/clock?
Can you please help me get it working on v7.16.1?
Thank you

Isn’t it “creation-time” that doesn’t exist anymore ? The only value that exists now is “last-modified”.

Just read changelogs before blindly upgrade or update something:

What’s new in 7.16 (2024-Sep-20 16:00):
[…]
*) file - renamed “creation-time” to “last-modified”;
[…]

So, on script rename “creation-time” to “last-modified”