Page 1 of 1

Generate Backup and RSC based on Router Name + Date + Time

Posted: Mon Aug 01, 2016 11:46 pm
by tomasi
Hi, 

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?

:?

Re: Generate Backup and RSC based on Router Name + Date + Time

Posted: Tue Aug 02, 2016 10:10 am
by stam
hello Tomasi and welcome aboard,

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


:log info "STARTING BACKUP";

:local filename;

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

:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
:local varHour [:pick $time 0 2];
:local varMin [:pick $time 3 5];
:local varSec [:pick $time 6 8];
: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 filename ($name. "-" .$varYear."-".$varMonth."-".$varDay."-".$varHour.$varMin.$varSec);



/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";
RouterName-2016-08-02-095308.backup 
RouterName-2016-08-02-095308.rsc

Re: Generate Backup and RSC based on Router Name + Date + Time

Posted: Tue Aug 02, 2016 4:02 pm
by tomasi
Thank you for your reply :)

I tested the new script in my RB2011 6.34.4, but it gave me some errors. Another issue was it did not generate the RSC.

Some idea about this?

Image

:?

Re: Generate Backup and RSC based on Router Name + Date + Time

Posted: Tue Aug 02, 2016 9:26 pm
by tomasi
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"

:D

Re: Generate Backup and RSC based on Router Name + Date + Time

Posted: Fri Aug 05, 2016 11:20 am
by stam
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.

Re: Generate Backup and RSC based on Router Name + Date + Time

Posted: Thu Nov 02, 2017 1:59 am
by marlab
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
       address: smtp.gmail.com
          port: 587
     start-tls: yes
          from: <nice.guy@gmail.com>
          user: nice.guy@gmail.com
      password: yourgmailpassword

Re: Generate Backup and RSC based on Router Name + Date + Time

Posted: Fri Nov 10, 2017 11:48 am
by EMSebi
HI
I created a script to create daily backup and send it to Server FTP

Copy this to your router terminal
# Create Script For Add Backup every day with name and date
# Change Or Set Router Identity Unique name 
# Generate By Iran FCP

/system scheduler
add comment="--==  Create Backup By Date ==--" interval=1d name=Backup-Day \
    on-event=":local day ([:pick [/system clock get date] 4 6]);\r\
    \n:local mon ([:pick [/system clock get date] 0 3]);\r\
    \n:local yer ([:pick [/system clock get date] 7 11]);\r\
    \n:local date \"\$day-\$mon-\$yer\";\r\
    \n:local iden [/system identity get name];\r\
    \n:local name \"\$date_{\$iden}\";\r\
    \n\r\
    \nexport compact file= \$name\r\
    \nsystem backup save dont-encrypt=yes name= \$name" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=may/21/2017 start-time=23:20:00
add comment="--== SEND Backup -- >  FTP ==--" interval=1d name=Send-BK-TO-FTP \
    on-event=":local day ([:pick [/system clock get date] 4 6]);\r\
    \n:local mon ([:pick [/system clock get date] 0 3]);\r\
    \n:local yer ([:pick [/system clock get date] 7 11]);\r\
    \n:local date \"\$day-\$mon-\$yer\";\r\
    \n:local iden [/system identity get name];\r\
    \n:local name \"\$date_{\$iden}\";\r\
    \n\r\
    \n:local rec1 \"\$name.rsc\";\r\
    \n:local rec2 \"Backup/\$name.rsc\";\r\
    \n\r\
    \n:local bk1 \"\$name.backup\";\r\
    \n:local bk2 \"Backup/\$name.backup\";\r\
    \n\r\
    \n/tool fetch address=xxx.xxx.xxx.xxx src-path= \$rec1 user=UserFTP mode=ftp pas\
    sword=PasFTP dst-path= \$rec2 upload=yes\r\
    \n/tool fetch address=xxx.xxx.xxx.xxx src-path= \$bk1 user=UserFTP mode=ftp pass\
    word=PasFTP dst-path= \$bk2 upload=yes\r\
    \n\r\
    \n\r\
    \n# Change xxx.xxx.xxx.xxx To Ip FTP Server  And UserFTP - PasFTP\r\
    \n# Create Backup Folder To FTP Server \r\
    \n# EMSebi@Gmail.Com\r\
    \n# My Tel & Telegram : 00989195999975   @EMSebi" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=may/21/2017 start-time=23:30:00