Community discussions

MikroTik App
 
guipoletto
Member Candidate
Member Candidate
Topic Author
Posts: 195
Joined: Mon Sep 19, 2011 5:31 am

Yet Another Backup Script (with bugs! \0/)

Wed Mar 17, 2021 5:04 am

I have implemented a small and precarious daily backup script, as the existing ones i found are always based on e-mail, and i need to drop-off files via FTP.

I have some problems tough

the script always runs if I manually run from the CLI (/sys script run DailyBackup), and presents debugging info in the screen.
if run from "run script" button, or scheduler, it will often crash and never finish. I suspect I should have to escape the output somehow, pipe it to log?

The Backup file is always generated, sometimes with an "could not read all configuration files" warning, but that is typical behavior, AFAIK.
The RSC file on the other hand, is always truncated at ~~4Kb, terminating in the /system scripts section.

#
#variables to know to delete yesterdays files
#
:global BKNAME
:global OLDBKNAME
/log info message="======script===backup===INIT=========="
#
:local HOST [/sys identity get name]
:local PLACA [/sys reso get board-name]
:local date [/sys clock get date]
:local time [/sys clock get time]
:local DD [:pick $date 4 6 ]
:local MES [:pick $date 0 3 ]
:local AAAA [:pick $date 7 11 ]
:local HH [:pick $time 0 2]
:local MM [:pick $time 3 5]
:local NOMEBK "$HOST-$PLACA-$AAAA-$MES-$DD-$HH$MM"
/log info message="Gerando backup diario"
/log info message="$NOMEBK"
:global OLDBKNAME value=$BKNAME
:global BKNAME "$NOMEBK"
/delay 3;
/log info message="Salvando Backup"
/system backup save name="diario/$NOMEBK" dont-encrypt=yes;
/log info message="Backup Salvo"
/delay 3;
/log info message="Salvando Export"
/system export file="diario/$NOMEBK"
/log info message="Export Salvo"
/delay 3;
/log info message="Removendo BACKUP anterior"
file remove [find where name="diario/$OLDBKNAME.backup"]
/log info message="BACKUP anterior removido"
/delay 3;
/log info message="Removendo RSC anterior"
file remove [find where name="diario/$OLDBKNAME.rsc"]
/log info message="RSC anterior removido"
/delay 3;
/log info message="Enviando RSC via FTP"
/tool fetch upload=yes mode=ftp address=192.168.168.168 user=xxxzzz password=xxxzzz dst-path="/SSD/diario/$BKNAME.rsc" src-path="diario/$BKNAME.rsc"
/log info message="RSC enviado"
/delay 3;
/log info message="Enviando BACKUP via FTP"
##
#remote file uses .bak as mikrotik permissions for drop-only do not like .backup format
##
/tool fetch upload=yes mode=ftp address=192.168.168.168 user=xxxzzz password=xxxzzz dst-path="/SSD/diario/$BKNAME.bak" src-path="diario/$BKNAME.backup"
/log info message="BACKUP enviado"
/delay 3;
/log info message="Gerando CloudBackup"
/system backup cloud upload-file action=create-and-upload replace=[/system backup cloud get value-name=name number=0] password="correct-horse-battery-staple"
/log info message="CloudBackup Salvo"
/delay 3;
#
#
/log info message="======script===backup===END=========="
#


Last edited by guipoletto on Wed Mar 17, 2021 12:47 pm, edited 2 times in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Yet Another Backup Script (with bugs! \0/)

Wed Mar 17, 2021 9:10 am

Since you have lot log messages, what is the latest log message when your script fails?
If I have multiple devices, I like to add serial number to the file name.
:put [/system routerboard get serial-number]
Last edited by Jotne on Wed Mar 17, 2021 5:20 pm, edited 1 time in total.
 
guipoletto
Member Candidate
Member Candidate
Topic Author
Posts: 195
Joined: Mon Sep 19, 2011 5:31 am

Re: Yet Another Backup Script (with bugs! \0/)

Wed Mar 17, 2021 12:46 pm

I thought about including the serial, but the filenames are already big as they are.
Furthermore, SN can be found in the .rsc/Export, if i ever manage to fix the 4Kb limitation.

The script tends to halt during the Cloud backup phase.


=== edit ===

[:export file="diario/$NOMEBK"]

As i understand, by using this version of the command between brackets, i'm directing the system to execute the command instead of the shell running the script, therefore escaping the 4kb file write limit.
Is this the correct way of executing these commands? should i rewrite the other ones in a similar fashion?

Who is online

Users browsing this forum: No registered users and 31 guests