-#!/bin/bash -e
+#!/bin/bash
# shellcheck disable=SC1091
+set -euo pipefail
#set +e -u
#o pipefail
# Add to cron
DEFAULT_BINPWD="BinPwd123"
DEFAULT_EXP_CRYPT=false
DEFAULT_EXPPWD="ExpPwd321"
+RouterName=""
+LogFile="${DEFAULT_LOG}"
# Handy tput commands:
#tput bold - Bold effect #'\033[1m'
#exit 0
save_log() {
local log="${DEFAULT_LOG}"
- if [ -n "${LogFile}" ]; then
+ if [ -n "${LogFile:-}" ]; then
log="${LogFile}"
fi
if ${SAVE_LOG}; then
- echo "$(date '+%b %d %T') - $*" >> ${log}
+ echo "$(date '+%b %d %T') - $*" >> "${log}"
return 0
else
return 1
log_info() {
local msg="$*"
local prefix=""
- if [ -n "${RouterName}" ]; then
+ if [ -n "${RouterName:-}" ]; then
prefix="${RouterName} "
fi
if [ -t 1 ] && $DEBUG_OUT; then
log_success() {
local msg="$*"
local prefix=""
- if [ -n "${RouterName}" ]; then
+ if [ -n "${RouterName:-}" ]; then
prefix="${RouterName} "
fi
if [ -t 1 ] && $DEBUG_OUT; then
log_error() {
local msg="$*"
local prefix=""
- if [ -n "${RouterName}" ]; then
+ if [ -n "${RouterName:-}" ]; then
prefix="${RouterName} "
fi
if [ -t 1 ] && $DEBUG_OUT; then
log_warning() {
local msg="$*"
local prefix=""
- if [ -n "${RouterName}" ]; then
+ if [ -n "${RouterName:-}" ]; then
prefix="${RouterName} "
fi
if [ -t 1 ] && $DEBUG_OUT; then
local remote_login
# Require router-specific SSH key
- if [ -n "$DEFAULT_SSH_KEY" ]; then
+ if [ -f "${DEFAULT_SSH_KEY}" ]; then
opts=(-i "${DEFAULT_SSH_KEY}" -o PasswordAuthentication=no)
else
log_msg="- Error: SSH key not found at $DEFAULT_SSH_KEY\nRun ./setup-ssh-keys.sh to generate the key"
#opts+=("-o PreferredAuthentications=publickey" "-o StrictHostKeyChecking=no" "-o UserKnownHostsFile=/dev/null" \
#"-o GlobalKnownHostsFile=/dev/null" "-o CheckHostIP=no")
# Require router host IP
- if [ -n "${SSHhost}" ]; then
+ if [ -n "${SSHhost:-}" ]; then
host="${SSHhost}"
else
log_msg="- Error! SSH Host not found. Exit 1."
ssh_cmd=("$CMD_SSH" "-2" "-4" "${opts[@]}")
scp_cmd=("$CMD_SCP" "-2" "-4" "-B" "${opts[@]}")
- if [ -n "${SSHport}" ]; then
+ if [ -n "${SSHport:-}" ]; then
ssh_cmd+=("-p" "${SSHport}")
scp_cmd+=("-P" "${SSHport}")
else
scp_cmd+=("-P" "${DEFAULT_SSH_PORT}")
fi
- if [ -n "${SSHuser}" ]; then
+ if [ -n "${SSHuser:-}" ]; then
ssh_cmd+=("-l" "${SSHuser}")
remote_login="${SSHuser}@${host}"
else
download_config() {
local conf="${1}"
local log_msg
+ local pid
+ local rc
"${SSH_CMD[@]}" "/export" > "${conf}" & pid=$!
spinstr "$pid"
wait "$pid"
rc=$?
- if [ $rc -eq 0 ]; then
+ if [ "${rc}" -eq 0 ]; then
log_msg="- Config download success!"
log_success "${log_msg}"
return 0
local chk_dir="${1}"
local log_msg
- if [[ -d ${chk_dir} && -r ${chk_dir} ]]
+ if [[ -d "${chk_dir}" && -r "${chk_dir}" ]]
then
return 0
else
if "${CMD_MKD}" -p "${chk_dir}"; then
- log_msg="- Direcory ${chk_dir} created."
+ log_msg="- Directory ${chk_dir} created."
log_success "${log_msg}"
return 0
else
- log_msg="- Error create direcory ${chk_dir}! Exit 1."
+ log_msg="- Error creating directory ${chk_dir}! Exit 1."
log_error "${log_msg}"
exit 1
fi
local bin_tmp="Binary.backup"
local log_msg
local crypt_str
+ local pid
+ local rc
# Examples:
# bin_pass="$(${SSH_STR} ':put [/system script environment get [find name=BackupPassword] value]')"
# bin_pass="$(${SSH_STR} ':put $BackupPassword')" # if BackupPassword is a global
spinstr "$pid"
wait "$pid"
rc=$?
- if [ $rc -eq 0 ]; then
+ if [ "${rc}" -eq 0 ]; then
log_msg="Create binary backup ${bin_tmp} on ${RouterName} success."
log_success "${log_msg}"
spinstr "$pid"
wait "$pid"
rc=$?
- if [ $rc -eq 0 ]; then
+ if [ "${rc}" -eq 0 ]; then
log_msg="- Binary Backup download complete!"
log_success "${log_msg}"
else
- log_msg="- Error! Download binary backup on ${RoutrName} failed!"
+ log_msg="- Error! Download binary backup on ${RouterName} failed!"
log_error "${log_msg}"
fi
else
log_msg="- Remove ${bin_tmp} file on ${RouterName}."
log_info "${log_msg}"
if "${SSH_CMD[@]}" "/file remove [find name=${bin_tmp}]"; then
- log_msg="- Remove ${bin_tmp} file on ${RouterName} complete succesfully!"
+ log_msg="- Remove ${bin_tmp} file on ${RouterName} completed successfully!"
log_success "${log_msg}"
else
log_msg="- Error Remove ${bin_tmp} file on ${RouterName}!"
readarray -t lines < "$HostsFile"
for line in "${lines[@]}"; do
read -r -a HR <<< "${line}"
-[[ -z "${HR[0]}" || "${HR[0]}" =~ ^# ]] && continue
+[[ ${#HR[@]} -eq 0 || -z "${HR[0]:-}" || "${HR[0]:-}" =~ ^# ]] && continue
DStamp=$(date '+%Y%m%d')
DTStamp=$(date '+%Y%m%d%H%M%S')
RouterName=${HR[0]}
-SSHhost=${HR[1]}
-SSHport=${HR[2]}
-SSHuser=${HR[3]}
-backup_pass=${HR[4]}
+SSHhost=${HR[1]:-}
+SSHport=${HR[2]:-}
+SSHuser=${HR[3]:-}
+backup_pass=${HR[4]:-}
+
+if [ -z "${SSHhost}" ]; then
+ log_error "Host is missing in ${HostsFile} for router ${RouterName}. Skip."
+ continue
+fi
get_connections_str
TempDir=${BaseDir}"tmp/"
HistoryDir=${BaseDir}"history/"
LogFile="/var/log/Mikrotiks/${RouterName}.log"
-log_msg="undefined"
log_msg="- Start backup script ${ScriptName} for ${RouterName}"
log_info "${log_msg}"
echo
fi
-log_msg="- Check and creatre services directories for ${RouterName} if not exsist..."
+log_msg="- Check and create service directories for ${RouterName} if they do not exist..."
if [ -t 1 ] && $DEBUG_OUT; then
section " CHECK DIRS "
fi
CheckDirs=("${BaseDir}" "${TempDir}" "${HistoryDir}")
for dir in "${CheckDirs[@]}"; do
-(check_directory "${dir}")
+check_directory "${dir}"
done
-log_msg="All Directories for ${RouterName} was created sucessfully!"
+log_msg="All directories for ${RouterName} are ready."
log_success "${log_msg}"
log_msg="Set variables for ${RouterName}..."
precedent_config=${BaseDir}"Precedent-Config.rsc"
current_config=${TempDir}"Config.rsc"
diff_config=${TempDir}"Diff-Config.rsc"
- log_msg="Set variables for ${RouterName} success!"
- log_success "${log_msg}"
+log_msg="Set variables for ${RouterName} success."
+log_success "${log_msg}"
section " CHECK CONFIG "
log_msg="- Download ${current_config} file to Temp Directory..."
log_msg="- Comparing with the previous config..."
log_info "${log_msg}"
-if compare_config ${current_config} ${precedent_config} ${diff_config} ${RouterName} ${LogFile}; then
+if compare_config "${current_config}" "${precedent_config}" "${diff_config}"; then
BackupDir=${BaseDir}"${DStamp}/"
"${CMD_MKD}" -p "${BackupDir}"
cp "${current_config}" "${save_config}"
- ${CMD_RM} -f "${precedent_config}"
+ "${CMD_RM}" -f "${precedent_config}"
- ${CMD_MV} -f "${current_config}" "${precedent_config}"
+ "${CMD_MV}" -f "${current_config}" "${precedent_config}"
log_msg="- Copy configuration to ${save_config} and ${precedent_config} success."
log_success "${log_msg}"
log_msg="- Move ${diff_config} file to ${CurrentHistoryDir}."
log_info "${log_msg}"
- if ${CMD_MV} -f "${diff_config}" "${diff_config_history}"; then
- log_msg="- Move ${diff_config} file to ${CurrentHistoryDir} complete succesfully!"
+ if "${CMD_MV}" -f "${diff_config}" "${diff_config_history}"; then
+ log_msg="- Move ${diff_config} file to ${CurrentHistoryDir} completed successfully!"
log_success "${log_msg}"
else
log_msg="- Error move ${diff_config} file to ${CurrentHistoryDir}!"
log_error "${log_msg}"
fi
- log_msg="- Go to Git directory (${GitDir}), add backups files and commit him."
+ log_msg="- Go to Git directory (${GitDir}), add backup files and commit."
if [ -t 1 ] && $DEBUG_OUT; then
section " COMMIT "
fi
git add .
log_msg="- Git add commit..."
log_info "${log_msg}"
- if git commit -m "Backup for ${RouterName} sucessfully creatd at $(date '+%b %d %T')."; then
+ if git commit -m "Backup for ${RouterName} created successfully at $(date '+%b %d %T')."; then
log_msg=" - Git add commit success."
log_success "${log_msg}"
else
log_error "${log_msg}"
fi
else
- log_msg="- ${RouterName} backup complete!"
+ log_msg="- ${RouterName}: backup complete."
log_info "${log_msg}"
fi
log_info "${log_msg}"
"${CMD_RM}" -d "${TempDir}"
-log_msg="- backup complete!"
+log_msg="- Backup complete."
log_success "${log_msg}"
done