/export not working in a script created by script + man. fix

hi, i wrote a script that sets up email host, creates a backup script and schedules that script

the script itsself creates a .backup and .rsc with the actual timestamp, sends that via mail, removes those files and then creates a permanent .backup and .rsc on the device itsself as a local fallback.

the thing is, when the script is run manually via gui or via shell, it works, but when it is scheduled at first it creates and sends the .backup but the /export command for the .rsc stops and the script hangs. I also found a workaround fix. When i create another script where i try i.e. to just do a /export file=1 and schedule that script. The file is created and after that the inital script also begins to work when scheduled. Somehow this looks like a bug to me.
/system script add name=backup source={:log info (“backup: run”)

:local permanentbackupfile ([/system identity get name])
:local backupfile ([/system identity get name] . “-” . [:pick [/system clock get date] 7 11] . “.” . [:pick [/system clock get date] 0 3] . “.” . [:pick [/system clock get date] 4 6] . “-” . [/sys cl get time])
:local extensions { “.backup”, /
“.rsc” }
:local subject (“"Mikrotik " . [/system identity get name] . " Backup"”)
:local receiver “x@xxx”
:local body “.backup = full backup\n.rsc = config”

:foreach r in=[:toarray $extensions] do={
if ([:tostr $r] = “.backup”) do={
:log info (“backup: creating " . [:tostr $r] . " file run : /system backup save name=” . $backupfile)
/system backup save name=$backupfile
}
if ([:tostr $r] = “.rsc”) do={
:log info (“backup: creating " . [:tostr $r] . " file run : /export file=” . $backupfile)
/export file=($backupfile)
}
:log info (“backup: creating " . [:tostr $r] . " file end”)

:delay 3
:log info (“backup: sending email run : /tool e-mail send to=” . $receiver . " subject=" . $subject . " file=" . $backupfile . [:tostr $r])
/tool e-mail send to=$receiver subject=$subject file=($backupfile . [:tostr $r]) body=$body

:log info (“backup: sending email end”)
delay 2
:log info ("backup: removing temp file run : /file rem " . $backupfile . [:tostr $r])
/file rem ($backupfile . [:tostr $r])
:log info (“backup: removing temp file end”)
}

:log info (“backup: creating permanent backupfile run : /system backup save name=” . $permanentbackupfile)
/system backup save name=$permanentbackupfile
:log info (“backup: creating permanent backupfile end”)
:log info (“backup: creating permanent scriptfile run : /system backup save name=” . $permanentbackupfile)
/export file=$permanentbackupfile
:log info (“backup: creating permanent scriptfile end”)

:log info (“backup: end”)}

/tool e-mail
set address=xxx
from=admin@xxx
user=admin@xxx
password=xxx
port=587
start=yes

/system scheduler
add disabled=no interval=4w name=email-backup-schedule
on-event=“/system script run email-backup”
start-date=jun/08/2012 start-time=9:50:00

This is the corrected script

:log info message="backup: run";

:local permanentbackupfile value=[/system identity get name];
:local backupfile value=([/system identity get name]."-".[:pick [/system clock get date] 7 11].".".[:pick [/system clock get date] 0 3].".".[:pick [/system clock get date] 4 6]."-".[/system clock get time]);
:local extensions value=[:toarray ".backup,.rsc"];
:local receiver value="x@xxx";
:local subject value=("Mikrotik ".[/system identity get name]." Backup");
:local body value=".backup = full backup\r\n.rsc = config";

:foreach r in=[:toarray $extensions] do={
 if ([:tostr $r] = ".backup") do={
  :log info message=("backup: creating ".[:tostr $r]." file run: /system backup save name=".$backupfile);
  /system backup save name=$backupfile;
 }
 if ([:tostr $r] = ".rsc") do={
  :log info message=("backup: creating ".[:tostr $r]." file run: /export file=".$backupfile);
  /export file=$backupfile;
 }
 :delay delay-time=10s;
 :log info message=("backup: creating ".[:tostr $r]." file end");
 :log info message=("backup: sending email run: /tool e-mail send to=".$receiver." subject=".$subject." file=".$backupfile. [:tostr $r]);
 /tool e-mail send to=$receiver subject=$subject body=$body file=($backupfile.[:tostr $r]);
 :delay delay-time=10s;
 :log info message="backup: sending email end";
 :log info message=("backup: removing temp file run: /file remove ".$backupfile.[:tostr $r]);
 /file remove ($backupfile.[:tostr $r]);
 :log info message="backup: removing temp file end";
}

:log info message=("backup: creating permanent backupfile run: /system backup save name=".$permanentbackupfile);
/system backup save name=$permanentbackupfile;
:delay delay-time=10s;
:log info message="backup: creating permanent backupfile end";

:log info message=("backup: creating permanent scriptfile run: /export file=".$permanentbackupfile);
/export file=$permanentbackupfile;
:delay delay-time=10s;
:log info message="backup: creating permanent scriptfile end";

:log info message="backup: end";

And you need to set that policy to this script and the scheduling, or the script do not have the sufficents right to do all.

policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive

as far as i can see you only changed my delay x seconds to

i have it working now with my script too, but i had to create another script issuing a /export command there to somehow release the break on the /export command in my script, after that my script is running too. I assume it has something to do with my script creating the backup script in first place.

Really one good response for someone who tried to help you.

Use “fc” to see the difference.
Your export on first post is one mess.

Are you read this:

And you need to set that policy to this script and the scheduling, or the script do not have the sufficents right to do all.

policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive

or you are too busy to find the “100” differencies?

All the text added, ignoring indexing:

:log info > message=> “backup: run”> ;

:local permanentbackupfile > value=> [/system identity get name]> ;
:local backupfile > value=> ([/system identity get name].“-”.[:pick [/system clock get date] 7 11].“.”.[:pick [/system clock get date] 0 3].“.”.[:pick [/system clock get date] 4 6].“-”.[/system cl> ock > get time])> ;
:local extensions > value=[:toarray > “.backup,.rsc”> ];
:local receiver > value=> “x@xxx”> ;
:local subject > value=> (“Mikrotik “.[/system identity get name].” Backup”)> ;
:local body > value=> “.backup = full backup> \r> \n.rsc = config”> ;

:foreach r in=[:toarray $extensions] do={
if ([:tostr $r] = “.backup”) do={
:log info > message=> (“backup: creating “.[:tostr $r].” file run: /system backup save name=”.$backupfile)> ;
/system backup save name=$backupfile> ;
}
if ([:tostr $r] = “.rsc”) do={
:log info > message=> (“backup: creating “.[:tostr $r].” file run: /export file=”.$backupfile)> ;
/export file=$backupfile> ;
}
:delay delay-time=10s> ;
:log info > message=> (“backup: creating “.[:tostr $r].” file end”)> ;
:log info > message=> (“backup: sending email run: /tool e-mail send to=”.$receiver." subject=“.$subject.” file=".$backupfile. [:tostr $r])> ;
/tool e-mail send to=$receiver subject=$subject body=$body file=($backupfile.[:tostr $r])> ;
:> delay delay-time=10s> ;
:log info > message=> “backup: sending email end”> ;
:log info > message=> ("backup: removing temp file run: /file remove ".$backupfile.[:tostr $r])> ;
/file rem> ove > ($backupfile.[:tostr $r])> ;
:log info > message=> “backup: removing temp file end”> ;
}

:log info > message=> (“backup: creating permanent backupfile run: /system backup save name=”.$permanentbackupfile)> ;
/system backup save name=$permanentbackupfile> ;
:delay delay-time=10s;
:log info > message=> “backup: creating permanent backupfile end”> ;

:log info > message=> (“backup: creating permanent scriptfile run: > /export file> =”.$permanentbackupfile)> ;
/export file=$permanentbackupfile> ;
:delay delay-time=10s;
:log info > message=> “backup: creating permanent scriptfile end”> ;

:log info > message=> “backup: end”> ;

i see you put some ; here and there and you changed the delay command, for me it did not look messy and now not really different :slight_smile:

thx for the effort and for making the fc but do you really think that explains why the script did run manually but not when scheduled? i’ll give it a try and try to reproduce the behaviour i found

You still to not read this: ?

And you need to set that policy to this script and the scheduling, or the script do not have the sufficents right to do all.

policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive

For completion I post also this:

All the text removed, ignoring excessive spacing:

{> :log info > (> “backup: run”> )


:local permanentbackupfile > (> [/system identity get name]> ) >
:local backupfile ([/system identity get name] . “-” . [:pick [/system clock get date] 7 11] . “.” . [:pick [/system clock get date] 0 3] . “.” . [:pick [/system clock get date] 4 6] . “-” . [/sys cl get time])
:local extensions { “.backup”, > /
“.rsc” }
:local subject ("> "> Mikrotik " . [/system identity get name] . " Backup> "> ")
:local receiver “x@xxx”
:local body “.backup = full backup\n.rsc = config”

:foreach r in=[:toarray $extensions] do={
if ([:tostr $r] = “.backup”) do={
:log info (“backup: creating " . [:tostr $r] . " file run : /system backup save name=” . $backupfile)
/system backup save name=$backupfile
}
if ([:tostr $r] = “.rsc”) do={
:log info (“backup: creating " . [:tostr $r] . " file run : /export file=” . $backupfile)
/export file=> (> $backupfile> )
}
:log info (“backup: creating " . [:tostr $r] . " file end”)

:delay 3
:log info (“backup: sending email run : /tool e-mail send to=” . $receiver . " subject=" . $subject . " file=" . $backupfile . [:tostr $r])
/tool e-mail send to=$receiver subject=$subject file=($backupfile . [:tostr $r]) body=$body

:log info > (> “backup: sending email end”> )
delay 2
:log info ("backup: removing temp file run : /file rem " . $backupfile . [:tostr $r])
/file rem ($backupfile . [:tostr $r])
:log info > (> “backup: removing temp file end”> )
}

:log info (“backup: creating permanent backupfile run : /system backup save name=” . $permanentbackupfile)
/system backup save name=$permanentbackupfile
:log info > (> “backup: creating permanent backupfile end”> )

:log info (“backup: creating permanent scriptfile run : > /system backup save name> =” . $permanentbackupfile)
/export file=$permanentbackupfile
:log info > (> “backup: creating permanent scriptfile end”> )

:log info > (> “backup: end”> )}

i had checked all policy boxes and i still do not think my non routeros conform and messy skripting is the key to the fact it works manually and not when scheduled, but as i already said before ill give it a try

On the first post, the rule are not exported, and is whiy I write that, but I want you let know why I have modified some parts, ignoring the right full syntax:

:log info message=“backup: run”;

:local permanentbackupfile value=[/system identity get name];
:local backupfile value=([/system identity get name].“-”.[:pick [/system clock get date] 7 11].“.”.[:pick [/system clock get date] 0 3].“.”.[:pick [/system clock get date] 4 6].“-”.[/system clock get time]);
:local extensions value=[:toarray “.backup,.rsc”]; > <<<— replaced: better way to create array…
:local receiver value=“x@xxx”;
:local subject value=(“Mikrotik “.[/system identity get name].” Backup”); > <<<— removed extra "
:local body value=“.backup = full backup\r\n.rsc = config”; > <<<— added \r for full compatibility

:foreach r in=[:toarray $extensions] do={
if ([:tostr $r] = “.backup”) do={
:log info message=(“backup: creating “.[:tostr $r].” file run: /system backup save name=”.$backupfile);
/system backup save name=$backupfile;
}
if ([:tostr $r] = “.rsc”) do={
:log info message=(“backup: creating “.[:tostr $r].” file run: /export file=”.$backupfile);
/export file=$backupfile;
}
:delay delay-time=10s; > <<<— increasing time available to generate backups, on the latest version is needed more time, and I not know why…
:log info message=(“backup: creating “.[:tostr $r].” file end”);
:log info message=(“backup: sending email run: /tool e-mail send to=”.$receiver." subject=“.$subject.” file=".$backupfile. [:tostr $r]);
/tool e-mail send to=$receiver subject=$subject body=$body file=($backupfile.[:tostr $r]);
:delay delay-time=10s; > <<<— added missing “:” and increasing time available to email to send the message and for the system to delete the file when are “free”
:log info message=“backup: sending email end”;
:log info message=("backup: removing temp file run: /file remove ".$backupfile.[:tostr $r]);
/file remove ($backupfile.[:tostr $r]);
:log info message=“backup: removing temp file end”;
}

:log info message=(“backup: creating permanent backupfile run: /system backup save name=”.$permanentbackupfile);
/system backup save name=$permanentbackupfile;
:delay delay-time=10s; > <<<— added for same reason as before
:log info message=“backup: creating permanent backupfile end”;

:log info message=(“backup: creating permanent scriptfile run: /export file=”.$permanentbackupfile); <<<— simply fixed the log
/export file=$permanentbackupfile;
:delay delay-time=10s; > <<<— added for same reason as before
:log info message=“backup: creating permanent scriptfile end”;

:log info message=“backup: end”;

nice explination

please refer to this explanation

According to it the script and schedule task should have all the policy rights.
I had the same issue, when updated the rights for policies and script (all enabled) the “/export” started to work