]> Git [git.vados.ru] :: Repos - Mikrotiks/Mikrotiks.git/commitdiff
Fix ros-diff compare and backup temp handling
authorvados-dev <192440777+vados-dev@users.noreply.github.com>
Sun, 5 Apr 2026 00:53:18 +0000 (03:53 +0300)
committervados-dev <192440777+vados-dev@users.noreply.github.com>
Sun, 5 Apr 2026 00:53:18 +0000 (03:53 +0300)
bin/ros-diff.sh

index 1a46738c9746e1b34ea14ac4de547a249e403db9..3533d726c156a06e3a974dcc415fd389082b822e 100755 (executable)
@@ -34,7 +34,7 @@ CMD_MV=$(which mv)
 CMD_GZ=$(which gzip)
 CMD_CHO=$(which chown)
 CMD_CHM=$(which chmod)
-CMD_MKD=$(which mkdir)" -p "
+CMD_MKD=$(which mkdir)
 CMD_RM=$(which rm)
 CMD_DATE=$(date +%Y%m%d_%H%M) # date in format YYYYMMDD_HHmm
 CMD_SSL=$(which openssl)
@@ -116,7 +116,11 @@ warning() {
     echo -e "${yellow}⚠  $*${reset}" >&2
 }
 section() {
-    echo -e "\n${magenta}................[$(tput rev) $* ${reset}${magenta}]................${reset}\n"
+    if [ -t 1 ] && $DEBUG_OUT; then
+        echo -e "\n${magenta}................[$(tput rev) $* ${reset}${magenta}]................${reset}\n"
+    else
+        echo -e "\n................[ $* ]................\n"
+    fi
 }
 
 spinstr()
@@ -205,12 +209,12 @@ get_connections_str() {
     local opts
     local log_msg
     local host
-    local ssh_str
-    local scp_str
+    local ssh_cmd
+    local scp_cmd
 
 # Require router-specific SSH key
     if [ -n "$DEFAULT_SSH_KEY" ]; then
-        opts=("-i ${DEFAULT_SSH_KEY}" "-o PasswordAuthentication=no")
+        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"
         log_error "${log_msg}"
@@ -224,8 +228,6 @@ get_connections_str() {
 #    fi
     #opts+=("-o PreferredAuthentications=publickey" "-o StrictHostKeyChecking=no" "-o UserKnownHostsFile=/dev/null" \
     #"-o GlobalKnownHostsFile=/dev/null" "-o CheckHostIP=no")
-SSH_OPTS="${opts[*]}"
-
 # Require router host IP
     if [ -n "${SSHhost}" ]; then
         host="${SSHhost}"
@@ -235,30 +237,28 @@ SSH_OPTS="${opts[*]}"
         exit 1
     fi
 
-    ssh_str=("$CMD_SSH" "-2" "-4")    
-    scp_str=("${CMD_SCP}" "-2" "-4" "-B ${SSH_OPTS}")
+    ssh_cmd=("$CMD_SSH" "-2" "-4" "${opts[@]}")
+    scp_cmd=("$CMD_SCP" "-2" "-4" "-B" "${opts[@]}")
 
     if [ -n "${SSHport}" ]; then
-        ssh_str+=("-p ${SSHport}")
-        scp_str+=("-P ${SSHport}")
+        ssh_cmd+=("-p" "${SSHport}")
+        scp_cmd+=("-P" "${SSHport}")
     else
-        ssh_str+=("-p ${DEFAULT_SSH_PORT}")
-        scp_str+=("-P ${DEFAULT_SSH_PORT}")
+        ssh_cmd+=("-p" "${DEFAULT_SSH_PORT}")
+        scp_cmd+=("-P" "${DEFAULT_SSH_PORT}")
     fi
 
     if [ -n "${SSHuser}" ]; then
-        ssh_str+=("-l ${SSHuser}")
-        scp_str+=("${SSHuser}@${host}")
+        ssh_cmd+=("-l" "${SSHuser}")
+        scp_cmd+=("${SSHuser}@${host}")
     else
-        ssh_str+=("-l ${DEFAULT_SSH_USER}")
-        scp_str+=("${DEFAULT_SSH_USER}@${host}")
+        ssh_cmd+=("-l" "${DEFAULT_SSH_USER}")
+        scp_cmd+=("${DEFAULT_SSH_USER}@${host}")
     fi
 
-    ssh_str+=("${host}")
-
-ssh_str+=("${SSH_OPTS}")
-SSH_STR="${ssh_str[*]}"
-SCP_STR="${scp_str[*]}"
+    ssh_cmd+=("${host}")
+    SSH_CMD=("${ssh_cmd[@]}")
+    SCP_CMD=("${scp_cmd[@]}")
 #SFTP_STR="${sftp_str[*]}"
 #SFTP_OPTS="-i ${DEFAULT_SSH_KEY} -o PasswordAuthentication=no -oPort=${SSHport}"
 }
@@ -266,7 +266,7 @@ SCP_STR="${scp_str[*]}"
 run_ssh_cmd() {
     local run="${1}"
     warning "${run}"
-    if ${SSH_STR} ${run}; then
+    if "${SSH_CMD[@]}" "${run}"; then
         return 0
     else
         return 1
@@ -276,7 +276,7 @@ run_ssh_cmd() {
 download_config() {
     local conf="${1}"
     local log_msg
-    ${SSH_STR} "/export" >${conf} >/dev/null & pid=$!
+    "${SSH_CMD[@]}" "/export" > "${conf}" & pid=$!
     spinstr "$pid"
     wait "$pid"
     rc=$?
@@ -297,9 +297,19 @@ compare_config() {
     local diff_file="${3}"
     local diff_args=("${DEFAULT_DIFF_ARGS[@]}")
     local log_msg
+    local diff_rc
+
+    if [ ! -f "${precedent_file}" ]; then
+        log_msg="${precedent_file} - Precedent file not found. First run, force backup."
+        log_warning "${log_msg}"
+        printf "First run: no precedent config\n" > "${diff_file}"
+        return 0
+    fi
+
+    diff "${diff_args[@]}" "${config_file}" "${precedent_file}" > "${diff_file}"
+    diff_rc=$?
 
-    diff "${diff_args[@]}" ${config_file} ${precedent_file} >${diff_file}
-    if [ "$?" -ne "0" ]; then
+    if [ "${diff_rc}" -eq 1 ]; then
         log_msg=${diff_file}" - There is a difference! Create backup..."
         if [ -t 1 ] && $DEBUG_OUT; then
             section " CREATE BACKUP "
@@ -307,7 +317,7 @@ compare_config() {
         fi
         log_info "${log_msg}"
         return 0
-    else
+    elif [ "${diff_rc}" -eq 0 ]; then
         log_msg=${diff_file}" - The files are identical. No backup is required."
         log_info "${log_msg}"
         if [ -t 1 ] && $DEBUG_OUT; then
@@ -316,12 +326,16 @@ compare_config() {
         fi
         log_msg="- Remove ${diff_file} file."
         log_info "${log_msg}"
-        ${CMD_RM} -f ${diff_file}
+        ${CMD_RM} -f "${diff_file}"
         log_msg="- Remove ${config_file} file."
         log_info "${log_msg}"
-        ${CMD_RM} -f ${config_file}
+        ${CMD_RM} -f "${config_file}"
 
         return 1
+    else
+        log_msg="${diff_file} - Diff command failed with rc=${diff_rc}. Exit 1."
+        log_error "${log_msg}"
+        exit 1
     fi
 }
 
@@ -333,7 +347,7 @@ check_directory() {
   then
    return 0
   else
-    if $CMD_MKD ${chk_dir}; then
+    if "${CMD_MKD}" -p "${chk_dir}"; then
         log_msg="- Direcory ${chk_dir} created."
         log_success "${log_msg}"
         return 0
@@ -359,10 +373,10 @@ backup_binary() {
     # 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
-    if [ -n "$(${SSH_STR} ':put $BackupEncrypt')" ] && [ -n "$(${SSH_STR} ':put $BackupPassword')" ] ; then
-        crypt_str="dont-encrypt=no password=$(${SSH_STR} ':put $BackupPassword')"
+    if [ -n "$("${SSH_CMD[@]}" ':put $BackupEncrypt')" ] && [ -n "$("${SSH_CMD[@]}" ':put $BackupPassword')" ] ; then
+        crypt_str="dont-encrypt=no password=$("${SSH_CMD[@]}" ':put $BackupPassword')"
     else
-        if [ -n ${bin_crypt} ]; then
+        if [ "${bin_crypt}" = true ]; then
             crypt_str="dont-encrypt=no password=${bin_pass}"
         else
             crypt_str="dont-encrypt=yes"
@@ -372,7 +386,7 @@ backup_binary() {
     log_msg="Create ${bin_tmp} file on ${RouterName}..."
     log_info "${log_msg}"
 
-    ${SSH_STR} "/system backup save name=${bin_tmp} ${crypt_str}" >/dev/null & pid=$!
+    "${SSH_CMD[@]}" "/system backup save name=${bin_tmp} ${crypt_str}" & pid=$!
     spinstr "$pid"
     wait "$pid"
     rc=$?
@@ -382,8 +396,7 @@ backup_binary() {
 
         log_msg="- Start download ${bin_tmp} file to ${BackupDir}..."
         log_info "${log_msg}"
-        # Здесь для /dev/null не хватит прав!
-        ${SCP_STR}:/${bin_tmp} ${bin_file} >/dev/null & pid=$!
+        "${SCP_CMD[@]}" ":/${bin_tmp}" "${bin_file}" & pid=$!
         spinstr "$pid"
         wait "$pid"
         rc=$?
@@ -404,10 +417,7 @@ backup_binary() {
     fi
     log_msg="- Remove ${bin_tmp} file on ${RouterName}."
     log_info "${log_msg}"
-    # Здесь не удаляется уже почему-то созданный ${bin_file}, хотя compare_config сообщил, 
-    # что конфиг не менялся (а он 0 как и то, с чем он сравнивал)!
-    # А потом не удаляется и каталог ${TempDir} из 551-й строки.
-    if ${SSH_STR} "/file remove [find name=${bin_tmp}]"; then
+    if "${SSH_CMD[@]}" "/file remove [find name=${bin_tmp}]"; then
         log_msg="- Remove ${bin_tmp} file on ${RouterName} complete succesfully!"
         log_success "${log_msg}"
     else
@@ -416,10 +426,10 @@ backup_binary() {
     fi
 }
 
-readarray -t lines < $HostsFile
-for HR in "${lines[@]}"; do
-while IFS=$' ' read -a HR ; do
-[[ -z ${HR[0]} ]] && continue
+readarray -t lines < "$HostsFile"
+for line in "${lines[@]}"; do
+read -r -a HR <<< "${line}"
+[[ -z "${HR[0]}" || "${HR[0]}" =~ ^# ]] && continue
 
 DStamp=$(date '+%Y%m%d')
 DTStamp=$(date '+%Y%m%d%H%M%S')
@@ -452,7 +462,7 @@ 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!"
@@ -464,10 +474,8 @@ log_info "${log_msg}"
 precedent_config=${BaseDir}"Precedent-Config.rsc"
 current_config=${TempDir}"Config.rsc"
 diff_config=${TempDir}"Diff-Config.rsc"
-bin_tmp=${TempDir}"Binary.backup"
-
-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..."
@@ -480,7 +488,7 @@ log_info "${log_msg}"
 
 if compare_config ${current_config} ${precedent_config} ${diff_config} ${RouterName} ${LogFile}; then
     BackupDir=${BaseDir}"${DStamp}/"
-    ${CMD_MKD} ${BackupDir}
+    "${CMD_MKD}" -p "${BackupDir}"
 
     save_config=${BackupDir}"${DTStamp}-Config.rsc"
     save_backup=${BackupDir}"${DTStamp}-Binary.backup"
@@ -488,26 +496,26 @@ if compare_config ${current_config} ${precedent_config} ${diff_config} ${RouterN
     log_msg="- Copy ${current_config} file to ${BackupDir} directory..."
     log_info "${log_msg}"
 
-    cp ${current_config} ${save_config}
+    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 ${BackupDir}/${save_config} and ${precedent_config} success."
     log_success "${log_msg}"
 
-    backup_binary ${bin_tmp} ${backup_pass}
+    backup_binary "${save_backup}" "${backup_pass}"
 
     cd "${BaseDir}"
 
     CurrentHistoryDir=${HistoryDir}"${DStamp}/"
-    ${CMD_MKD} -p ${CurrentHistoryDir}
+    "${CMD_MKD}" -p "${CurrentHistoryDir}"
     diff_config_history=${CurrentHistoryDir}"${DTStamp}-Diff-Config.rsc"
     log_msg="- Move ${diff_config} file to ${CurrentHistoryDir}."
     log_info "${log_msg}"
 
-    if ${CMD_MV} -f ${diff_config} ${diff_config_history}; then
+    if ${CMD_MV} -f "${diff_config}" "${diff_config_history}"; then
         log_msg="- Move ${diff_config} file to ${CurrentHistoryDir} complete succesfully!"
         log_success "${log_msg}"
     else
@@ -548,14 +556,13 @@ fi
 log_msg="- Remove Temp directory."
 log_info "${log_msg}"
 
-${CMD_RM} -d ${TempDir}
+"${CMD_RM}" -d "${TempDir}"
 log_msg="- backup complete!"
 log_success "${log_msg}"
 
-done <<<"$(echo -e $HR)"
+done
 LogFile="${DEFAULT_LOG}"
 RouterName=""
-done
 
 if [ -t 1 ] && $DEBUG_OUT; then
     section " DONE "