Blackhold

Script para hacer backups de los router mikrotik

Posted on juliol 5th, 2015 by admin

Pues aquí una pequeña ayuda :)

#!/bin/bash

############################################
# SCRIPT CREATED BY: Laura Mora i Aubert   #
# SCRIPT DATE: 2015-07-05                  #
# WEBSITE: http://blackhold.nusepas.com    #
# INFO: Script that allows remote backup   #
#       of Mikrotik Routers.               #
#       tested on 5.x & 6.x routers        #
# LICENSE: creative commons (by:sa)        #
############################################

############################################
#             INSTRUCTIONS                 #
############################################
#                                          #
# 1. Generate ssh-keys on your server      #
#      cd ~/.ssh                           #
#      sh-keygen -t dsa                    #
#                                          #
# 2. Rename pubkey                         #
#      cp .ssh/id_dsa.pub backupkey        #
#                                          #
# 3. Put the file on MKT Router            #
#      ftp x.x.x.x                         #
#      ftp> bin                            #
#      ftp> put backupkey                  #
#      ftp> bye                            #
#                                          #
# 4. Import pubkey in MKT Router           #
#      telnet x.x.x.x                      #
#      [admin@mikrotik] /user ssh-keys    #
#       import user=admin                 #
#       public-key-file=backupkey          #
#                                          #
# 5. Access with ssh to the MKT Router     #
#                                          #
# 6. [SCRIPT] Change USER  and ID values   #
#                                          #
# 7. Run the script!                       #
#                                          #
############################################

############################################
#               OTHERS                     #
############################################
#                                          #
# If you have bruteforce login rules,      #
# add your backup server IP in the last    #
# rule: src-address=!SERVERIP              #
#                                          #
############################################ 

# Change username
USER=admin
BACKUP_DIR="/root/backups/"


if [ ! -d "${BACKUP_DIR}" ]; then
        mkdir -p ${BACKUP_DIR}
fi

cd ${BACKUP_DIR}

############################################
# Keep this structure, take care of the :  #
# Increment the "n" with a number          #
# ID[n]="RouterName:IPAddress:Port"        #
############################################ 
ID[0]="Router-A:10.0.0.1:22"
ID[1]="Router-B:192.168.1.1:22"
ID[2]="Router-C:10.1.0.1:22"

for INFO in "${ID[@]}"
do
   DATE=`date +%Y-%m-%d_%H:%M:%S`
   NAME=`echo ${INFO} |awk -F ':' '{print $1}'`
   IP=`echo ${INFO} |awk -F ':' '{print $2}'`
   PORT=`echo ${INFO} |awk -F ':' '{print $3}'`

   echo "-----------------------------------------------------"
   echo "Starting Backup: ${NAME} (${IP}:${PORT}) on ${DATE}" 
   echo "-----------------------------------------------------"
   ssh -p${PORT} ${USER}@${IP} "/export f=${NAME}_${IP}_${DATE}"
   ssh -p${PORT} ${USER}@${IP} "/system backup save name=${NAME}_${IP}_${DATE}"
   ssh -p${PORT} ${USER}@${IP} "/ip service enable ftp"
   sleep 5
   sftp ${USER}@${IP}:${NAME}_${IP}_${DATE}.rsc
   sftp ${USER}@${IP}:${NAME}_${IP}_${DATE}.backup
   ssh -p${PORT} ${USER}@${IP} "/ip service disable ftp"
   ssh -p${PORT} ${USER}@${IP} "/file remove ${NAME}_${IP}_${DATE}.rsc"
   ssh -p${PORT} ${USER}@${IP} "/file remove ${NAME}_${IP}_${DATE}.backup"
   echo "-----------------------------------------------------"
   echo
done

« »

guy fawkes