Fix excluding destination dir from removal

Touch can lead to wrong ls order, and destination dir gets selected for
removal.
Use grep -v to exclude, instead of touch.
This commit is contained in:
Darko Poljak 2019-11-25 13:50:30 +01:00
parent 6c24e8a7d3
commit 61ab45fc65

View file

@ -767,20 +767,27 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
_techo "Using ${oldest_bak} for destination dir ${destination_dir}" _techo "Using ${oldest_bak} for destination dir ${destination_dir}"
if mv "${oldest_bak}" "${destination_dir}"; then if mv "${oldest_bak}" "${destination_dir}"; then
# Touch dest dir so it is not sorted wrong in listings below. # Touch dest dir so it is not sorted wrong in listings below.
touch "${destination_dir}" ls_rm_exclude=$(basename "${destination_dir}")
# We have something to remove only if count > interval. # We have something to remove only if count > interval.
remove="$((count - c_interval))" remove="$((count - c_interval))"
else else
_techo_err "Renaming oldest backup ${oldest_bak} to ${destination_dir} failed, removing it." _techo_err "Renaming oldest backup ${oldest_bak} to ${destination_dir} failed, removing it."
remove="$((count - c_interval + 1))" remove="$((count - c_interval + 1))"
ls_rm_exclude=""
fi fi
if [ "${remove}" -gt 0 ]; then if [ "${remove}" -gt 0 ]; then
_techo "Removing ${remove} backup(s)..." _techo "Removing ${remove} backup(s)..."
# shellcheck disable=SC2010 if [ -z "${ls_rm_exclude}" ]; then
ls -${TSORT}1r | grep "^${INTERVAL}\\." | head -n "${remove}" > "${TMP}" || \ # shellcheck disable=SC2010
_exit_err "Listing old backups failed" ls -${TSORT}1r | grep "^${INTERVAL}\\." | head -n "${remove}" > "${TMP}" || \
_exit_err "Listing old backups failed"
else
# shellcheck disable=SC2010
ls -${TSORT}1r | grep -v "${ls_rm_exclude}" | grep "^${INTERVAL}\\." | head -n "${remove}" > "${TMP}" || \
_exit_err "Listing old backups failed"
fi
delete_from_file "${TMP}" & delete_from_file "${TMP}" &
fi fi