mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[enh] Create ynh_backup helper based on ynh_bind_or_cp
This commit is contained in:
parent
26aa1cba37
commit
be9d744f3c
1 changed files with 42 additions and 17 deletions
|
@ -1,27 +1,43 @@
|
||||||
CAN_BIND=${CAN_BIND:-1}
|
CAN_BIND=${CAN_BIND:-1}
|
||||||
|
|
||||||
# Bind a directory or copy it on error
|
# Mark a file or a directory for backup
|
||||||
|
# Note: currently, SRCPATH will be copied or binded to DESTPATH
|
||||||
#
|
#
|
||||||
# usage: ynh_bind_or_cp srcdir destdir as_root
|
# usage: ynh_backup srcdir destdir can_bind as_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: as_root - 1 to execute commands as root
|
# | arg: to_bind - 1 to bind mounting the directory if possible
|
||||||
ynh_bind_or_cp() {
|
# | arg: no_root - 1 to execute commands as current user
|
||||||
local SRCDIR=$1
|
ynh_backup() {
|
||||||
local DESTDIR=$2
|
local SRCPATH=$1
|
||||||
local SUDO_CMD=
|
local DESTPATH=$2
|
||||||
[[ "${3}" = "1" ]] && SUDO_CMD="sudo"
|
local TO_BIND=${3:-0}
|
||||||
|
local SUDO_CMD="sudo"
|
||||||
|
[[ "${4:-}" = "1" ]] && SUDO_CMD=
|
||||||
|
|
||||||
[[ ! -f "${DESTDIR}" ]] || {
|
# validate arguments
|
||||||
echo "Destination directory '${DESTDIR}' already exists" >&2
|
[[ -e "${SRCPATH}" ]] || {
|
||||||
|
echo "Source path '${DESTPATH}' does not exist" >&2
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
[[ "${DESTPATH:0:1}" = "/" ]] && {
|
||||||
|
echo "Destination path should be relative" >&2
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# prepend the backup directory
|
||||||
|
[[ -z "${YNH_APP_BACKUP_DIR}" ]] || \
|
||||||
|
DESTPATH="${YNH_APP_BACKUP_DIR}/${DESTPATH}"
|
||||||
|
[[ ! -e "${DESTPATH}" ]] || {
|
||||||
|
echo "Destination path '${DESTPATH}' already exist" >&2
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# attempt to bind mounting the directory
|
# attempt to bind mounting the directory
|
||||||
if [[ "${CAN_BIND}" = "1" ]]; then
|
if [[ "${CAN_BIND}" = "1" && "${TO_BIND}" = "1" ]]; then
|
||||||
eval $SUDO_CMD mkdir -p "${DESTDIR}"
|
eval $SUDO_CMD mkdir -p "${DESTPATH}"
|
||||||
|
|
||||||
if sudo mount --rbind "${SRCDIR}" "${DESTDIR}"; then
|
if sudo mount --rbind "${SRCPATH}" "${DESTPATH}"; then
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
CAN_BIND=0
|
CAN_BIND=0
|
||||||
|
@ -30,13 +46,22 @@ ynh_bind_or_cp() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# delete mountpoint directory safely
|
# delete mountpoint directory safely
|
||||||
mountpoint -q "${DESTDIR}" && sudo umount -R "${DESTDIR}"
|
mountpoint -q "${DESTPATH}" && sudo umount -R "${DESTPATH}"
|
||||||
eval $SUDO_CMD rm -rf "${DESTDIR}"
|
eval $SUDO_CMD rm -rf "${DESTPATH}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ... or just copy the directory
|
# ... or just copy the directory
|
||||||
eval $SUDO_CMD mkdir -p $(dirname "${DESTDIR}")
|
eval $SUDO_CMD mkdir -p $(dirname "${DESTPATH}")
|
||||||
eval $SUDO_CMD cp -a "${SRCDIR}" "${DESTDIR}"
|
eval $SUDO_CMD cp -a "${SRCPATH}" "${DESTPATH}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Deprecated helper since it's a dangerous one!
|
||||||
|
ynh_bind_or_cp() {
|
||||||
|
local AS_ROOT=${3:-0}
|
||||||
|
local NO_ROOT=0
|
||||||
|
[[ "${AS_ROOT}" = "1" ]] || NO_ROOT=1
|
||||||
|
echo "This helper is deprecated, you should use ynh_backup instead" >&2
|
||||||
|
ynh_backup "$1" "$2" 1 "$NO_ROOT"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create a directory under /tmp
|
# Create a directory under /tmp
|
||||||
|
|
Loading…
Add table
Reference in a new issue