Skip to content

Instantly share code, notes, and snippets.

@elico
Created June 14, 2024 13:35
Show Gist options
  • Save elico/f7828a83678f521f8766ffec015d565f to your computer and use it in GitHub Desktop.
Save elico/f7828a83678f521f8766ffec015d565f to your computer and use it in GitHub Desktop.
a simple backup script for RouterOS V7.x
{
:do {
:log info "STARTING BACKUP";
:global customerIdentificationVar;
:local customerIdentification "{{ customer_identifictation }}";
if ( [:len $customerIdentificationVar] > 0 ) do={
:set customerIdentification "$customerIdentificationVar";
}
: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 probably not a RouterBoard Hardware: " . [[ :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 tlsOnly "{{ tls_only }}";
:local mailServer "{{ mail_server }}";
:local fromLine "{{ from_line ))";
:local mailServerSubmissioPort {{ mail_server_submission_port }};
:local emailUsername "{{ email_username }}";
:local emailPassword "{{ email_password }}";
:local recipient;
:local runtimeEnvironment;
:set runtimeEnvironment "{{ runtime_environment }}";
:local runtimeEnvironmentMatch false;
:if ( $runtimeEnvironment = "lab" ) do={
:set recipient "{{ lab_email }}";
:set runtimeEnvironmentMatch true;
}
:if ( $runtimeEnvironment = "prod" ) do={
:set recipient "{{ production_email }}";
:set runtimeEnvironmentMatch true;
}
if ( $runtimeEnvironmentMatch = false ) do={
:log info "Unknown environment -> $runtimeEnvironment , stopping the backup process";
/break
}
:local tlsOption;
if ( $tlsOnly = true ) do={
:set tlsOption "starttls";
} else={
:set tlsOption "yes";
}
: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=$tlsOption 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=$tlsOption user=$emailUsername password=$emailPassword";
};
:do {
:log info "Running the command: [ $setMailConfigCmd ]";
:execute $setMailConfigCmd;
:delay 1s;
:log info "Ran the command: [ $setMailConfigCmd ] Succesfully";
} on-error={
:log info "Error running the command: [ $setMailConfigCmd ]";
}
}
:log info "GENERATING backup file";
:local backupfile ($customerIdentification . "_" . $boardSerial . ".backup");
/system/backup/save name=$backupfile dont-encrypt=yes;
:delay 3s;
:if ($sendMail = true) do={
:local subject "$customerIdentification - $boardSerial - backup file";
/tool/e-mail/send from="$emailUsername" to="$recipient" subject="$subject" file="$backupfile";
/tool/e-mail/send from="$emailUsername" to="$recipient" subject="$subject"
:delay 3s;
}
:log info "GENERATING RSCs";
:log info "GENERATING generic export RSC";
:local rscGeneric ($customerIdentification . "_" . $boardSerial . "_generic.rsc");
/export file=$rscGeneric show-sensitive;
:delay 3s;
:if ($sendMail = true) do={
:local subject "$customerIdentification - $boardSerial - rscGeneric backup file";
/tool/e-mail/send from="$emailUsername" to="$recipient" subject="$subject" file="$rscGeneric";
:delay 3s;
}
:log info "GENERATING verobse export RSC";
:local rscVerbose ($customerIdentification . "_" . $boardSerial . "_verbose.rsc");
/export file=$rscVerbose verbose show-sensitive;
:delay 3s;
:if ($sendMail = true) do={
:local subject "$customerIdentification - $boardSerial - rscVerbose backup file";
/tool/e-mail/send from="$emailUsername" to="$recipient" subject="$subject" file="$rscVerbose";
:delay 3s;
}
:log info "GENERATING terse export RSC";
:local rscTerse ($customerIdentification . "_" . $boardSerial . "_terse.rsc");
/export file=$rscTerse terse show-sensitive;
:delay 3s;
:if ($sendMail = true) do={
:local subject "$customerIdentification - $boardSerial - rscTerse backup file";
/tool/e-mail/send from="$emailUsername" to="$recipient" subject="$subject" file="$rscTerse";
:delay 3s;
}
:log info "GENERATING compact export RSC";
:local rscCompact ($customerIdentification . "_" . $boardSerial . "_compact.rsc");
/export file=$rscCompact compact show-sensitive;
:delay 3s;
:if ($sendMail = true) do={
:local subject "$customerIdentification - $boardSerial - rscCompact backup file";
/tool/e-mail/send from="$emailUsername" to="$recipient" subject="$subject" file="$rscCompact";
:delay 3s;
}
:log info "GENERATING supout.rif";
:local supoutRif ($customerIdentification . "_" . $boardSerial . "_suptout.rif");
/system/sup-output name=$supoutRif;
:log info "finished GENERATING supout.rif";
:delay 3;
:if ($sendMail = true) do={
:local subject "$customerIdentification - $boardSerial - supout.rif file";
/tool/e-mail/send from="$emailUsername" to="$recipient" subject="$subject" file="$supoutRif";
:delay 10s;
}
:log info "BACKUP Process FINISHED, the email might take to to sent to the recpient";
} on-error={
:log info "BACKUP Error [:jobname]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment