Change backup handling.

This commit is contained in:
Christoph 2017-08-04 13:27:11 +02:00
parent 9211cb8970
commit 568a481ddf
4 changed files with 132 additions and 23 deletions

View File

@ -448,13 +448,16 @@ while IFS='' read -r _line || [[ -n $_line ]] ; do
fi fi
done < "$ZONES_DECLARATION_FILE" 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 [[ -d "$ZONE_FILE_MASTER_DIR" ]] ; then
if [[ $? -eq 0 ]]; then echononl "\tBackup directory '${ZONE_FILE_MASTER_DIR}'.."
echo_ok cp -a "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1
else if [[ $? -eq 0 ]]; then
echo_failed echo_ok
fatal "$(cat $log_file)" else
echo_failed
fatal "$(cat $log_file)"
fi
fi fi

View File

@ -290,6 +290,7 @@ if $LOGGING ; then
fi fi
[[ -n "$DEFAULT_ZONES_DECLARATION_FILE" ]] || DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" [[ -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 "" echo ""
@ -354,6 +355,7 @@ else # if $LOGGING
fi fi
[[ -n "$ZONES_DECLARATION_FILE" ]] || ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" [[ -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 fi
@ -376,6 +378,18 @@ else
echo_ok echo_ok
fi 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 declare -i line_number=0
_found=false _found=false
@ -479,6 +493,18 @@ if [[ -n "$key_directory" ]]; then
$LOGGING && echo "" $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" _dir="$(dirname ${key_directory})/DELETED"
echononl " Create directory '${_dir}'.." echononl " Create directory '${_dir}'.."
if [[ ! -d "${_dir}" ]] ; then if [[ ! -d "${_dir}" ]] ; then
@ -530,6 +556,54 @@ if [[ -n "$key_directory" ]]; then
fi fi
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 "" $LOGGING && echo ""
echononl " Reeload bind configuration" echononl " Reeload bind configuration"

View File

@ -42,6 +42,8 @@ conf_file="${working_dir}/conf/bind.conf"
log_file="$(mktemp)" log_file="$(mktemp)"
backup_date="$(date +%Y-%m-%d-%H%M)"
#--------------------------------------- #---------------------------------------
#----------------------------- #-----------------------------
@ -273,7 +275,7 @@ $verbose && echo ""
# - Validate Syntax of given domain # - 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]))*$" 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 =~ $valid_domain_regex ]]; then
if [[ ! $host_name =~ \. ]]; then if [[ ! $host_name =~ \. ]]; then
echo_failed echo_failed
@ -346,14 +348,14 @@ fi
zone_file_dir=`dirname $zone_file` 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 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 if [[ $? -eq 0 ]]; then
echo_ok echo_ok
else else
echo_failed echo_failed
error "Backup directory 'zone_file_dir' containg zonefiles failed!" error "$(cat $log_file)"
clean_up 99 clean_up 99
fi fi
else else
@ -365,7 +367,7 @@ fi
# - Determin new serial # - Determin new serial
# - # -
echononl "\tDetermin new serial.." echononl "\t Determin new serial.."
_failed=false _failed=false
declare -i serial_new=`date +%Y%m%d01` declare -i serial_new=`date +%Y%m%d01`
serial_cur=`grep -e "^\s*[0-9]\{10\}" $zone_file | grep serial | awk '{print$1}'` 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 # - 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 perl -i -n -p -e "s#^(\s*)\s$serial_cur(.*)#\1 $serial_new\2#" $zone_file > /dev/null 2>&1
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo_ok echo_ok
@ -400,7 +402,7 @@ else
fi fi
$verbose && echo "" $verbose && echo ""
echononl "\tCorrect Owner for $zone_file .." echononl "\t Correct Owner for $zone_file .."
chown $BIND_USER:$BIND_GROUP $zone_file chown $BIND_USER:$BIND_GROUP $zone_file
if [[ $? -eq 0 ]] ; then if [[ $? -eq 0 ]] ; then
echo_ok echo_ok
@ -408,7 +410,7 @@ else
echo_failed echo_failed
clean_up 99 clean_up 99
fi fi
echononl "\tCorrect permissions on $zone_file .." echononl "\t Correct permissions on $zone_file .."
chmod 644 $zone_file chmod 644 $zone_file
if [[ $? -eq 0 ]] ; then if [[ $? -eq 0 ]] ; then
echo_ok echo_ok
@ -418,6 +420,22 @@ else
fi 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 # - Reload Zone
# - # -
echononl "\tReloading zone \"$domain\".." echononl "\tReloading zone \"$domain\".."

View File

@ -42,6 +42,8 @@ conf_file="${working_dir}/conf/bind.conf"
log_file="$(mktemp)" log_file="$(mktemp)"
backup_date="$(date +%Y-%m-%d-%H%M)"
#--------------------------------------- #---------------------------------------
#----------------------------- #-----------------------------
@ -155,7 +157,7 @@ backup_dir () {
dir_to_backup=$1 dir_to_backup=$1
echononl "\tBackup existing directory \"$dir_to_backup\" .." echononl "\tBackup existing directory \"$dir_to_backup\" .."
if [[ -d "$dir_to_backup" ]] ; then 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 if [[ $? -eq 0 ]]; then
echo_ok echo_ok
else else
@ -233,7 +235,7 @@ fi
info "Given TLSA Record: \n\t\033[1m$@\033[m" 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 if [[ ! -f "$conf_file" ]]; then
echo_skipped echo_skipped
else else
@ -357,8 +359,9 @@ fi
zone_file_dir="$(dirname $zone_file)" 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 # - 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 clean_up 0
else else
_replac_string=${record_arr[@]} _replac_string=${record_arr[@]}
# - Backup Zone directory
backup_dir $zone_file_dir
# - Replace TLSA Record # - Replace TLSA Record
echononl "\tGoing to replace TLSA Record.." echononl "\tGoing to replace TLSA Record.."
@ -433,9 +434,6 @@ else
_tmpfile=`mktemp` _tmpfile=`mktemp`
> $_tmpfile > $_tmpfile
# - backup zone directory
backup_dir $zone_file_dir
# - Add new TLSA record # - Add new TLSA record
echononl "\tAdd new TLSA record to zonefile \"\".." echononl "\tAdd new TLSA record to zonefile \"\".."
while read -r line || [[ -n "$line" ]]; do while read -r line || [[ -n "$line" ]]; do
@ -493,5 +491,21 @@ else
clean_up 2 clean_up 2
fi 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 "" $verbose && echo ""
clean_up 99 clean_up 99