From 4cf4e0dccb45a7d8a6d49c24af6f331024a6974f Mon Sep 17 00:00:00 2001 From: GARDAIS Jeremy Date: Wed, 25 Apr 2018 16:31:54 +0200 Subject: [PATCH] Fix printf colors --- install_backuppc_mac.sh | 83 +++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/install_backuppc_mac.sh b/install_backuppc_mac.sh index 9106e25..4169f61 100755 --- a/install_backuppc_mac.sh +++ b/install_backuppc_mac.sh @@ -23,7 +23,7 @@ function AllowUserToConnectToThisMachineUsingSsh() local userLogin="$1" #dscl . append '/Groups/com.apple.access_ssh' user "${userLogin}" #dscl . append /Groups/com.apple.access_ssh groupmembers $(dscl . read "/Users/${userLogin}" GeneratedUID | cut -d " " -f 2) - printf '\e[1;31m%-6s\e[m\n' "DEBUG : Autoriser les accès SSH pour ${userLogin}." + printf '\e[1;33m%-6s\e[m\n' "DEBUG : Autoriser les accès SSH pour ${userLogin}." } ## }}} @@ -31,9 +31,10 @@ function AllowUserToConnectToThisMachineUsingSsh() function EnsurePingIsAllowed() { #sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setstealthmode off - printf '\e[1;31m%-6s\e[m\n' "DEBUG : Désactivation du mode furtif (ping)." + printf '\e[1;33m%-6s\e[m\n' "DEBUG : Désactivation du mode furtif (ping)." if [ $? != 0 ]; then - printf '\e[1;31m%-6s\e[m\n' "ERREUR : La désactivation du mode furtif a échoué"; return "$ERROR" + printf '\e[1;31m%-6s\e[m\n' "ERREUR : La désactivation du mode furtif a échoué" + return "$ERROR" fi } ## }}} @@ -42,14 +43,14 @@ function EnsurePingIsAllowed() function EnsureSshdIsRunning() { #sudo launchctl list | grep 'com.openssh.sshd' &> /dev/null - printf '\e[1;31m%-6s\e[m\n' "DEBUG : Vérification si sshd est déjà en cours." + printf '\e[1;33m%-6s\e[m\n' "DEBUG : Vérification si sshd est déjà en cours." if [ $? != 0 ]; then # enable 'Remote login' in 'system preferences' sudo launchctl enable system/com.openssh.sshd &> /dev/null sudo launchctl load /System/Library/LaunchDaemons/ssh.plist &> /dev/null fi #sudo launchctl list | grep 'com.openssh.sshd' &> /dev/null - printf '\e[1;31m%-6s\e[m\n' "DEBUG : Activation du serveur ssh." + printf '\e[1;33m%-6s\e[m\n' "DEBUG : Activation du serveur ssh." if [ $? != 0 ]; then printf '\e[1;31m%-6s\e[m\n' "ERREUR : L'activation du serveur ssh a échoué"; return "$ERROR" @@ -132,11 +133,11 @@ function AddUserBackuppc() { #dscl . -create "/Users/$userLogin" PrimaryGroupID "$groupId" #dscl . -create "/Users/$userLogin" NFSHomeDirectory "$homeDir" #dscl . -create "/Users/$userLogin" IsHidden 1 # hide from login window - printf '\e[1;31m%-6s\e[m\n' "DEBUG : Création de l'utilisateur ${userLogin}." + printf '\e[1;33m%-6s\e[m\n' "DEBUG : Création de l'utilisateur ${userLogin}." # Homedir permissions #chown -R "$userLogin:$groupId" "$homeDir" - printf '\e[1;31m%-6s\e[m\n' "DEBUG : chown -R $userLogin:$groupId $homeDir" + printf '\e[1;33m%-6s\e[m\n' "DEBUG : chown -R $userLogin:$groupId $homeDir" AllowUserToConnectToThisMachineUsingSsh "${userLogin}" @@ -152,31 +153,33 @@ function AllowBackuppcSudo() { elif [ -f /private/etc/sudoers ]; then local sudoersDir=$(grep "^#includedir " /private/etc/sudoers | cut -d" " -f2) else - printf '\e[1;31m%-6s\e[m\n' "Unable to find sudo configuration file." + printf '\e[1;31m%-6s\e[m\n' "ERREUR : Impossible de trouver le fichier de configuratio de sudo." return "$ERROR" fi #sudo mkdir -p -- "${sudoersDir}" - printf '\e[1;31m%-6s\e[m\n' "DEBUG : Création du dossier ${sudoersDir}." + printf '\e[1;33m%-6s\e[m\n' "DEBUG : Création du dossier ${sudoersDir}." # Allow user to use rsync with sudo #sudo sh -c "echo '${userLogin} ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync' > ${sudoersDir}/backuppc_noexec" - printf '\e[1;31m%-6s\e[m\n' "DEBUG : ajout de '${userLogin} ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync'" - printf '\e[1;31m%-6s\e[m\n' "DEBUG : dans le fichier ${sudoersDir}/backuppc_noexec." + printf '\e[1;33m%-6s\e[m\n' "DEBUG : ajout de '${userLogin} ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync'" + printf '\e[1;33m%-6s\e[m\n' "DEBUG : dans le fichier ${sudoersDir}/backuppc_noexec." } ## }}} # }}} #### VÉRIFIER QUE L’ON A BIEN LES DROITS ADMIN #### +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Vérification des droits admin" if [ "$EUID" -ne 0 ]; then printf '\e[1;31m%-6s\e[m\n' "ERREUR : À lancer avec les droits administrateur " exit fi #### NOM DU COMPTE À SAUVEGARDER #### +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Nom du compte à sauvegarder" default_user=$(GetDefaultUser) -printf '\e[1;34m%-6s\e[m\n' "liste des comptes détectés sur cette machine : $(ListUsers)" -printf "login de l’utilisateur dont les données sont à sauvegarder ?\n [${default_user}] : " +printf "%b\n" "Liste des comptes détectés sur cette machine : $(ListUsers)" +printf '\e[1;34m%-6s\e[m' "Login de l’utilisateur dont les données sont à sauvegarder ? [${default_user}] : " read input_login if [[ ${input_login} != "" ]]; then usr="${input_login}" @@ -185,8 +188,9 @@ else fi #### VÉRIFIER QUE LE COMPTE EST BIEN DANS LA LISTE DES USER ID #### +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Vérification du compte" if ! id "${usr}" &> /dev/null; then - printf '\e[1;35m%-6s\e[m\n' "${usr} n’apparait pas dans la liste des user ids. Continuer tout de même ? [o/N] : " + printf '\e[1;34m%-6s\e[m' "${usr} n’apparait pas dans la liste des user ids. Continuer tout de même ? [o/N] : " read input_continue if [[ "${input_continue}" != "o" ]]; then printf '\e[1;31m%-6s\e[m\n' "Installation annulée." @@ -195,11 +199,13 @@ if ! id "${usr}" &> /dev/null; then fi #### DOSSIER À SAUVEGARDER #### +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Dossier à sauvegarder" default_dir=$(eval echo ~"${usr}") if [ ! -d "${default_dir}" ]; then default_dir="" fi -printf "Par défaut, le dossier sauvegardé est le home de l’utilisateur. Il est possible d’en ajouter un supplémentaire ensuite. Dossier à sauvegarder ? [${default_dir}] : " +printf "%b\n" "Par défaut, le dossier sauvegardé est le home de l’utilisateur. Il est possible d’en ajouter un supplémentaire ensuite." +printf '\e[1;34m%-6s\e[m' "Dossier à sauvegarder ? [${default_dir}] : " read input_dir if [[ "${input_dir}" == "" ]]; then dir1="${default_dir}" @@ -209,19 +215,20 @@ fi #### DOSSIER À SAUVEGARDER INTROUVABLE, ANNULATION #### if [ ! -d "${dir1}" ]; then - printf "\n" +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Dossier introuvable" printf '\e[1;31m%-6s\e[m\n' "ERREUR : Dossier ${dir1} introuvable, installation annulée." exit fi #### DOSSIER SUPPLÉMENTAIRE #### -printf "Si vous avez un dossier supplémentaire à sauvegarder (/mnt/data par exemple) entrer-le maintenant, sinon laissez vide. [] : " +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Dossier supplémentaire" +printf '\e[1;34m%-6s\e[m' "Si vous avez un dossier supplémentaire à sauvegarder (/mnt/data par exemple) entrer-le maintenant, sinon laissez vide. [] : " read input_dir2 if [[ "${input_dir2}" != "" ]]; then #### DOSSIER SUPPLÉMENTAIRE INTROUVABLE, ANNULATION DE CELUI-CI #### if [ ! -d "${input_dir2}" ]; then printf "\n" - printf '\e[1;35m%-6s\e[m\n' "Dossier supplémentaire introuvable, non ajouté." + printf "%b\n" "Dossier supplémentaire introuvable, non ajouté." else directories="'${dir1}','${input_dir2}'" fi @@ -230,38 +237,43 @@ else fi #### AUTORISER LE PING #### +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Configuration du poste" EnsurePingIsAllowed #### INSTALLATION DE OPENSSH-SERVER #### +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Installation de Openssh-server" EnsureSshdIsRunning if [ "$?" != "$SUCCESS" ]; then - printf '\e[1;31m%-6s\e[m\n' "ERREUR : L'installation du serveur ssh a échoué, installation annulée."; exit + printf '\e[1;31m%-6s\e[m\n' "ERREUR : L'installation du serveur ssh a échoué, installation annulée." + exit fi hostkey=$(GetMyHostKey) #printf "hostkey=$hostkey" if [ "$?" != "$SUCCESS" ]; then - printf '\e[1;31m%-6s\e[m\n' "ERREUR : Clé inaccessible, merci de contacter votre administrateur réseau, installation annulée."; exit + printf '\e[1;31m%-6s\e[m\n' "ERREUR : Clé inaccessible, merci de contacter votre administrateur réseau, installation annulée." + exit fi #### CRÉATION DU FICHIER DE CONFIGURATION #### +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Création du fichier de configuration" fqdn="$(MyFqdn)" filepl="${fqdn}.pl" exclude="['ownCloud','.local/share/Trash','.cache','.Play*','.steam','.wine','Perso','temp','tmp','.Trash*','.DS_Store','._*', '.thumbnails','.ssh/id_*','.xsession-*']" echo "\$Conf{XferMethod} = 'rsync';" > "${filepl}" echo "\$Conf{RsyncShareName} = [${directories}];" >> "${filepl}" echo "\$Conf{BackupFilesExclude} = {'*' => ${exclude} };" >> "${filepl}" -printf "\n" -printf '\e[1;34m%-6s\e[m\n' "Fichier de configuration créé (${filepl})" - -#### ADRESSE MAIL DE L’UTILISATEUR #### -printf "Votre adresse e-mail ?\n : " -read input_mail +printf '\e[1;33m%-6s\e[m\n' "Fichier de configuration créé (${filepl})" #### LE SERVEUR DOIT CONNAITRE #### # ssh_host_ecdsa_key.pub DU CLIENT # L’ADRESSE MAIL # L'IP # Le nom d'utilisateur +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Informations complémentaires dans le fichier de configuration" + +printf '\e[1;34m%-6s\e[m' "Votre adresse e-mail : " +read input_mail + echo "# host:${fqdn}" >> "${filepl}" cmd_hostkey=$(cat "${hostkey}") @@ -271,24 +283,23 @@ echo "# ip:$(IpAddress)" >> "${filepl}" echo "# username:${usr}" >> "${filepl}" #### Déplacer le fichier dans le dossier de l'utilisateur (plus accessible) -mv "${filepl}" "${dir1}" +mv -f -- "${filepl}" "${dir1}" #### CRÉATION DE L’UTILISATEUR BACKUPPC #### -printf '\e[1;34m%-6s\e[m\n' "Création du compte backuppc…" +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Création du compte backuppc" homebackuppc='/var/lib/backuppc' AddUserBackuppc "${input_login}" "${homebackuppc}" #mkdir -p -- "${homebackuppc}"/.ssh -printf '\e[1;31m%-6s\e[m\n' "DEBUG : Création du répertoire .ssh de l'utilisateur : ${homebackuppc}/.ssh" +printf '\e[1;33m%-6s\e[m\n' "DEBUG : Création du répertoire .ssh de l'utilisateur : ${homebackuppc}/.ssh" #echo "from=\"129.20.203.16\" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIhMc8ixQXfWDACJy4q0v8T877UxahhCjO51PQFzylwVpf88LX3yWeDrWIW0NRu0zoSm396mig918OpD5ggqML/QbYbQsoDdAFUV/tK4JU6UJgEQIl25MOcUBCFepsFBGS09CH/V07xSUqSP/+beeTRLNO2CQzk3S2y3YfkXpM7KmOGfeLgoCaQAcxIkgLXeM3TpCZEzJDlZ8c8k/DjVvsgwCpQktYzNo2b37KHLLfgyW9KSo6N9sReUuNQjS6lu8rjrXfc6+J0pY2D6IxWptTWL/JVrhFCUqe4QQy+xYjoR41wqnAQyl/kOcyBNhSvojMKwQT6vlPwru6pOno16/X backuppc@backuppc.ipr.univ-rennes1.fr" > "${homebackuppc}"/.ssh/authorized_keys -printf '\e[1;31m%-6s\e[m\n' "DEBUG : Ajout de la clef SSH du server dans "${homebackuppc}"/.ssh/authorized_keys." +printf '\e[1;33m%-6s\e[m\n' "DEBUG : Ajout de la clef SSH du serveur dans "${homebackuppc}"/.ssh/authorized_keys." #chown -R backuppc "${homebackuppc}"/.ssh/ -printf '\e[1;31m%-6s\e[m\n' "DEBUG : chown -R backuppc ${homebackuppc}/.ssh/" - +printf '\e[1;33m%-6s\e[m\n' "DEBUG : chown -R backuppc ${homebackuppc}/.ssh/" #### END #### -printf "\n" -printf '\e[1;34m%-6s\e[m\n' "Configuration du poste terminée." -printf '\e[1;34m%-6s\e[m\n' "Envoyez bien votre fichier de configuration (${dir1}/${filepl}) à Jérémy GARDAIS (jeremy.gardai@univ-rennes1.fr)." -printf '\e[1;34m%-6s\e[m\n' "Vous pourrez affiner la configuration de votre sauvegarde depuis https://backuppc.ipr.univ-rennes1.fr" +printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - FIN" +printf "%b\n" "Configuration du poste terminée." +printf '\e[1;31m%-6s\e[m\n' "Envoyez bien votre fichier de configuration situé dans votre répertoire personnel (${dir1}/${filepl}) à Jérémy GARDAIS (jeremy.gardai@univ-rennes1.fr)." +printf "%b\n" "Vous pourrez affiner la configuration de votre sauvegarde depuis https://backuppc.ipr.univ-rennes1.fr" exit 0