I tried to execute the following script to get a backup and .rsc file (with name + date + time) but it gave me errors:
:log info “STARTING BACKUP” :global backupfile ([/system identity get name] . “-” . [/system clock get date] . “-” . [/system clock get time]) /system backup save name=$backupfile
:log info “DELAY 3S” :delay 3s
:log info “GENERATING RSC” :global rsc ([/system identity get name] . “-” . [/system clock get date] . “-” . [/system clock get time]) /export file=$rsc
:log info “BACKUP FINISHED”
It gives me:
#[in backup section] Saving system configuration Failed to save system configuration backup action failed (6)
#[in rsc section] file operation error
I don’t know, may be system doesn’t accept some special character.
I was not able to copy RB-16:42:00.rsc to my local computer. I suspect it could be the colons in the name.
How do I can change the colon to dash, for example, via script?
Is there a solution to these errors?
The problem occurs at line 3, when you try to give a name containing a backslash (in date format) and a column (in time format).
Your script produce a name like “RouterName-aug/02**/2016-09:53:**08.backup”. Underlined characters are not allowed for almost every OS not only ROS.
So, you have to make a few changes in order to remove these characters
I’ve changed a bit your example (all local to global).
Now the BACKUP and RSC were sucessfully created.
Can I have problems using global instead of local?
:log info "STARTING BACKUP";
:global filename;
:global date [/system clock get date];
:global time [/system clock get time];
:global name [/system identity get name];
:global months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
:global hour [:pick $time 0 2];
:global min [:pick $time 3 5];
:global sec [:pick $time 6 8];
:global month [:pick $date 0 3];
:set month ([ :find $months $month -1 ] + 1);
:if ($month < 10) do={ :set month ("0" . $month); }
:global day [:pick $date 4 6];
:global year [:pick $date 7 11];
:set filename ($name. "-" .$year."-".$month."-".$day."-".$hour.$min.$sec);
/system backup save name=$filename;
:log info "DELAY 3S"
:delay 3s;
:log info "GENERATING RSC";
:global rsc $filename;
/export file=$rsc;
:log info "BACKUP FINISHED"
I am not an expert on programming, but i think that a global variable consume more resources.
Because a local’s variable value discarded from memory when script execution ends but a global’s variable value is not discarded from memory and you can use it later from the same or another script.
Maybe someone finds the below useful - a modification of known script I use to send both attachments in a single email to my Gmail account.
Please notice you need ROS version at least 6.40 to be able to attach multiple files to a single email.
#### Modify these values to match your requirements ####
#Your email address to receive the backups
:local toemail "nice.guy@gmail.com"
#The From address (you can use your own address if you want)
:local fromemail "MikrotikBackup"
#A mail server your machines can send through
:local emailserver "smtp.gmail.com"
############## Don’t edit below this line ##############
:local textfilename
:local backupfilename
:local allfiles
:local filedate
:local date [/system clock get date];
:local time [/system clock get time];
:local sysname [/system identity get name];
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
:local varMonth [:pick $date 0 3];
:set varMonth ([ :find $months $varMonth -1 ] + 1);
:if ($varMonth < 10) do={ :set varMonth ("0" . $varMonth); }
:local varDay [:pick $date 4 6];
:local varYear [:pick $date 7 11];
:set filedate ($varYear."-".$varMonth."-".$varDay);
:set textfilename ($"filedate" . "-" . $"sysname" . ".rsc")
:set backupfilename ($"filedate" . "-" . $"sysname" . ".backup")
:set allfiles ($"textfilename" . "," . $"backupfilename")
:execute [/export file=$"textfilename"]
:execute [/system backup save name=$"backupfilename"]
#Allow time for export to complete
:delay 2s
#email copies
:log info "Emailing backups"
/tool e-mail send to=$"toemail" from=$"fromemail" server=[:resolve $emailserver] port=587 subject="[Config Backup] $sysname" file=$"allfiles"
#Allow time to send
:delay 10s
#delete copies
/file remove $textfilename
/file remove $backupfilename
And of course you’d need to set your settings for /tool e-mail service, in my case I use similar to