Community discussions

MikroTik App
 
tigro11
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 87
Joined: Tue Feb 20, 2018 12:31 am

Script delete old log files for date

Wed Sep 12, 2018 4:28 pm

hi guys, I have a huge problem, on my rb that I use for hotspots I have a lot of log files like log.01.txt etc .. only that in the long run I fill the rb.
I wanted to create a script to copy all the log files to an ftp server and then delete all the old log files of more than 30 days.
Can you help me?
thank you very much
valerio
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: Script delete old log files for date

Thu Sep 13, 2018 3:11 pm

schedule this script every 1d
:do {
:local array {"jan"; "feb"; "mar"; "apr"; "may"; "jun"; "jul"; "aug"; "sep"; "oct"; "nov"; "dec"}
:local DATE [/system clock get date];
:local day [:pick $DATE 4 6];
:local month ([:find $array [:pick $DATE 0 3]]);
 :if ($month=0) do={
 :set $month 11;
 :set $year ($year-1);
 } else={
 :set $month ($month-1)
 }
:local year [:pick $DATE 7 11];
:local bmDATE (($array->$month) . "/" . $day . "/" . $year);

:do {/file remove number=[find where name~"log" && creation-time~$bmDATE]} on-error={:log error "removing failed"}

} on-error={:log error "script failed"}
You do not have the required permissions to view the files attached to this post.
 
tigro11
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 87
Joined: Tue Feb 20, 2018 12:31 am

Re: Script delete old log files for date

Thu Sep 13, 2018 4:08 pm

perfect, if I wanted to delete only the older 10-day examples, do I have to modify the script?
is there a command in ftp to move the log files? it would be ideal, so you do not need to delete all the log files.
are stopped on the function where you can read all the log files in the rb and through a loop I wanted to copy them automatically on the nas and keep only the daily logs
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: Script delete old log files for date

Thu Sep 13, 2018 7:27 pm

if I wanted to delete only the older 10-day examples, do I have to modify the script?
yes, you have to modify the script.

is there a command in ftp to move the log files? it would be ideal, so you do not need to delete all the log files.
yes, you can use fetch command to send the log files to other ftp site before remove them.
 
tigro11
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 87
Joined: Tue Feb 20, 2018 12:31 am

Re: Script delete old log files for date

Thu Sep 13, 2018 8:09 pm

very thanks
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: Script delete old log files for date

Thu Sep 13, 2018 8:24 pm

You're very welcome
 
tigro11
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 87
Joined: Tue Feb 20, 2018 12:31 am

Re: Script delete old log files for date

Fri Sep 14, 2018 4:41 pm

I use this script to find the old 1-day files.
only that the result gives me all the files in the rb, is it possible to search in just one specific folder example disk1/log?
This is the script

thank you
##############################################################################
{
# how many days ago
:local daysAgo 1;

# only show files that contain this in their name txt
:local filter "txt";

# months array
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
# get current date
:local curDate [ /system clock get date ];
# extract current month
:local curMonth [ :pick $curDate 0 3 ];
# get position of our month in the array = month number
:set curMonth ([ :find $months $curMonth -1 ] + 1);
# extract current day
:local curDay [ :pick $curDate 4 6 ];
# extract current year
:local curYear [ :pick $curDate 7 11 ];

# loop through all files
:foreach i in=[/file find] do={
# get this file's creation time
:local fileDate [/file get number="$i" creation-time]
# extract the date
:set fileDate [ :pick $fileDate 0 11 ];
# extract the month
:local fileMonth [ :pick $fileDate 0 3 ];
# get position of our month in the array = month number
:set fileMonth ([ :find $months $fileMonth -1 ] + 1);
# extract the day
:local fileDay [ :pick $fileDate 4 6 ];
# extract the year
:local fileYear [ :pick $fileDate 7 11 ];

# the sum of total days
:local sum 0;
# subtract the file's year from the current year, multiply times 365 to get approx days, add to sum
:set sum ($sum + (($curYear - $fileYear)*365));
# subtract the file's month from the current month, multiply times 30 to get approx days, add to sum
:set sum ($sum + (($curMonth - $fileMonth) * 30));
# subtract the file's day from the current day, add to sum
:set sum ($sum + ($curDay - $fileDay));
# if the sum is greater than or equal to our daysAgo and the file name contains our filter
:if ($sum >= $daysAgo && [/file get number="$i" name]~"$filter") do={
# show file info to the terminal $sum =giorni passati
:put ([/file get number="$i" creation-time ] . "\t" . [/file get number="$i" name] . " - " . "$sum days ago");
# if a match was found cancello i files vecchi
:put ( . "\t" .[/file get number="$i" name] );
#/file remove ( . "\t" .[/file get number="$i" name] );
}
}
}
#################################################################################
 
tigro11
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 87
Joined: Tue Feb 20, 2018 12:31 am

Re: Script delete old log files for date

Sun Sep 16, 2018 1:06 am

I solved almost the whole script, I miss a function that removes the file path in the variable.
I have the variable $filename with the file name including the path (disk1/log/log.0.txt), is it possible to remove the file path (disk1/log/) with a script?
Thanks so much

Who is online

Users browsing this forum: alixviral, TosLin and 24 guests