modified on 20 February 2012 at 16:46 ••• 58,807 views

Automated Backups

From MikroTik Wiki

Jump to: navigation, search

Contents

E-mail backups

One of the easiest (and simplest) methods of automating Mikrotik backups is by using the built-in Mikrotik e-mail system. Setting it up consists of only a few commands and utilizes the scheduler to execute the e-mail script at any given time. Before continuing on implementing the e-mail script, it is necessary to make sure that the e-mail system is properly configured to dispatch e-mail messages. You can do this by typing the following commands in the MT console:

/tool e-mail print

You should receive output similar to this:

 server: 192.168.0.1
 from: mikrotikbackup@yourhost.com

…where “192.168.0.1” is your SMTP server, and mikrotikbackup@yourhost.com the e-mail address that MT will use to identify itself when sending out backups. If you do not see a similar output, but only blank fields instead, you need to configure your e-mail settings using the following commands:

/tool e-mail
set server=192.168.0.1 from=mikrotikbackup@yourhost.com

Now it’s time to insert the needed e-mailing script. The following command should do the job:

/tool e-mail send to="youremail@yourhost.com" subject=([/system identity get name]." backup") file=email.backup;
:log info "Backup e-mail sent."; }

Replace youremail@yourhost.com with the recipient e-mail address. For multiple e-mail addresses, use the following command (and edit it accordingly):

/system script
add name="e-mail-backup" source={/system backup save name=email;
/tool e-mail send to="youremail1@yourhost.com" subject=([/system identity get name]." backup") file=email.backup;
/tool e-mail send to="youremail2@yourhost.com" subject=([/system identity get name]." backup") file=email.backup;
/tool e-mail send to="youremail3@yourhost.com" subject=([/system identity get name]." Backup") file=email.backup; 
:log info "Backup e-mail sent.";  }

Note the last character in the command line ( “}” ), make sure that it’s always present so that the script text can be closed properly. The last step is to configure the scheduler to execute the above script at a given time interval. The following command makes sure that the script is executed every day:

/system scheduler 
add comment="" disabled=no interval=1d name="e-mail-backup" \
on-event=e-mail-backup start-date=jan/01/1970 start-time=00:00:00

When done, you can review your settings using WinBox, and the finished solution should look like this:

Script list - final look
Scheduler - final look

Automated central backups

Introduction

While e-mail backups are simple to realize, they do eventually become a hassle by conflicting with spam filters and by filling up the recipient’s e-mail inbox fairly quickly (especially if multiple routers are e-mailing their routine backups in short time intervals) thus making the whole backup procedure seem unorganized, slow and clumsy. Using the built in FTP server in Mikrotik and the built in FTP client in Microsoft Windows (along with some handy utilities), a much more sophisticated backup system can be created which can be accessed and used much more quickly and efficiently. The following solution can be used with Mikrotik v2.9x and v3.x.

Router configuration

First of all, we need to configure all routers that will need to be regularly backed up. (in this example, it will be on a daily basis) To do this, we execute this command on every router:

/system scheduler 
add name="backup" on-event="system backup save name=today.backup" \
    start-date=jan/01/1970 start-time=00:00:00 interval=10h comment="" \
    disabled=no

The above command simply creates a Mikrotik backup file called “today.backup” every 10 hours that will be picked up by our main server via FTP. Make sure that you execute the following command manually afterwards in order to be able to test if everything is working correctly without having to wait 10 hours:

/system backup save name=today.backup

The backup should be created. Confirm by taking a quick look in the file list:

Three.jpg

The router’s side of the job is almost complete, but it is still necessary to make sure that FTP access is available:

/ip service print
When the command is executed, the following output (or similar) should appear:
Flags: X - disabled, I - invalid 
 #   NAME      PORT  ADDRESS       CERTIFICATE    
 0   telnet    23    0.0.0.0/0    
 1   ftp       21    0.0.0.0/0    
 2   www       80    0.0.0.0/0      
 3   ssh       22    0.0.0.0/0    
 4 X www-ssl   443   0.0.0.0/0     none
 5 X api       8728  0.0.0.0/0    
 6   winbox    8291  0.0.0.0/0    

If there is no “X” next to the FTP item on the services list, then it is ok to proceed. (it is possible to use a different port other than 21 if preferred). It is recommended to create a separate FTP user (and group) for every router available for security reasons:

/user group 
add name="ftp" policy=ftp,!local,!telnet,!ssh,!reboot,!read,!write,!policy,!test,!winbox,!password,!web,!sniff
/user 
add address=0.0.0.0/0 comment="FTP backup" disabled=no group=ftp name="ftp"
/user 
set [find name="ftp"] password="mypass"

Server configuration

Now that the routers are fully configured, we can move on to the Windows machine that will serve as the backup host. First of all, download the “mtbackup” utility from here. Mtbackup.exe is a simple application I have written that will accept an input template FTP script, modify it accordingly and place it into a specified folder for Windows’ FTP client to use. Mtbackup will execute the FTP script automatically once the script is parsed.

The syntax for “mtbackup.exe” follows:

Usage: mtbackup.exe [input ftp script template] [output ftp script] [destination backup folder] [windows FTP client path]
Example: mtbackup.exe "c:\example\template.s" "c:\example\script.s" "c:\mikrotik-backups" "C:\Windows\System32\ftp.exe"

Copy the “mtbackup.exe” application in any available folder for later use. (in this example, the path will be “c:\mtbackup”)

Creating an FTP script template

This procedure is very straight forward and simple to use. Open notepad (Start > Programs > Accessories > Notepad) and type the following:

o 192.168.0.1 21
USER ftp
mypass
binary
$
192.168.0.1 – Router backup
close
quit

Here’s an explanation of the FTP template script syntax:


o 192.168.0.1 21 (Open a connection to my router’s FTP server – 192.168.0.1 on port 21)

USER ftp (Use the username “ftp” to log in)

mypass (Use the password “mypass” to log in)

binary (Use binary, instead of ASCII mode to preserve integrity of backup files)

$ (Leave this field just the way it is containing only the dollar sign, no spaces)

192.168.0.1 – Router backup (The file name of the router’s backup)

close (Don’t edit this field, it instructs the FTP client to close the connection appropriately)

quit (Don’t edit this field, it instruct the FTP client to exit after executing every command)



Save this file in any location you prefer, but it is recommended that the script stays in the same folder as “mtbackup.exe”. In this example, we will name it “mtscript.s”. In order to have multiple routers backed up, simply add more entries to the script list separated only by one empty row, like so:

o 192.168.0.1 21
USER ftp
mypass1
binary
$
192.168.0.1 - Backup
close

o 192.168.0.2 21
USER ftp
mypass2
binary
$
192.168.0.2 - Backup
close

o 192.168.0.3 21
USER ftp
mypass3
binary
$
192.168.0.3 - Backup
close
quit

Now that the script is configured, a quick test is in order… Using the command line, run the following command:

C:\mtbackup\mtbackup.exe "c:\mtbackup\mtscript.s" "c:\mtbackup\script.s" "c:\mtbackup\backups"

If Windows’ FTP client ftp.exe is not located inside “C:\windows\system32”, use the following command (edit the ftp.exe path correctly):

C:\mtbackup\mtbackup.exe "c:\mtbackup\mtscript.s" "c:\mtbackup\script.s" "c:\mtbackup\backups" "d:\windows\system32\ftp.exe"

Short explanation of the mtbackup's syntax:

c:\mtbackup\mtscript.s – The template script

c:\mtbackup\script.s – The output script that will be used by ftp.exe

c:\mtbackup\backups – The folder containing your backups (it will be created automatically if it doesn’t exist)


After you have executed the command, notice that the backup folder now contains your router backups categorized by their date (provided that you have correctly created the script).

Configuring Windows scheduler

When you have confirmed that everything works the way it should, we can now use Windows Scheduler to execute our backup utility on any given time period. In this example, it will be a daily backup.

Windows Server 2003:

1. Open "Sheduled tasks" (Start > All Programs (Programs) > Accessories > System tools > Scheduled tasks)

2. From the list of currently active tasks (if there is any), select "Add Scheduled Task"

3. The scheduled task wizard will appear. Select NEXT to continue.

4. Select BROWSE to locate "mtbackup.exe"

5. Locate "mtbackup.exe" and select OPEN

6. Type in a name for your scheduled task (in this case it's "Mikrotik Backup"). Select "Daily" to perform this task every day. (feel free to select any other value if you like)

7. 5 o' clock in the morning is a good time to create backups, since there is a very low probability of someone changing any values inside your router outside working hours.

8. Type in your password if you would like the task to execute under your username. Change the values accordingly if you want it to be executed under any other username on the local system.

9. Check "Open advanced properties for this task when I click Finish" and click on the FINISH button.

10. Make sure that the "RUN" field contains the following value:

C:\mtbackup\mtbackup.exe "c:\mtbackup\mtscript.s" "c:\mtbackup\script.s" "c:\mtbackup\backups"

or if your ftp.exe file is located outside "C:\windows\sytem32" (edit the last value accordingly):

C:\mtbackup\mtbackup.exe "c:\mtbackup\mtscript.s" "c:\mtbackup\script.s" "c:\mtbackup\backups" "d:\windows\system32\ftp.exe"

11. Select "OK"

Windows Server 2008:

1. Right click on "Computer" and select "Manage" (the "Computer" icon can be located on your desktop, or inside your Start menu)

2. When the Computer Management console appears, select the + sign next to "Task Scheduler" and select "Task Scheduler Library". Click on "Create Task" on your right.

3. Type in an appropriate name for this task. "Router Backup" should do it. A description is not necessary, but I typed one in just in case somebody else wonders what it does. Make sure that you select "Run whether user is logged in or not" to make sure that the script executes even if there is nobody logged in. Check the "Run with highest privileges" to make sure that the script can write to folders that Windows may consider to be protected.

4. The "Triggers" tab is currently empty. Click on the "NEW" button to add a new trigger.

5. The task should begin "On a schedule". Select the "Daily" option to make the script run every day. The "start" value should be set to a time such as 5AM (when there is no one around to configure the routers and meddle with our backups). Don't forget to check the "Stop the task if it runs longer than..." checkbox and set it to "30" minutes just in case something goes wrong.

6. The "Actions" tab is currently empty. Click on the "NEW" button to add a new action.

7. Make sure that the action is set to "Start a program". Click the "Browse" button and locate "mtbackup.exe". Inside the "Add arguments (optional)" field, type in the following values:

 "c:\mtbackup\mtscript.s" "c:\mtbackup\script.s" "c:\mtbackup\backups"

or if your ftp.exe file is located outside "C:\windows\sytem32" (edit the last value accordingly):

 "c:\mtbackup\mtscript.s" "c:\mtbackup\script.s" "c:\mtbackup\backups" "d:\windows\system32\ftp.exe"

Type in "c:\mtbackup" inside the "Start in (optional)" field as well. (confirm with OK)

8. Under the "Settings" tab, make sure that all parameters are set up just like the ones on the "Step 8" image. Confirm with OK.


Final adjustments

Hosting the backups on a secured FTP server

FTP – You can run an FTP server on your machine that will host your backup folder.

Web – You can point a web server such as Apache to your backup folder and host them online or on a local web server. Using ModRewrite, you can protect them with a username and password as well.

Nothing – You are fine with accessing your backups directly.


Your backups will be created silently from now on. Whenever there is a hardware failure, misconfiguration or any other issue, these backups will always be ready to save the day.


mtbackup.exe mirrors

RapidShare mirror Second mirror