Community discussions

MikroTik App
 
KurangAceng
just joined
Topic Author
Posts: 1
Joined: Fri Mar 08, 2024 6:14 am

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

Fri Mar 08, 2024 6:20 am

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?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Fri Mar 08, 2024 11:46 am

I do not have suggestions, only solutions.

example code

: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}
    }
}
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

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

Sat Mar 16, 2024 9:13 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Sun Mar 17, 2024 1:16 am

example code

{
    /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"
}
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

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

Sun Mar 17, 2024 10:20 am

Thanks Rex, it works fine.
 
kevinds
Long time Member
Long time Member
Posts: 651
Joined: Wed Jan 14, 2015 8:41 am

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

Wed Mar 20, 2024 8:20 pm

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

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?
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.

Who is online

Users browsing this forum: No registered users and 7 guests