Backup script stopped working after 7.20 update

Hi everyone!

I use several “RBD52G-5HacD2HnD” routers for years now.

Have been using this script for my daily backup of settings on all of them …

2 days ago I updated them to RouterOS to 7.20 ( from 7.19.4 )

Since then this script has stopped working with the following error in log on ALL my installations:

“invalid file name (:export; line 37)”

line 37 of my script looks like : /export show-sensitive file=($sfname.".rsc")

The complete script follows :


:local saveUserDB false
:local saveSysBackup true
:local encryptSysBackup false
:local saveRawExport true

:local FTPServer "192.168.1.1"
:local FTPPort 21
:local FTPUser "user"
:local FTPPass "pass"

:local filename;

:local date [/system clock get date];
:local time [/system clock get time];
:local name [/system identity get name];

:local varHour [:pick $time 0 2];
:local varMin [:pick $time 3 5];
:local varSec [:pick $time 6 8];

:set filename ("Backup"."".$name."".$date."_".$varHour."-".$varMin."-".$varSec);

:local sfname ("/".$filename);

:if ($saveUserDB) do={
/tool user-manager database save name=($sfname.".umb")
:log info message="User Manager DB Backup Finished"
}

:if ($saveSysBackup) do={
:if ($encryptSysBackup = true) do={ /system backup save name=($sfname.".backup") }
:if ($encryptSysBackup = false) do={ /system backup save dont-encrypt=yes name=($sfname.".backup") }
:log info message="System Backup Finished"
}

if ($saveRawExport) do={
/export show-sensitive file=($sfname.".rsc")
:log info message="Raw configuration script export Finished"
}

:delay 5s

:local backupFileName "";

:foreach backupFile in=[/file find] do={
:set backupFileName ("/".[/file get $backupFile name])
:if ([:typeof [:find $backupFileName $sfname]] != "nil") do={
/tool fetch address=$FTPServer port=$FTPPort src-path=$backupFileName user=$FTPUser mode=ftp password=$FTPPass dst-path=$backupFileName upload=yes
}
}
:delay 5s
:foreach backupFile in=[/file find] do={
:if ([:typeof [:find [/file get $backupFile name] "Backup_"]]!="nil") do={
/file remove $backupFile
}
}

:log info message="Successfully removed Temporary Backup Files"
:log info message="Automatic Backup Completed Successfully"


Any help would be appreciated!

For me this message is very clear. Check your filename.

You could try to enable "sanitize-names" property under "/console/settings" menu and see if this helps.

See changelog 7.15:

*) console - added "sanitize-names" property under "/console/settings" menu (option for replacing reserved characters with underscores for files, disabled by default);

Thank You for Your reply ...

if I add ":log info message=$filename" after setting the filename I get : Backup_PCX_2025-10-06_13-50-37

which is a reasonable filename - one I have been using on all my configurations without any error messages until this update :frowning:

looks valid. Maybe it isn't the filename but the path instead?

Thank You for the hint ...

I changed one line :

:local sfname ("/".$filename);

to

:local sfname ($filename);

and the script runs again as expected.

Sorry for the bother, and thanks for the time and help!

What should I change?

automated backup export ftp

ftp configuration

:local ftphost "wwwwwwwwwwwww"
:local ftpuser "wwwwwwww"
:local ftppassword "wwwwwwwwwwwww"
:local ftppath "/MTK/"

months array

:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");

get time

:local ts [/system clock get time]
:set ts ([:pick $ts 0 2].[:pick $ts 3 5].[:pick $ts 6 8])

get Date

:local ds [/system clock get date]

convert name of month to number

:local month [ :pick $ds 0 3 ];
:local mm ([ :find $months $month -1 ] + 1);
:if ($mm < 10) do={ :set mm ("0" . $mm); }

set $ds to format YYYY-MM-DD

:set ds ([:pick $ds 7 11] . $mm . [:pick $ds 4 6])

file name for system backup - file name will be Mikrotik-servername-date-time.backup

:local fname1 ("/Mikrotik-".[/system identity get name]."-".$ds."-".$ts.".backup")

file name for config export - file name will be Mikrotik-servername-date-time.rsc

:local fname2 ("/Mikrotik-".[/system identity get name]."-".$ds."-".$ts.".rsc")

backup the data

/system backup save name=$fname1
:log info message="System backup finished (1/2).";
/export show-sensitive compact file=$fname2
:log info message="Config export finished (2/2)."

upload the user manager backup

:log info message="Uploading system backup (1/2)."
/tool fetch address="$ftphost" src-path=$fname1 user="$ftpuser" mode=ftp password="$ftppassword" dst-path="$ftppath/$fname1" upload=yes

upload the config export

:log info message="Uploading config export (2/2)."
/tool fetch address="$ftphost" src-path=$fname2 user="$ftpuser" mode=ftp password="$ftppassword" dst-path="$ftppath/$fname2" upload=yes

delay time to finish the upload - increase it if your backup file is big

:delay 30s;

find file name start with Mikrotik- then remove

:foreach i in=[/file find] do={ :if ([:typeof [:find [/file get $i name] "Mikrotik-"]]!="nil") do={/file remove $i}; }
:log info message="Configuration backup finished.";

The error is:

(scheduler:FTP_BACKUP-8) invalid file name (:export; line 27)

Try to remove / from "/Mikrotik-" in your fname1 and fname2 variables assignments.

file name for system backup - file name will be Mikrotik-servername-date-time.backup

:local fname1 ("Mikrotik-".[/system identity get name]."-".$ds."-".$ts.".backup")

file name for config export - file name will be Mikrotik-servername-date-time.rsc

:local fname2 ("Mikrotik-".[/system identity get name]."-".$ds."-".$ts.".rsc")

doesn't help

invalid file name (:export; line 27)

I copied your script up until :log info message="Config export finished (2/2).", with / removed from the file names, and the export worked. Here, before and after removing /:

So the problem on your device might be due to some invalid characters in the system identity. Try to insert a

:log warning $fname2

line before the # backup the data line, to see in the log what the variable really contains.


Thanks a lot

The classic naming mistake...

This applies to any brand.

When naming a device,
never use characters that are reserved on the operating system filenames...

Then there's also the error of the months...

And then the rest of the script seems to have been written by a more drunken than usual Artificial Deficiency...


Simply this suffice:

{
:local ftphost "host"
:local ftpuser "user"
:local ftppass "password"
# NOTICE: NO / at the end
:local ftppath "/MTK"

:local ds [/system clock get date]
:local ts [/system clock get time]
# NOTICE: Script will NOT work if the RouterOS identity has unusable reserved characters in the file name
:local si [/system identity get name]
# NOTICE: NO / at the begin
:local fn "MikroTik-$si-$[:pick $ds 0 4]$[:pick $ds 5 7]$[:pick $ds 8 10]-$[:pick $ts 0 2]$[:pick $ts 3 5]$[:pick $ts 6 8]"

/system backup save name="$fn.backup"
/export show-sensitive file="$fn.rsc"

/tool fetch mode=ftp upload=yes address=$ftphost user=$ftpuser password=$ftppass src-path="$fn.backup" dst-path="$ftppath/$fn.backup" 

/tool fetch mode=ftp upload=yes address=$ftphost user=$ftpuser password=$ftppass src-path="$fn.rsc" dst-path="$ftppath/$fn.rsc" 

/file remove [find where name~$fn]
}

I think it's a terrible idea to delete Backup and .rsc from the machine. At most, I'd delete the old ones after leaving the new ones...

(last line become /file remove [find where !(name~$fn) and (name~".rsc\$" or name~".backup\$")])

Alternative script: GitHub - drpioneer/MikrotikBackup: Backup script for settings and device log with automatic storage location detection · GitHub

(Thank you for linking to my posts for the original scripts.)

That script used on v7.2x+ is outdated now...
RouterOS v7 has implemented a lot of features that greatly simplify the script...

This script is good for its versatility. It works on both old and new versions of RoterOS.