Tuesday, June 16, 2015

Mikrotik backup script using SSH/SCP - easy and dirty way

*Disclaimer*
This is not high-end, ultra safe script. It's considered a bad practice to store your passwords in cleartext, especially in script file. I am beginner in scripting and this is just a way to automate task that when done by hand could take very long time. But, if you still want to proceed, read on!

For this script to work we will need to install program called sshpass, install it from your distro repository. This script assumes that all your Mikrotik devices have same username and password and SSH service enabled.
EDIT: After some experimenting it seems that sometimes ConnectTimeout=10 is not enough, if you have troubles connecting, increase to 20.

#!/bin/bash
#Don't just copy and paste this script, adjust all paths to your needs
#You can remove any line starting with hashtag because those are comments and they are not executed

NOW=$(date +%d-%b)
#This line creates variable NOW with day and month, like "03-May"
#I will use this example, "03-May" to make it easier to follow

COUNTER=0
#sets counter to zero

mkdir /home/locodog/backups/mikrotik/$NOW
#creates directory with name according to date "03-May" for example

for i in $(cat /home/locodog/mikrotik-backup-script/tiks.txt); do
#tiks.txt is file that has list of all Mikrotik devices you want to backup
COUNTER=$((COUNTER + 1))
#adds +1 to counter for every device it attempts to backup

echo -e $COUNTER"." "Backing up device \033[1m$i\033[0m" 
#tells you which device is currently being backed up

sshpass -p "password" ssh -oStrictHostKeyChecking=no -o ConnectTimeout=15 $i -l username system backup save name=$NOW
#Connects to mikrotik device from list and creates backup file with name "03-May"

sshpass -p "password" scp -oStrictHostKeyChecking=no -o ConnectTimeout=10 username@$i:$NOW.backup /home/locodog/backups/mikrotik/$NOW/$i.backup
#Downloads previously created backup file but now that file is downloaded into folder "03-May" 
#and file has name 192.168.1.1.backup (or whatever IP address your Mikrotik has
#keep in mind that backup file inside your Mikrotik will not be deleted and it will have a name 03-May.backup
done

TOTAL=$(echo $COUNTER)
#Creates variable "total" with value equal to total number of devices that were backed up (or attempted)
for f in $(cat /home/locodog/mikrotik-backup-script/tiks.txt); do
if [ ! -f /home/locodog/backups/mikrotik/$NOW/$f.backup ];
 then
echo -e "Backup of \033[1m$f\033[0m was not done" && COUNTER=$((COUNTER - 1 ))
fi
done
#this section checks is there any backup file missing and
#gives you IP address of device where backup could not be performed for whatever reason

echo -e "Backup was successful on \033[1m$COUNTER\033[0m out of \033[1m$TOTAL\033[0m Mikrotik devices."
#Here you get report, on how many devices did you try to do backup and how many sucessful

After adjusting this script to your needs save it in file for example backup-mikrotik.sh
and make it executable with chmod +x backup-mikrotik.sh

File tiks.txt (or whatever you decide to call it) should have list of IP addresses of your Mikrotik devices like this:
192.168.1.1
192.168.1.2
192.163.25.4
10.25.66.8

And so on. This file should be plain text, that means don't create it with LibreOffice or anything like that. Use plain text editor like Gedit, Kate, Leafpad, Vim.

No comments:

Post a Comment