MikroTik Scripts: Create backup and send to email

The script creates a backup file “backup_file.backup” and sends it to an email address using the SendEmailFunction function call.

Specify the parameters: SendTo, Subject, MessageText, FileName and Password, for correct sending of the letter.

Exemple d'e-mail reçu d'un appareil MikroTik avec un fichier de sauvegarde
Example email received from MikroTik device with backup file

Article in other languages:
🇪🇸 – MikroTik Scripts: Cree una copia de seguridad y envía por correo electrónico
🇷🇺 – MikroTik Скрипты: Создать бэкап и отправить на почту
🇫🇷 – Scripts MikroTik: Créer une sauvegarde et envoyer par e-mail
🇩🇪 – MikroTik-Skripte: Erstellen Sie ein Backup und senden Sie es per E-Mail

RouterOS backup feature allows you to save your current device’s configuration, which then can be re-applied on the same or a different device (with the same model name/number). This is very useful since it allows you to effortlessly restore device’s configurations or to re-apply the same configuration on a backup device. System’s backup file also contain the device’s MAC addresses, which are also restored when the backup file is loaded.

wiki.mikrotik.com

Script: Create backup and send to mail

[System] -> [Scripts] -> [+] -> [Name: CreateBackupAndSendEmail] -> [Policy: read, write, policy, test, sensitive]

Script code:

#Variables
:local DeviceName [/system identity get name];
:local Date [/system clock get date];

:local SendTo "destination_mail@mail.com";
:local Subject "\F0\9F\93\A6 BACKUP: $DeviceName [$Date]";
:local MessageText "$DeviceName - device backup file ";
:local FileName "backup_file.backup";
:local Password "Pa$$Word";

# Main script code
/system backup save name=$FileName password=$Password
:local SendMail [:parse [/system script get SendEmailFunction source]];
$SendMail SendTo=$SendTo TextMail=$MessageText Subject=$Subject FileName=$FileName;
Script: Create backup and send to mail

⚠️ If you do not specify the Password parameter, the backup file will not be encrypted. All data, including script data (Telegram token, mail password) will be available.

If, when executing the script, you receive the error input does not match any value of file – the file name contains service characters, change the file name or escape MikroTik service characters with the “\” character.

Add script launch to Scheduler

For me, it is enough if the devices make a backup copy of the settings once a week.

For the script to work, you must set the following permissions: read, write, policy, test, sensitive.

[System] -> [Schedule] -> [+] -> [Name: CreateBackupAndSendEmail] - > [Interval: 7d 00:00:00] -> [Policy: read, write, policy, test, sensitive]

Script code:

/system script run CreateBackupAndSendEmail
Add script launch to Scheduler

🟢 The script for creating backup and sending backup file by email was discussed in this article. I hope this article helped automate your work on creating backups of MikrotTik devices. However, if you run into any problems while setting up, feel free to write in the comments. I will try to help.

Leave a Reply

Your email address will not be published. Required fields are marked *