Mikrotik automated maintenance

Let’s look a bit at one of Mikrotik routers most powerful capability: scripting. Using scripts we can automate updating and backups, so we can sleep a bit easier. We also can send the backups directly via email, so I will assume that you have a (spare) email address from where to send notifications.

Email setup

Log in to your router with winbox:

01

Go to Tools -> Email:

02

The Email Settings panel should appear:

03

Enter your email servers Address and Port. Set the appropriate encryption, make sure to use the same From and User names and enter the password as well:

04

Open a terminal by clicking New Terminal:

05

Type the following command to test if everything is setup properly and hit enter:

/tool e-mail send to="backoffice@domain.tld" subject="test"

06

Backup script

If you received the test e-mail, than we can move to the next step. Go to System -> Scripts:

07

The Script List panel will appear. Click on the blue plus icon to add a new script:

08

The New Script panel should appear:

09

Input backup for Name and paste the following into the Source text-box, leave everything else as is adn hit OK:


:log info "backup beginning now"
:global backupfile [/system identity get name]
/system backup save name=$backupfile
:delay 10s
/export file=$backupfile compact
:delay 10s
:log info "backup being emailed"
/tool e-mail send to="backoffice@domain.tld" subject=("Mikrotik Backup of " . [/system identity get name]) file=($backupfile . ".backup")
:delay 10s
/tool e-mail send to="backoffice@domain.tld" subject=("Mikrotik Export Compact of " . [/system identity get name]) file=($backupfile . ".rsc")
:log info "backup finished"

10

You now should have a script named backup, just like in the following screenshot:

11

Now let’s see if it works. Open another terminal (by clicking New Terminal):

12

Run the following to test the script:

/system script run backup

13

You should receive two emails. One containing a binary backup and the other containing the compact configuration file of the router.

NOTE: As of writing this article RouterOS doesn’t allow to attach two or more files to a single email.

Update script

If everything wen’t fine let’s move on; close the terminal window and go back to the Script List panel. Create another script (with the blue plus button):

14

We gonna Name this one update, leave everything as is and enter the following in the Source text-box:


/system package update
check-for-updates once
:delay 5s;
:if ( [get status] = "New version is available") do={ install }

15

After pressing OK we now should have two scripts:

16

Let us test the second one as well, by running:

/system script run update

NOTE: If there is an update pending the router will reboot after applying it, that could lead to a few minutes of downtime!

17

Here’s a screenshot with the update in progress:

18

Scheduling

Now we need to schedule our two scripts. To do that go to System -> Scheduler:

19

The Scheduler panel will appear, click on the blue plus to add a new schedule:

20

The New Schedule panel pops up:

21

Enter maintenance as Name, set the Start Date and the Start Time to a day / time you want to run the scripts, in my case it is every Sunday at 5 AM. Set the Interval to 7 days to run it weekly, leave everything else as is and paste the following into the On Event text-box:


/system script run backup
/system script run update

22

After hitting OK you should see your schedule:

23

Schedule depends on proper timekeeping so let’s make sure that the routers clock is accurate. To do that, let’s go to System -> Clock: and set the date, clock and timezone manually first:

24

After that, we can set up syncing. Go to System -> SNTP Client:

25

The SNTP Client panel will appear:

26

Check Enabled and enter two NTP Servers. If you prefer to use public NTP servers by host-name (here’s a helpful list), than be sure to enter at-least two public DNS servers as well. In my case I will use the local ADDS servers:

27

After hitting Apply (or OK and reopening the panel) we can see that it updated correctly:

28

Finally let’s give our router a proper name, since we use that in our backup scripts. Go to System -> Identity:

29

Enter a name into the Identity field and hit OK to save it:

30

That’s it!

Leave a Reply

Your email address will not be published.