[fix] Prepend backup dir to relative path only and allow absolute in ynh_backup

This commit is contained in:
Jérôme Lebleu 2016-05-08 00:26:07 +02:00
parent 36a8a2d278
commit 403b1d71a3

View file

@ -3,7 +3,7 @@ CAN_BIND=${CAN_BIND:-1}
# Mark a file or a directory for backup # Mark a file or a directory for backup
# Note: currently, SRCPATH will be copied or binded to DESTPATH # Note: currently, SRCPATH will be copied or binded to DESTPATH
# #
# usage: ynh_backup srcdir destdir can_bind as_root # usage: ynh_backup srcdir destdir to_bind no_root
# | arg: srcdir - directory to bind or copy # | arg: srcdir - directory to bind or copy
# | arg: destdir - mountpoint or destination directory # | arg: destdir - mountpoint or destination directory
# | arg: to_bind - 1 to bind mounting the directory if possible # | arg: to_bind - 1 to bind mounting the directory if possible
@ -20,14 +20,10 @@ ynh_backup() {
echo "Source path '${DESTPATH}' does not exist" >&2 echo "Source path '${DESTPATH}' does not exist" >&2
return 1 return 1
} }
[[ "${DESTPATH:0:1}" = "/" ]] && {
echo "Destination path should be relative" >&2
return 1
}
# prepend the backup directory # prepend the backup directory
[[ -z "${YNH_APP_BACKUP_DIR}" ]] || \ [[ -n "${YNH_APP_BACKUP_DIR}" && "${DESTPATH:0:1}" != "/" ]] \
DESTPATH="${YNH_APP_BACKUP_DIR}/${DESTPATH}" && DESTPATH="${YNH_APP_BACKUP_DIR}/${DESTPATH}"
[[ ! -e "${DESTPATH}" ]] || { [[ ! -e "${DESTPATH}" ]] || {
echo "Destination path '${DESTPATH}' already exist" >&2 echo "Destination path '${DESTPATH}' already exist" >&2
return 1 return 1
@ -39,7 +35,7 @@ ynh_backup() {
if sudo mount --rbind "${SRCPATH}" "${DESTPATH}"; then if sudo mount --rbind "${SRCPATH}" "${DESTPATH}"; then
# try to remount destination directory as read-only # try to remount destination directory as read-only
sudo mount -o remount,ro "${DESTPATH}" \ sudo mount -o remount,ro "${DESTPATH}" >/dev/null 2>&1 \
|| sudo mount -o remount,ro,bind "${SRCPATH}" "${DESTPATH}" \ || sudo mount -o remount,ro,bind "${SRCPATH}" "${DESTPATH}" \
|| true || true
return 0 return 0