Skip to content

Instantly share code, notes, and snippets.

@elico
Created December 28, 2024 10:25
Show Gist options
  • Save elico/42e86ec8b2d143a95350d953f4277944 to your computer and use it in GitHub Desktop.
Save elico/42e86ec8b2d143a95350d953f4277944 to your computer and use it in GitHub Desktop.
{
:do {
:log info "STARTING BACKUP";
:local customerId [system/identity/get name ];
:local boardId;
:local boardSerial;
:do {
:set boardId [[ :parse "/system/routerboard/get" ]];
} on-error={
:log info "error getting routerboard serial";
:set boardId "no";
};
:if ( $boardId = "no" ) do={
:log info ( "The device is a CHR: " . [[ :parse "/system/license/get system-id" ]] );
:set boardSerial [[ :parse "/system/license/get system-id" ]];
} else={
:log info ( "The device is a RouterBoard:" . $boardId->"serial-number" );
:set boardSerial ( $boardId->"serial-number" );
}
:local sendMail true;
:local mailServer "smtp.gmail.com";
:local fromLine "Mikrotik Router";
:local mailServerSubmissioPort 587;
:local emailUsername "[email protected]";
:local emailPassword "secret";
:local recipient;
:local runtimeEnvironment;
:set runtimeEnvironment "lab";
:local recipients { "[email protected]"; "[email protected]"};
:log info "Configuring email settings";
:if ($sendMail = true) do={
:local setMailConfigCmd;
:do {
:local emailServerAdress;
:set emailServerAdress [[ :parse "/tool/e-mail/get server" ]];
:set setMailConfigCmd "/tool/e-mail set server=\"$mailServer\" from=\"$fromLine\" port=$mailServerSubmissioPort tls=starttls user=\"$emailUsername\" password=\"$emailPassword\"";
} on-error={
:log info "Couldnt get email server, probably RouterOS below v7.14";
:set setMailConfigCmd "/tool/e-mail set address=\"$mailServer\" from=\"$fromLine\" port=$mailServerSubmissioPort tls=starttls user=\"$emailUsername\" password=\"$emailPassword\"";
};
:do {
:log info "Running the command: [ $setMailConfigCmd ]";
:execute $setMailConfigCmd;
:log info "Ran the command: [ $setMailConfigCmd ] Succesfully";
} on-error={
:log info "Error running the command: [ $setMailConfigCmd ]";
}
}
:log info "GENERATING backup file";
:local backupfile ($customerId . "_" . $boardSerial . ".backup");
/system/backup/save name="$backupfile" dont-encrypt=yes;
:delay 3s;
:if ($sendMail = true) do={
:foreach emailAddress in=$recipients do={
:local subject "$customerId - $boardSerial - backup file";
/tool/e-mail/send from="$emailUsername" to="$emailAddress" subject="$subject" file="$backupfile";
:delay 3s;
}
}
:log info "GENERATING RSCs";
:log info "GENERATING generic export RSC";
:local rscGeneric ($customerId . "_" . $boardSerial . "_generic.rsc");
/export file=$rscGeneric show-sensitive;
:delay 3s;
:if ($sendMail = true) do={
:foreach emailAddress in=$recipients do={
:local subject "$customerId - $boardSerial - rscGeneric backup file";
/tool/e-mail/send from="$emailUsername" to="$emailAddress" subject="$subject" file="$rscGeneric";
:delay 3s;
}
}
:log info "GENERATING verobse export RSC";
:local rscVerbose ($customerId . "_" . $boardSerial . "_verbose.rsc");
/export file=$rscVerbose verbose show-sensitive;
:delay 3s;
:if ($sendMail = true) do={
:foreach emailAddress in=$recipients do={
:local subject "$customerId - $boardSerial - rscVerbose backup file";
/tool/e-mail/send from="$emailUsername" to="$emailAddress" subject="$subject" file="$rscVerbose";
:delay 3s;
}
}
:log info "GENERATING terse export RSC";
:local rscTerse ($customerId . "_" . $boardSerial . "_terse.rsc");
/export file=$rscTerse terse show-sensitive;
:delay 3s;
:if ($sendMail = true) do={
:foreach emailAddress in=$recipients do={
:local subject "$customerId - $boardSerial - rscTerse backup file";
/tool/e-mail/send from="$emailUsername" to="$emailAddress" subject="$subject" file="$rscTerse";
:delay 3s;
}
}
:log info "GENERATING compact export RSC";
:local rscCompact ($customerId . "_" . $boardSerial . "_compact.rsc");
/export file=$rscCompact compact show-sensitive;
:delay 3s;
:if ($sendMail = true) do={
:foreach emailAddress in=$recipients do={
:local subject "$customerId - $boardSerial - rscCompact backup file";
/tool/e-mail/send from="$emailUsername" to="$emailAddress" subject="$subject" file="$rscCompact";
:delay 3s;
}
}
:log info "GENERATING supout.rif";
:local supoutRif ($customerId . "_" . $boardSerial . "_suptout.rif");
/system/sup-output name=$supoutRif;
:log info "finished GENERATING supout.rif";
:delay 3;
:if ($sendMail = true) do={
:foreach emailAddress in=$recipients do={
:local subject "$customerId - $boardSerial - supout.rif file";
/tool/e-mail/send from="$emailUsername" to="$emailAddress" subject="$subject" file="$supoutRif";
:delay 15s;
}
}
:log info "BACKUP FINISHED";
} on-error={
:log info "BACKUP Error";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment