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)
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()
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}"
# 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}"
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}"
}
run_ssh_cmd() {
local run="${1}"
warning "${run}"
- if ${SSH_STR} ${run}; then
+ if "${SSH_CMD[@]}" "${run}"; then
return 0
else
return 1
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=$?
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 "
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
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
}
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
# 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"
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=$?
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=$?
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
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')
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!"
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..."
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"
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
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 "