Procházet zdrojové kódy

Merge pull request #1786 from cedarlug/master

relative path using readlink instead of realpath
UltimateByte před 8 roky
rodič
revize
c5f46b073e
1 změnil soubory, kde provedl 59 přidání a 2 odebrání
  1. 59 2
      lgsm/functions/command_backup.sh

+ 59 - 2
lgsm/functions/command_backup.sh

@@ -112,8 +112,16 @@ fn_backup_compression(){
 	sleep 2
 	fn_print_dots "Backup (${rootdirduexbackup}) ${backupname}.tar.gz, in progress..."
 	fn_script_log_info "backup ${rootdirduexbackup} ${backupname}.tar.gz, in progress"
-	excludedir=$(realpath --relative-to="${rootdir}" "${backupdir}")
-	tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" ./*
+        excludedir=$(fn_backup_relpath)
+
+	# CHECK THAT excludedir is a valid path.
+	if [ ! -d "${excludedir}" ] ; then
+		fn_print_info_nl "Problem identifying the previous backup directory for exclusion."
+		fn_script_log_error "Problem identifying the previous backup directory for exclusion"
+		core_exit.sh
+	fi
+
+	tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" --exclude "${tmpdir}/.backup.lock" ./*
 	local exitcode=$?
 	if [ ${exitcode} -ne 0 ]; then
 		fn_print_fail_eol
@@ -180,6 +188,55 @@ fn_backup_prune(){
 	fi
 }
 
+fn_backup_relpath() {
+  	# Written by CedarLUG as a "realpath --relative-to" alternative in bash
+
+	# Populate an array of tokens initialized from the rootdir components:
+  	declare -a rdirtoks=($(readlink -f "${rootdir}" | sed "s/\// /g"))
+
+	if [ ${#rdirtoks[@]} -eq 0 ]; then
+		fn_print_info_nl "Problem assessing rootdir during relative path assessment"
+		fn_script_log_error "Problem assessing rootdir during relative path assessment: ${rootdir}"
+		core_exit.sh
+	fi
+
+	# Populate an array of tokens initialized from the backupdir components:
+  	declare -a bdirtoks=($(readlink -f "${backupdir}" | sed "s/\// /g"))
+	if [ ${#bdirtoks[@]} -eq 0 ]; then
+		fn_print_info_nl "Problem assessing backupdir during relative path assessment"
+		fn_script_log_error "Problem assessing backupdir during relative path assessment: ${rootdir}"
+		core_exit.sh
+	fi
+
+	# Compare the leading entries of each array.  These common elements will be clipped off
+	# for the relative path output.
+  	for ((base=0; base<${#rdirtoks[@]}; base++))
+  	do
+      		[[ "${rdirtoks[$base]}" != "${bdirtoks[$base]}" ]] && break
+  	done
+
+	# Next, climb out of the remaining rootdir location with updir references...
+  	for ((x=base;x<${#rdirtoks[@]};x++))
+  	do
+      		echo -n "../"
+  	done
+
+	# Climb down the remaining components of the backupdir location.
+  	for ((x=base;x<$(( ${#bdirtoks[@]} - 1 ));x++))
+  	do
+      		echo -n "${bdirtoks[$x]}/"
+  	done
+
+	# In the event there were no directories left in the backupdir above to
+	# traverse down, just add a newline.  Otherwise at this point, there is
+	# one remaining directory component in the backupdir to navigate.
+  	if (( "$base" < "${#bdirtoks[@]}" )) ; then
+      		echo "${bdirtoks[ $(( ${#bdirtoks[@]} - 1)) ]}"
+  	else
+      		echo
+  	fi
+}
+
 # Restart the server if it was stopped for the backup
 fn_backup_start_server(){
 	if [ "${serverstopped}" == "yes" ]; then