Arreglando el script de hacer backup con mikrotik

Actualizo éste script/post para que si por algún motivo no pueda conectar con el router mikrotik haga timeout y siga con el Backup.

root@xava-ftp:~/scripts# cat /root/scripts/backup_mikrotiks_SN.sh 
#!/bin/bash

############################################
# SCRIPT CREATED BY: Laura Mora i Aubert   #
# SCRIPT DATE: 2019-11-27                  #
# WEBSITE: https://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=blackhold
BACKUP_DIR="/root/backups/capa8"


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

cd ${BACKUP_DIR}

# delete files older than x days
find ${BACKUP_DIR}/ -mtime +7 -exec rm {} \;

############################################
# Keep this structure, take care of the :  #
# Increment the "n" with a number          #
# ID[n]="RouterName:IPAddress:Port"        #
############################################ 
ID[0]="Loc01-Router01:10.x.x.x:22"
ID[1]="Loc02-Router02:192.168.x.x: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} -o ConnectTimeout=3 "/export f=${NAME}_${IP}_${DATE}"
   ssh -p${PORT} ${USER}@${IP} -o ConnectTimeout=3 "/system backup save name=${NAME}_${IP}_${DATE}"
   ssh -p${PORT} ${USER}@${IP} -o ConnectTimeout=3 "/ip service enable ftp"
   sleep 5
   sftp -o ConnectTimeout=3 ${USER}@${IP}:${NAME}_${IP}_${DATE}.rsc
   sftp -o ConnectTimeout=3 ${USER}@${IP}:${NAME}_${IP}_${DATE}.backup
   ssh -p${PORT} ${USER}@${IP} -o ConnectTimeout=3 "/ip service disable ftp"
   ssh -p${PORT} ${USER}@${IP} -o ConnectTimeout=3 "/file remove ${NAME}_${IP}_${DATE}.rsc"
   ssh -p${PORT} ${USER}@${IP} -o ConnectTimeout=3 "/file remove ${NAME}_${IP}_${DATE}.backup"
   echo "-----------------------------------------------------"
   echo
done

Deixa un comentari

L'adreça electrònica no es publicarà. Els camps necessaris estan marcats amb *

Aquest lloc utilitza Akismet per reduir els comentaris brossa. Apreneu com es processen les dades dels comentaris.