search tag # rextended hack sort file by date
Based on my scripts:
http://forum.mikrotik.com/t/i-did-it-script-to-compute-unix-time/68576/1
Step 1 [DONE]: convert all backup filenames and date to a 2D array and convert the date from mmm-DD-YYYY to YYYY-MM-DD:
Step 2 [DONE]: sort the array by YYYY-MM-DD using an hack
Step 3 [DONE]: delete the older backups over max “X” allowed
I comment the line when the file is removed for security, the number of max backup allowed is defined on maxbackup variable insidethe script
for sort all file, just remove > where type=“backup”< on find
(compatible also with new date format on RouterOS 7.10+)
/file
{
:local maxbackup 5
:local crtime ""
:local filename ""
:local filelist [:toarray ""]
:foreach file in=[find where type="backup"] do={
:set crtime [get $file creation-time]
:local vdoff [:toarray "0,4,5,7,8,10"]
:local MM [:pick $crtime ($vdoff->2) ($vdoff->3)]
:local M [:tonum $MM]
:if ($crtime ~ ".../../....") do={
:set vdoff [:toarray "7,11,1,3,4,6"]
:set M ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $crtime ($vdoff->2) ($vdoff->3)] -1] / 2)
:if ($M>12) do={:set M ($M - 12)}
:set MM [:pick (100 + $M) 1 3]
}
:set crtime "$[:pick $crtime ($vdoff->0) ($vdoff->1)]-$MM-$[:pick $crtime ($vdoff->4) ($vdoff->5)] $[:pick $crtime 12 20]"
:set filename [get $file name]
:set filelist ($filelist, [[:parse ":return {\"$crtime\"=\"$filename\"}"]])
:put "\"$crtime\"=\"$filename\""
}
:local currentbk [:len $filelist]
:local overbackp ($currentbk - $maxbackup)
:if ($currentbk > $maxbackup) do={
:foreach x,y in=$filelist do={
:if ($overbackp > 0) do={
:log info "There are more than $maxbackup backups, $y deleted"
# remove [find where name="$y"]
:set overbackp ($overbackp - 1)
}
}
}
}