From 13a5e8252e001f96a6187529b765520b07e1c270 Mon Sep 17 00:00:00 2001 From: vados-dev <192440777+vados-dev@users.noreply.github.com> Date: Sun, 5 Apr 2026 04:14:16 +0300 Subject: [PATCH] Harden ros-diff script options and logging --- bin/ros-diff.sh | 89 +++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/bin/ros-diff.sh b/bin/ros-diff.sh index 5e7a65b..65d633c 100755 --- a/bin/ros-diff.sh +++ b/bin/ros-diff.sh @@ -1,5 +1,6 @@ -#!/bin/bash -e +#!/bin/bash # shellcheck disable=SC1091 +set -euo pipefail #set +e -u #o pipefail # Add to cron @@ -69,6 +70,8 @@ DEFAULT_BIN_CRYPT=true DEFAULT_BINPWD="BinPwd123" DEFAULT_EXP_CRYPT=false DEFAULT_EXPPWD="ExpPwd321" +RouterName="" +LogFile="${DEFAULT_LOG}" # Handy tput commands: #tput bold - Bold effect #'\033[1m' @@ -146,11 +149,11 @@ tput cnorm -- #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 @@ -160,7 +163,7 @@ save_log() { log_info() { local msg="$*" local prefix="" - if [ -n "${RouterName}" ]; then + if [ -n "${RouterName:-}" ]; then prefix="${RouterName} " fi if [ -t 1 ] && $DEBUG_OUT; then @@ -172,7 +175,7 @@ log_info() { log_success() { local msg="$*" local prefix="" - if [ -n "${RouterName}" ]; then + if [ -n "${RouterName:-}" ]; then prefix="${RouterName} " fi if [ -t 1 ] && $DEBUG_OUT; then @@ -184,7 +187,7 @@ log_success() { log_error() { local msg="$*" local prefix="" - if [ -n "${RouterName}" ]; then + if [ -n "${RouterName:-}" ]; then prefix="${RouterName} " fi if [ -t 1 ] && $DEBUG_OUT; then @@ -196,7 +199,7 @@ log_error() { log_warning() { local msg="$*" local prefix="" - if [ -n "${RouterName}" ]; then + if [ -n "${RouterName:-}" ]; then prefix="${RouterName} " fi if [ -t 1 ] && $DEBUG_OUT; then @@ -214,7 +217,7 @@ get_connections_str() { 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" @@ -230,7 +233,7 @@ get_connections_str() { #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." @@ -241,7 +244,7 @@ get_connections_str() { 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 @@ -249,7 +252,7 @@ get_connections_str() { scp_cmd+=("-P" "${DEFAULT_SSH_PORT}") fi - if [ -n "${SSHuser}" ]; then + if [ -n "${SSHuser:-}" ]; then ssh_cmd+=("-l" "${SSHuser}") remote_login="${SSHuser}@${host}" else @@ -278,11 +281,13 @@ run_ssh_cmd() { 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 @@ -345,16 +350,16 @@ check_directory() { 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 @@ -372,6 +377,8 @@ backup_binary() { 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 @@ -392,7 +399,7 @@ backup_binary() { 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}" @@ -402,11 +409,11 @@ backup_binary() { 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 @@ -420,7 +427,7 @@ backup_binary() { 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}!" @@ -431,16 +438,21 @@ backup_binary() { 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 @@ -448,7 +460,6 @@ BaseDir=${HomeDir}"/backups/${RouterName}/" 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}" @@ -456,7 +467,7 @@ if [ -t 1 ] && $DEBUG_OUT; then 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 @@ -464,10 +475,10 @@ log_info "${log_msg}" 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}..." @@ -476,8 +487,8 @@ log_info "${log_msg}" 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..." @@ -488,7 +499,7 @@ download_config "${current_config}" 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}" @@ -500,9 +511,9 @@ if compare_config ${current_config} ${precedent_config} ${diff_config} ${RouterN 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}" @@ -517,15 +528,15 @@ if compare_config ${current_config} ${precedent_config} ${diff_config} ${RouterN 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 @@ -534,7 +545,7 @@ if compare_config ${current_config} ${precedent_config} ${diff_config} ${RouterN 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 @@ -551,7 +562,7 @@ if compare_config ${current_config} ${precedent_config} ${diff_config} ${RouterN log_error "${log_msg}" fi else - log_msg="- ${RouterName} backup complete!" + log_msg="- ${RouterName}: backup complete." log_info "${log_msg}" fi @@ -559,7 +570,7 @@ log_msg="- Remove Temp directory." log_info "${log_msg}" "${CMD_RM}" -d "${TempDir}" -log_msg="- backup complete!" +log_msg="- Backup complete." log_success "${log_msg}" done -- 2.52.0