feat: backup /etc/* when remove

This commit is contained in:
Yingjie Wang 2024-11-18 22:54:18 -05:00
parent a8422c9774
commit 7d2743074b

15
leaf
View File

@ -403,10 +403,21 @@ leaf_remove_package() {
leaf_error "Package ${PKG_PREFIX}/${PKG_NAME} is NOT installed"
}
src_preremove
local _file _link _directory
local _file _link _directory _etc_backup_path _relative_path _backup_etc=false
_etc_backup_path="/etc/.leaf_backup/${PKGNAME}_$(date +%Y%m%d%H%M%S)"
cat "${TRACE_DIR}/${PKG_PREFIX}/${PKG_NAME}/FILES" | while read -r _file; do
rm -fv "${_file}"
if [[ "${_file}" == /etc/* && -f "${_file}" ]]; then
_backup_etc=true
_relative_path="${_file#/etc/}"
mkdir -pv "${_etc_backup_path}/$(dirname "${_relative_path}")"
mv -v "${_file}" "${_etc_backup_path}/${_relative_path}"
else
rm -fv "${file}"
fi
done
if [[ "${_backup_etc}" == true ]]; then
leaf_record_message "Found files in /etc, backuped to ${_etc_backup_path}."
fi
cat "${TRACE_DIR}/${PKG_PREFIX}/${PKG_NAME}/LINKS" | while read -r _link; do
[ ! -L "${_link}" ] || rm -fv "${_link}"
done