Author Archives: CooLMinE

Automatically check for MikroTik software updates

If you own multiple MikroTik devices (or even one) you already know how annoying and time consuming it is to keep checking if there are new updates available for your devices all the time.

The purpose of this tutorial is to help you setup your devices in order to receive an email notification if a new software update is available to download for your device.

Step 1

First of all, we need to setup the SMTP settings of your MikroTik device to allow it to send emails when required.

Using WinBox, or the web interface, navigate to Tools --> Email and change your settings as shown in the image below. Please note that the image contains sample data and requires from you to change the settings with a working SMTP as well the correct credentials, if required.

mikrotik smtp settings

If you prefer to use the CLI over the web interface or WinBox, you can use the command below:

Make sure to send a test email to verify that everything is working properly.

Step 2

Now that our MikroTik device is able to send emails it is time to create our script which will be responsible for checking for new updates and sending us an email if one is available.

Navigate to System --> Scripts, click Add, change the title to “Check for software updates” or to something else if you prefer (don’t forget the name, you will need it in step 3) and finally, paste the following script in the source area:

CLI Version (pay attention to the highlighted line)

Step 3

It is time for the last step. In order for everything to work properly, we need to trigger our script every now and then in order for the update check to take place. To accomplish this, we will use the scheduler feature which is located under System --> Scheduler

Simply add a new scheduled task, give it a name and specify how often it should run in the interval section (I run mine with a 2d 00:00:00 interval).

In the “On Event” section paste the line below.

Important: Make sure the name of the script is exactly the same as the one you have created in step 2!

CLI Version

And you are done! If you have successfully followed this example your MikroTik device will check every 2 days for new updates and will let you know by email if one is available.

This method has been tested in the last few versions of RouterOS (currently 6.38). For older versions you might need to alter the script functions a bit to get everything working correctly.

If you have any questions don’t hesitate to ask them in the comment section below.

Using PowerShell to backup your MySQL databases

A few months ago we had to find a way to backup our MySQL databases, both remotely and locally. Instead of trying to find a software that did something similar I decided to write a few lines of code that solved our problem. Why spend time and money evaluating software when can create your own solution to the problem with little to no cost, right ?

The snippet below is fairly simple. The $databases variable holds an array of all the databases you want to dump. You will need to specify the database name, the path you wish to save the backup and the backup filename. I took the liberty of commenting the code in order to make it a bit more clear.

Please keep in mind that this solution uses mysqldump. This executable is usually located under C:\Program Files\MySQL\MySQL Server 5.6\bin, assuming you have MySQL server installed. In addition to mysqldump, since all our Windows servers we have WinRAR installed, this snippet uses WinRAR in order to compress the database files. You can always modify the code either skip the compression or to use .NET’s ZipFile class or 7zip.

This solution still needs some tweaking. For example, creating a temporary file with the passwords and passing it as an argument to mysqldump to avoid the “Specifying a password on the command line should be considered insecure” message, as well as some code cleanup and error catching. Until then, you are free to modify this example to better suit your needs.

If you have any questions feel free to post them in the comment section below.

Create a MySQL table dynamically in C#

Sometimes you might need to create database tables dynamically, based on new users, products etc. The snippet below allows you to do exactly that, create a new table in your database dynamically, straight from your code.

This example is heavily based on How to connect to a MySQL database and retrieve data in C# as the same concept of connecting to the database is required.

The first thing you need to do, as mentioned in the article linked above, is that you will need to add a reference to MySQL.Data in your project. After you have done that, you can modify the snippet below to create the table with the columns you want based on your requirements.

If you are unsure as to which database engine to use, please take a look at https://dev.mysql.com/doc/refman/5.0/en/storage-engines.html as every database engine has different type of limitations, so it is better to choose the one that better suits your needs.

How to connect to a MySQL database and retrieve data in C#

Connecting to a MySQL database is fairly simple and straight forward. The first thing you will need to do is to add a reference to MySQL.Data in your project.

This can be done in a few ways:

  • Right click on references in your project -> click Manage NuGet packages -> Search and install MySQL.Data from Oracle
  • Open Package Manager Console from Tools -> NuGet Package Manager -> Paste in the console the following line: Install-Package MySql.Data
  • If you have installed the .NET Connector from MySQL you can navigate to where you have installed the connector and add a reference to the MySQL.Data.dll file

After you have added a reference to MySQL.Data you can tailor the snippet below to suit your requirements.

If you are interested in other type of databases you can check my Microsoft Access Database and SQLite database tutorials.

Add www in-front of your domain using IIS

If you are using IIS you can use the snippet below in your web.config to add www. in-front of your domain if it is not already present.

Please keep in mind that to use the snippet below you will need to have URL Rewrite module installed and enabled.