Automatic Centralized Backup from Linux

From MikroTik Wiki
Jump to: navigation, search

Summary

This is BASH script executed from linux system, getting list of RouterOS devices, creating backup and downloading them.

First part of script connect to specified RouterOS device, read ip addresses of MikroTik devices and store them to file. Exactly script make this:

  • get list of ip addresses of Platform=MikroTik devices from / ip neighbour
  • compare addresses with previous list stored in file and ADD there new one

Second part of script make backup and export of RouterOS devices listed in this file. Exactly script make this:

  • download ALL files from device
  • ERASE ALL files which with TYPE=SCRIPT (*.rsc) and TYPE=BACKUP (*.backup)
  • create backup and export to file named SYSTEM_IDENTITY-YYYYmmmDD-HHMMSS.backup and .rsc
  • download ALL files from device

WARNING - THIS SCRIPT ERASE ALL PREVIOUS SCRIPT AND BACKUP FILES FROM DEVICE

Prerequisite

  • Linux machine (tested on Debian Etch, Ubuntu 10.04) with package sshpass
sudo aptitude install sshpass -y
  • IPv4 network (probably didnt work with IPv6 formating of ip address)
  • RouterOS devices (tested on 2.9, v3, v4)
  • Enabled ssh and ftp on RouterOS devices
/ ip service set ssh disabled=no port=XXXX
/ ip service set ftp disabled=no port=XXXX

Scripts

Getting list of RouterOS devices

Read ip addresses of Platform=MikroTik devices from / ip neighbour:

Write this code of this script to some file e.g. writemtlist.sh:

#!/bin/bash
# Install package "sshpass" before using this script (sudo aptitude install sshpass -y).
LOGIN=admin
PASSWORD=$(awk 'NR == 1' pf) #read password from file named "pf"
SSHPORT=2200
HOSTIP=$1
sshpass -p $PASSWORD ssh $LOGIN@$HOSTIP -p $SSHPORT -o StrictHostKeyChecking=no ':global mtlist [/ip neighbor find platform="MikroTik"]; :foreach i in=$mtlist do={:put [/ip neighbor get $i address]};' >mtlist-temp ; cat mtlist-temp >>mtlist-full
cat mtlist mtlist-temp | sort | uniq > mtlist-add ; mv mtlist-add mtlist
echo $(cat mtlist-temp | sort | uniq | wc -l) aktualne naskeneno tiku.
echo $(cat mtlist-full | sort | uniq | wc -l) celkove naskeneno tiku.
echo $(cat mtlist | sort | uniq | wc -l) celkove naskeneno tiku - mtlist.
rm -fR mtlist-temp mtlist-add

Get this source code from: http://rrob.pastebin.com/SS2dVP2h

Make file executable:

 user@linuxumachine:~/$ chmod +x writemtlist.sh

Run script with ip address of machine (dont forget ./ before script):

 user@linuxumachine:~/$ ./writemtlist.sh 10.10.10.1

This script generate file mtlist containing list of ip addresses. Like this:

user@linuxumachine:~/$ cat mtlist
10.10.10.1
10.10.10.25
10.10.10.26

Backup

WARNING - THIS SCRIPT ERASE ALL PREVIOUS SCRIPT AND BACKUP FILES FROM DEVICE

Write this code of this script to some file e.g. backupmtlist.sh:

#!/bin/bash
# Install package "sshpass" before using this script (sudo aptitude install sshpass -y).
LOGIN=admin
PASSWORD=$(awk 'NR == 1' pf) #read password from file named "pf"
SSHPORT=2200
FTPPORT=2100
IPLISTFILE=mtlist #read ip addresses from this file - one address per line, end of line (press enter) after each
SUBDIR=mkbackup #directory where script store files
LOGFILE=zalohatik.log #file to log
index=0
while read line ; do
IPLIST[$index]="$line"
index=$(($index+1))
done < $IPLISTFILE
echo ${iplist[@]}
for (( i=0; i<${#IPLIST[@]}; i++ ));
do
HOSTIP=${IPLIST[$i]}
echo  >>$LOGFILE
echo $HOSTIP `date +%Y%m%d%H%M%S` >>$LOGFILE
wget -N -nv -P $SUBDIR/$HOSTIP ftp://$HOSTIP:$FTPPORT/* --ftp-user=$LOGIN --ftp-password=$PASSWORD >>$LOGFILE  2>>$LOGFILE
sshpass -p $PASSWORD ssh $LOGIN@$HOSTIP -p $SSHPORT -o StrictHostKeyChecking=no ':global rmbackup [/file find type="backup"]; :foreach i in=$rmbackup do={:put [/file remove $i]}' >>$LOGFILE  2>>$LOGFILE
sshpass -p $PASSWORD ssh $LOGIN@$HOSTIP -p $SSHPORT -o StrictHostKeyChecking=no ':global rmscript [/file find type="script"]; :foreach i in=$rmscript do={:put [/file remove $i]}' >>$LOGFILE  2>>$LOGFILE
sshpass -p $PASSWORD ssh $LOGIN@$HOSTIP -p $SSHPORT -o StrictHostKeyChecking=no ':global zalohafile ( [/ system identity get name] . "-" . [:pick [/ sys clock get date] 7 11] . [:pick [/ sys clock get date] 0 3] . [:pick [/ sys clock get date] 4 6] . "-" . [:pick [/ sys clock get time] 0 2] . [:pick [/ sys clock get time] 3 5] . [:pick [/ sys clock get time] 6 8]) ; / system backup save name=$zalohafile ; / export file=$zalohafile' >>$LOGFILE  2>>$LOGFILE
wget -N -nv -P $SUBDIR/$HOSTIP ftp://$HOSTIP:$FTPPORT/* --ftp-user=$LOGIN --ftp-password=$PASSWORD >>$LOGFILE  2>>$LOGFILE
done

Get this source code from: http://rrob.pastebin.com/8K5twxpM.

Make file executable:

 user@linuxumachine:~/$ chmod +x backupmtlist.sh

Run script (dont forget ./ before script):

 user@linuxumachine:~/$ ./backupmtlist.sh

Script create subdirectory speficied in variable $SUBDIR (e.g. mkbackup), files are stored in subdirectories named by ip addresses:

user@linuxumachine:~/$ tree mkbackup
mkbackup2
|-- 10.10.10.1
|-- 10.10.10.25
`-- 10.10.10.26