Удаление файлов по дате.
Добавлено: 03 фев 2017, 09:38
Всем здрасти! Подключил к своему RB951Ui-2HnD внешний диск для бекапов. Помогите со скриптом удаления всего, что старше определенной даты. 

Форум поддержи и обмена опытом пользователей оборудования RouterBOARD и операционной системы RouterOS Латвийского производителя MikroTik
https://forummikrotik.ru/
Код: Выделить всё
# Only delete files older than X days ago
# tested on RouterOS v6.38.1
{
# how many days ago
:local daysAgo 7;
# only delete files that contain this in their name
:local filter "disk1/*";
# 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={
# delete files
/file remove [find name~"$filter"];
}
}
}
Код: Выделить всё
# Only delete files older than X days ago
# tested on RouterOS v6.39.1
{
# how many days ago
:local daysAgo 7;
# only delete files that contain this in their name
:local filter "video";
# 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 ];
#extract file name
:local fileName [/file get number="$i" name ];
# 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={
# delete files
/file remove [find name~"$fileName"];
}
}
}
Для автоматического выполнения есть шедулер.Cool_Mike писал(а): ↑09 ноя 2018, 13:31 Ага... Посмотрел на Wiki синтаксис циклов в скриптовом языке. Просто написать как в Си "for (;;) {};" или "while (1){};" и поместить туда тело данного скрипта не получиться. Однако, есть конструкция, аналогичная Си - do... while (); Думаю, что это поможет зациклить данный скрипт, что бы в ручную не заходить и не запускать данный скрипт для автоматического удаления файлов старше 30-ти дней. Т.е. нечто подобное :do {"script body"} while (1);
Код: Выделить всё
:foreach i in=[/file find type=backup] do={
Код: Выделить всё
{
:local daysAgo 30;
:local filter "disk1/";
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
:local curDate [ /system clock get date ];
:local curMonth [ :pick $curDate 0 3 ];
:set curMonth ([ :find $months $curMonth -1 ] + 1);
:local curDay [ :pick $curDate 4 6 ];
:local curYear [ :pick $curDate 7 11 ];
:foreach i in=[/file find type=backup] do={
:local fileDate [/file get number="$i" creation-time]
:set fileDate [ :pick $fileDate 0 11 ];
:local fileMonth [ :pick $fileDate 0 3 ];
:set fileMonth ([ :find $months $fileMonth -1 ] + 1);
:local fileDay [ :pick $fileDate 4 6 ];
:local fileYear [ :pick $fileDate 7 11 ];
:local sum 0;
:set sum ($sum + (($curYear - $fileYear)*365));
:set sum ($sum + (($curMonth - $fileMonth) * 30));
:set sum ($sum + ($curDay - $fileDay));
:if ($sum >= $daysAgo && [/file get number="$i" name]~"$filter") do={
/file remove $i;
}
}
}