Page 1 of 1

Script delete old log files for date

Posted: Wed Sep 12, 2018 4:28 pm
by tigro11
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

Re: Script delete old log files for date

Posted: Thu Sep 13, 2018 3:11 pm
by ADahi
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"}

Re: Script delete old log files for date

Posted: Thu Sep 13, 2018 4:08 pm
by tigro11
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

Re: Script delete old log files for date

Posted: Thu Sep 13, 2018 7:27 pm
by ADahi
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.

Re: Script delete old log files for date

Posted: Thu Sep 13, 2018 8:09 pm
by tigro11
very thanks

Re: Script delete old log files for date

Posted: Thu Sep 13, 2018 8:24 pm
by ADahi
You're very welcome

Re: Script delete old log files for date

Posted: Fri Sep 14, 2018 4:41 pm
by tigro11
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] );
}
}
}
#################################################################################

Re: Script delete old log files for date

Posted: Sun Sep 16, 2018 1:06 am
by tigro11
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