diff --git a/scripts/_common.sh b/scripts/_common.sh index 82260a3..065a01e 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,25 +1,29 @@ #!/bin/bash -#================================================= -# BACKUP #================================================= -HUMAN_SIZE () { # Transforme une taille en Ko en une taille lisible pour un humain - human=$(numfmt --to=iec --from-unit=1K $1) - echo $human + +# EXEC_LOGIN_AS Helper + +# Execute a command as another user with login +# (hence in user home dir, with prior loading of .profile, etc.) +# usage: exec_login_as USER COMMAND [ARG ...] +exec_login_as() { + local user=$1 + shift 1 + exec_as $user --login "$@" } +# Execute a command as another user +# usage: exec_as USER COMMAND [ARG ...] +exec_as() { + local user=$1 + shift 1 -CHECK_SIZE () { # Vérifie avant chaque backup que l'espace est suffisant - file_to_analyse=$1 - backup_size=$(du --summarize "$file_to_analyse" | cut -f1) - free_space=$(df --output=avail "/home/yunohost.backup" | sed 1d) - - if [ $free_space -le $backup_size ] - then - ynh_print_err "Espace insuffisant pour sauvegarder $file_to_analyse." - ynh_print_err "Espace disponible: $(HUMAN_SIZE $free_space)" - ynh_die "Espace nécessaire: $(HUMAN_SIZE $backup_size)" - fi + if [[ $user = $(whoami) ]]; then + eval "$@" + else + sudo -u "$user" "$@" + fi } #=================================================