From 568a481ddf9fadbaa7a276cac9d9803b0c396376 Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 4 Aug 2017 13:27:11 +0200 Subject: [PATCH] Change backup handling. --- bind_change_SOA.sh | 17 ++++++---- bind_remove_domain.sh | 74 ++++++++++++++++++++++++++++++++++++++++++ bind_set_new_serial.sh | 34 ++++++++++++++----- bind_set_renew_tlsa.sh | 30 ++++++++++++----- 4 files changed, 132 insertions(+), 23 deletions(-) diff --git a/bind_change_SOA.sh b/bind_change_SOA.sh index 939faae..e5a4862 100755 --- a/bind_change_SOA.sh +++ b/bind_change_SOA.sh @@ -448,13 +448,16 @@ while IFS='' read -r _line || [[ -n $_line ]] ; do fi done < "$ZONES_DECLARATION_FILE" -echononl "\tBackup directory '${ZONE_FILE_MASTER_DIR}'.." -cp -a "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1 -if [[ $? -eq 0 ]]; then - echo_ok -else - echo_failed - fatal "$(cat $log_file)" + +if [[ -d "$ZONE_FILE_MASTER_DIR" ]] ; then + echononl "\tBackup directory '${ZONE_FILE_MASTER_DIR}'.." + cp -a "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fatal "$(cat $log_file)" + fi fi diff --git a/bind_remove_domain.sh b/bind_remove_domain.sh index 242dc2f..fada8b9 100755 --- a/bind_remove_domain.sh +++ b/bind_remove_domain.sh @@ -290,6 +290,7 @@ if $LOGGING ; then fi [[ -n "$DEFAULT_ZONES_DECLARATION_FILE" ]] || DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" + [[ -n "$ZONE_FILE_MASTER_DIR" ]] || ZONE_FILE_MASTER_DIR="${CONF_FILE_DIR}/master" echo "" @@ -354,6 +355,7 @@ else # if $LOGGING fi [[ -n "$ZONES_DECLARATION_FILE" ]] || ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" + [[ -n "$ZONE_FILE_MASTER_DIR" ]] || ZONE_FILE_MASTER_DIR="${CONF_FILE_DIR}/master" fi @@ -376,6 +378,18 @@ else echo_ok fi +if [[ -d "$ZONE_FILE_MASTER_DIR" ]] ; then + echononl " Backup directory '${ZONE_FILE_MASTER_DIR}'.." + cp -a "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fatal "$(cat $log_file)" + fi +fi + + declare -i line_number=0 _found=false @@ -479,6 +493,18 @@ if [[ -n "$key_directory" ]]; then $LOGGING && echo "" + if [[ "$(dirname ${key_directory})" != "$CONF_FILE_DIR" ]]; then + backup_key_dir="$(dirname ${key_directory})" + echononl " Backup directory '${backup_key_dir}'.." + cp -a "${backup_key_dir}" "${backup_key_dir}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fatal "$(cat $log_file)" + fi + fi + _dir="$(dirname ${key_directory})/DELETED" echononl " Create directory '${_dir}'.." if [[ ! -d "${_dir}" ]] ; then @@ -530,6 +556,54 @@ if [[ -n "$key_directory" ]]; then fi fi + +if [[ -f "${ZONES_DECLARATION_FILE}.$backup_date" ]]; then + diff "$ZONES_DECLARATION_FILE" "${ZONES_DECLARATION_FILE}.$backup_date" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + info "Zone declaration file $(basename $ZONES_DECLARATION_FILE) has not changed.\n\t Removing previously created backup now." + echononl "\tDelete '${ZONES_DECLARATION_FILE}.$backup_date'.." + rm "${ZONES_DECLARATION_FILE}.$backup_date" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fi + fi +fi + + +if [[ -d "${ZONE_FILE_MASTER_DIR}.${backup_date}" ]] ; then + diff -Nur "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + info "No zone file has changed.\n\t Removing previously created backup." + echononl "\tDelete '${ZONE_FILE_MASTER_DIR}.${backup_date}'.." + rm -rf "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + fi + fi +fi + +if [[ -d "${backup_key_dir}.${backup_date}" ]] ; then + diff -Nur "${backup_key_dir}" "${backup_key_dir}.${backup_date}" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + info "Key directory '${backup_key_dir}' has not changed.\n\t Removing previously created backup now." + echononl "\tDelete '${backup_key_dir}.${backup_date}'.." + rm -rf "${backup_key_dir}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + fi + fi +fi + + + $LOGGING && echo "" echononl " Reeload bind configuration" diff --git a/bind_set_new_serial.sh b/bind_set_new_serial.sh index a64d720..4c7290f 100755 --- a/bind_set_new_serial.sh +++ b/bind_set_new_serial.sh @@ -42,6 +42,8 @@ conf_file="${working_dir}/conf/bind.conf" log_file="$(mktemp)" +backup_date="$(date +%Y-%m-%d-%H%M)" + #--------------------------------------- #----------------------------- @@ -273,7 +275,7 @@ $verbose && echo "" # - Validate Syntax of given domain # - valid_domain_regex="^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$" -echononl "\tValidate syntax of given domain.." +echononl "\t Validate syntax of given domain.." if [[ $host_name =~ $valid_domain_regex ]]; then if [[ ! $host_name =~ \. ]]; then echo_failed @@ -346,14 +348,14 @@ fi zone_file_dir=`dirname $zone_file` -echononl "\tBackup existing directory containg zonefiles.." +echononl "\t Backup existing directory containg zonefiles.." if [[ -d "$zone_file_dir" ]] ; then - cp -a $zone_file_dir ${zone_file_dir}.BAK.`date +%Y-%m-%d-%H%M` + cp -a "$zone_file_dir" "${zone_file_dir}.$backup_date" > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed - error "Backup directory 'zone_file_dir' containg zonefiles failed!" + error "$(cat $log_file)" clean_up 99 fi else @@ -365,7 +367,7 @@ fi # - Determin new serial # - -echononl "\tDetermin new serial.." +echononl "\t Determin new serial.." _failed=false declare -i serial_new=`date +%Y%m%d01` serial_cur=`grep -e "^\s*[0-9]\{10\}" $zone_file | grep serial | awk '{print$1}'` @@ -389,7 +391,7 @@ fi # - Replace serial with the new one # - -echononl "\tIncrease serial for zone file \"`basename $zone_file`\".." +echononl "\t Increase serial for zone file \"`basename $zone_file`\".." perl -i -n -p -e "s#^(\s*)\s$serial_cur(.*)#\1 $serial_new\2#" $zone_file > /dev/null 2>&1 if [[ $? -eq 0 ]]; then echo_ok @@ -400,7 +402,7 @@ else fi $verbose && echo "" -echononl "\tCorrect Owner for $zone_file .." +echononl "\t Correct Owner for $zone_file .." chown $BIND_USER:$BIND_GROUP $zone_file if [[ $? -eq 0 ]] ; then echo_ok @@ -408,7 +410,7 @@ else echo_failed clean_up 99 fi -echononl "\tCorrect permissions on $zone_file .." +echononl "\t Correct permissions on $zone_file .." chmod 644 $zone_file if [[ $? -eq 0 ]] ; then echo_ok @@ -418,6 +420,22 @@ else fi +if [[ -d "${zone_file_dir}.$backup_date" ]] ; then + diff -Nur "$zone_file_dir" "${zone_file_dir}.$backup_date" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + info "No zone file has changed.\n\t Removing previously created backup." + echononl "\t Delete '${zone_file_dir}.$backup_date'.." + rm -rf "${zone_file_dir}.$backup_date" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fi + fi +fi + +$verbose && echo "" + # - Reload Zone # - echononl "\tReloading zone \"$domain\".." diff --git a/bind_set_renew_tlsa.sh b/bind_set_renew_tlsa.sh index 8e39fbb..5b6fe7b 100755 --- a/bind_set_renew_tlsa.sh +++ b/bind_set_renew_tlsa.sh @@ -42,6 +42,8 @@ conf_file="${working_dir}/conf/bind.conf" log_file="$(mktemp)" +backup_date="$(date +%Y-%m-%d-%H%M)" + #--------------------------------------- #----------------------------- @@ -155,7 +157,7 @@ backup_dir () { dir_to_backup=$1 echononl "\tBackup existing directory \"$dir_to_backup\" .." if [[ -d "$dir_to_backup" ]] ; then - cp -a $dir_to_backup ${dir_to_backup}.BAK.`date +%Y-%m-%d-%H%M` + cp -a "$dir_to_backup" "${dir_to_backup}.$backup_date" > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -233,7 +235,7 @@ fi info "Given TLSA Record: \n\t\033[1m$@\033[m" -echononl "\t Loading default Configuration values from $(basename ${conf_file}).." +echononl "\tLoading default Configuration values from $(basename ${conf_file}).." if [[ ! -f "$conf_file" ]]; then echo_skipped else @@ -357,8 +359,9 @@ fi zone_file_dir="$(dirname $zone_file)" -# - Backup existing zone file directory +# - Backup zone directory # - +backup_dir $zone_file_dir # - Update/Add TLSA recotd if needed @@ -374,8 +377,6 @@ if grep -E "^$record_name.+$record_type" $zone_file > /dev/null 2>&1 ; then clean_up 0 else _replac_string=${record_arr[@]} - # - Backup Zone directory - backup_dir $zone_file_dir # - Replace TLSA Record echononl "\tGoing to replace TLSA Record.." @@ -433,9 +434,6 @@ else _tmpfile=`mktemp` > $_tmpfile - # - backup zone directory - backup_dir $zone_file_dir - # - Add new TLSA record echononl "\tAdd new TLSA record to zonefile \"\".." while read -r line || [[ -n "$line" ]]; do @@ -493,5 +491,21 @@ else clean_up 2 fi + + +if [[ -d "${zone_file_dir}.$backup_date" ]] ; then + diff -Nur "$zone_file_dir" "${zone_file_dir}.$backup_date" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + info "No zone file has changed.\n\t Removing previously created backup." + echononl "\t Delete '${zone_file_dir}.$backup_date'.." + rm -rf "${zone_file_dir}.$backup_date" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fi + fi +fi + $verbose && echo "" clean_up 99