2018-04-25 11:14:31 +02:00
#!/bin/bash
2018-04-25 11:22:07 +02:00
# Automatisation de configuration backuppc pour poste MAC
2018-04-25 11:14:31 +02:00
# Institut de Physique de Rennes UMR6251
2018-04-25 11:22:07 +02:00
# Jérémy GARDAIS, Guillaume RAFFY — Avril 2018
2018-04-25 11:14:31 +02:00
SUCCESS = 0
ERROR = 1
2018-04-25 15:39:24 +02:00
# Functions {{{
2018-04-25 16:03:23 +02:00
## GetDefaultUser {{{
function GetDefaultUser( ) {
2018-04-25 11:14:31 +02:00
for user in $( ListUsers) ; do
echo $user
return
done
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 11:14:31 +02:00
2018-04-25 16:03:23 +02:00
## AllowUserToConnectToThisMachineUsingSsh {{{
2018-04-25 11:14:31 +02:00
# this performs the equivalent as adding a remote login user in system preferences using the gui
2018-04-25 16:03:23 +02:00
function AllowUserToConnectToThisMachineUsingSsh( )
2018-04-25 11:14:31 +02:00
{
local userLogin = " $1 "
2018-04-25 11:22:07 +02:00
#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)
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' " DEBUG : Autoriser les accès SSH pour ${ userLogin } . "
2018-04-25 11:14:31 +02:00
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 11:14:31 +02:00
2018-04-25 16:03:23 +02:00
## EnsurePingIsAllowed {{{
function EnsurePingIsAllowed( )
2018-04-25 11:14:31 +02:00
{
2018-04-25 11:22:07 +02:00
#sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setstealthmode off
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' "DEBUG : Désactivation du mode furtif (ping)."
2018-04-25 11:14:31 +02:00
if [ $? != 0 ] ; then
2018-04-25 16:31:54 +02:00
printf '\e[1;31m%-6s\e[m\n' "ERREUR : La désactivation du mode furtif a échoué"
return " $ERROR "
2018-04-25 11:14:31 +02:00
fi
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 11:14:31 +02:00
2018-04-25 16:03:23 +02:00
## EnsureSshdIsRunning {{{
function EnsureSshdIsRunning( )
2018-04-25 11:14:31 +02:00
{
2018-04-25 11:22:07 +02:00
#sudo launchctl list | grep 'com.openssh.sshd' &> /dev/null
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' "DEBUG : Vérification si sshd est déjà en cours."
2018-04-25 11:14:31 +02:00
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
2018-04-25 11:22:07 +02:00
#sudo launchctl list | grep 'com.openssh.sshd' &> /dev/null
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' "DEBUG : Activation du serveur ssh."
2018-04-25 11:14:31 +02:00
if [ $? != 0 ] ; then
2018-04-25 16:03:23 +02:00
printf '\e[1;31m%-6s\e[m\n' "ERREUR : L'activation du serveur ssh a échoué" ;
return " $ERROR "
2018-04-25 11:14:31 +02:00
fi
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 11:14:31 +02:00
2018-04-25 16:03:23 +02:00
## GetMyHostKey {{{
function GetMyHostKey( ) {
2018-04-25 11:14:31 +02:00
hostkey = "/etc/ssh_host_rsa_key.pub"
if [ ! -f " ${ hostkey } " ] ; then
hostkey = "/etc/ssh/ssh_host_rsa_key.pub"
if [ ! -f " ${ hostkey } " ] ; then
2018-04-25 16:03:23 +02:00
printf '\e[1;31m%-6s\e[m\n' "ERREUR : Impossible de trouver la clef ssh publique ce cette machine, SSH est-il bien installé?\n Installation annulée."
2018-04-25 11:14:31 +02:00
return " $ERROR "
fi
fi
echo " ${ hostkey } "
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 11:14:31 +02:00
2018-04-25 15:39:24 +02:00
## IpAddress {{{
2018-04-25 11:14:31 +02:00
function IpAddress( ) {
local strMyIpAddress = ''
local strOsName = $( uname )
2018-04-25 11:40:23 +02:00
strMyIpAddress = $( dig +short myip.opendns.com @resolver1.opendns.com)
2018-04-25 11:14:31 +02:00
if [ " $strMyIpAddress " = = '' ] ; then
error "failed to retrieve the ip address of this machine"
return 1
fi
echo $strMyIpAddress
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 11:14:31 +02:00
2018-04-25 15:39:24 +02:00
## MyFqdn {{{
2018-04-25 11:14:31 +02:00
function MyFqdn( ) {
local strMyIpAddress = $( IpAddress ) # eg '129.20.27.49'
local strMyFqdn = $( host $strMyIpAddress | awk '{print $5}' )
echo ${ strMyFqdn %? } # remove the trailing '.'
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 11:14:31 +02:00
2018-04-25 15:39:24 +02:00
## ListUsers {{{
2018-04-25 11:14:31 +02:00
function ListUsers( ) {
local users = ''
for user in $( ls -d /Users/[ a-zA-Z] *) ; do
user = $( basename $user )
case " $user " in
'Shared' | 'admin' )
; ;
*)
users = " $users $user "
; ;
esac
done
echo $users
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 11:14:31 +02:00
2018-04-25 15:39:24 +02:00
## AddUserBackuppc {{{
2018-04-25 15:31:14 +02:00
function AddUserBackuppc( ) {
local userToBackup = " $1 " # the login of the user to backup
local homeDir = " $2 " # par exemple '/var/lib/backuppc'
local userLogin = 'backuppc'
local groupId = $( id -g $userToBackup )
maxid = $( dscl . -list /Users UniqueID | awk '$2 < 1000 {print $2}' | sort -ug | tail -1)
newid = $(( maxid+1))
mkdir -p " $homeDir "
id " $newid " & > /dev/null
if [ $? = 0 ] ; then
echo " unable to find a suitable uid for user backuppc ( $newid is already used) "
exit $ERROR
fi
# Create user
#dscl . -create "/Users/$userLogin"
#dscl . -create "/Users/$userLogin" UserShell /bin/bash
#dscl . -create "/Users/$userLogin" RealName "backuppc"
#dscl . -create "/Users/$userLogin" UniqueID "$newid"
#dscl . -create "/Users/$userLogin" PrimaryGroupID "$groupId"
#dscl . -create "/Users/$userLogin" NFSHomeDirectory "$homeDir"
#dscl . -create "/Users/$userLogin" IsHidden 1 # hide from login window
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' " DEBUG : Création de l'utilisateur ${ userLogin } . "
2018-04-25 15:31:14 +02:00
# Homedir permissions
#chown -R "$userLogin:$groupId" "$homeDir"
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' " DEBUG : chown -R $userLogin : $groupId $homeDir "
2018-04-25 15:31:14 +02:00
2018-04-25 16:03:23 +02:00
AllowUserToConnectToThisMachineUsingSsh " ${ userLogin } "
2018-04-25 15:31:14 +02:00
AllowBackuppcSudo
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 15:31:14 +02:00
2018-04-25 15:39:24 +02:00
## AllowBackuppcSudo {{{
2018-04-25 15:31:14 +02:00
function AllowBackuppcSudo( ) {
# Get the configuration directory for sudoers
if [ -f /etc/sudoers ] ; then
local sudoersDir = $( grep "^#includedir " /etc/sudoers | cut -d" " -f2)
elif [ -f /private/etc/sudoers ] ; then
local sudoersDir = $( grep "^#includedir " /private/etc/sudoers | cut -d" " -f2)
else
2018-04-25 16:31:54 +02:00
printf '\e[1;31m%-6s\e[m\n' "ERREUR : Impossible de trouver le fichier de configuratio de sudo."
2018-04-25 15:31:14 +02:00
return " $ERROR "
fi
#sudo mkdir -p -- "${sudoersDir}"
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' " DEBUG : Création du dossier ${ sudoersDir } . "
2018-04-25 15:31:14 +02:00
# Allow user to use rsync with sudo
#sudo sh -c "echo '${userLogin} ALL=(ALL:ALL) NOEXEC:NOPASSWD: /usr/bin/rsync' > ${sudoersDir}/backuppc_noexec"
2018-04-25 16:31:54 +02:00
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. "
2018-04-25 15:31:14 +02:00
}
2018-04-25 15:39:24 +02:00
## }}}
2018-04-25 15:31:14 +02:00
# }}}
2018-04-25 11:14:31 +02:00
#### VÉRIFIER QUE L’ ON A BIEN LES DROITS ADMIN ####
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Vérification des droits admin"
2018-04-25 11:14:31 +02:00
if [ " $EUID " -ne 0 ] ; then
2018-04-25 16:03:23 +02:00
printf '\e[1;31m%-6s\e[m\n' "ERREUR : À lancer avec les droits administrateur "
2018-04-25 11:14:31 +02:00
exit
fi
#### NOM DU COMPTE À SAUVEGARDER ####
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Nom du compte à sauvegarder"
2018-04-25 16:03:23 +02:00
default_user = $( GetDefaultUser)
2018-04-25 16:31:54 +02:00
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 } ] : "
2018-04-25 11:14:31 +02:00
read input_login
if [ [ ${ input_login } != "" ] ] ; then
usr = " ${ input_login } "
else
usr = " ${ default_user } "
fi
#### VÉRIFIER QUE LE COMPTE EST BIEN DANS LA LISTE DES USER ID ####
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Vérification du compte"
2018-04-25 11:14:31 +02:00
if ! id " ${ usr } " & > /dev/null; then
2018-04-25 16:31:54 +02:00
printf '\e[1;34m%-6s\e[m' " ${ usr } n’ apparait pas dans la liste des user ids. Continuer tout de même ? [o/N] : "
2018-04-25 11:14:31 +02:00
read input_continue
if [ [ " ${ input_continue } " != "o" ] ] ; then
2018-04-25 16:03:23 +02:00
printf '\e[1;31m%-6s\e[m\n' "Installation annulée."
2018-04-25 11:14:31 +02:00
exit
fi
fi
#### DOSSIER À SAUVEGARDER ####
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Dossier à sauvegarder"
2018-04-25 11:14:31 +02:00
default_dir = $( eval echo ~" ${ usr } " )
if [ ! -d " ${ default_dir } " ] ; then
default_dir = ""
fi
2018-04-25 16:31:54 +02:00
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 } ] : "
2018-04-25 11:14:31 +02:00
read input_dir
if [ [ " ${ input_dir } " = = "" ] ] ; then
dir1 = " ${ default_dir } "
else
dir1 = " ${ input_dir } "
fi
#### DOSSIER À SAUVEGARDER INTROUVABLE, ANNULATION ####
if [ ! -d " ${ dir1 } " ] ; then
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Dossier introuvable"
2018-04-25 16:03:23 +02:00
printf '\e[1;31m%-6s\e[m\n' " ERREUR : Dossier ${ dir1 } introuvable, installation annulée. "
2018-04-25 11:14:31 +02:00
exit
fi
#### DOSSIER SUPPLÉMENTAIRE ####
2018-04-25 16:31:54 +02:00
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. [] : "
2018-04-25 11:14:31 +02:00
read input_dir2
if [ [ " ${ input_dir2 } " != "" ] ] ; then
#### DOSSIER SUPPLÉMENTAIRE INTROUVABLE, ANNULATION DE CELUI-CI ####
if [ ! -d " ${ input_dir2 } " ] ; then
printf "\n"
2018-04-25 16:31:54 +02:00
printf "%b\n" "Dossier supplémentaire introuvable, non ajouté."
2018-04-25 11:14:31 +02:00
else
directories = " ' ${ dir1 } ',' ${ input_dir2 } ' "
fi
else
directories = " ' ${ dir1 } ' "
fi
#### AUTORISER LE PING ####
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Configuration du poste"
2018-04-25 16:03:23 +02:00
EnsurePingIsAllowed
2018-04-25 11:14:31 +02:00
#### INSTALLATION DE OPENSSH-SERVER ####
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Installation de Openssh-server"
2018-04-25 16:03:23 +02:00
EnsureSshdIsRunning
2018-04-25 11:14:31 +02:00
if [ " $? " != " $SUCCESS " ] ; then
2018-04-25 16:31:54 +02:00
printf '\e[1;31m%-6s\e[m\n' "ERREUR : L'installation du serveur ssh a échoué, installation annulée."
exit
2018-04-25 11:14:31 +02:00
fi
2018-04-25 16:03:23 +02:00
hostkey = $( GetMyHostKey)
2018-04-25 11:14:31 +02:00
#printf "hostkey=$hostkey"
if [ " $? " != " $SUCCESS " ] ; then
2018-04-25 16:31:54 +02:00
printf '\e[1;31m%-6s\e[m\n' "ERREUR : Clé inaccessible, merci de contacter votre administrateur réseau, installation annulée."
exit
2018-04-25 11:14:31 +02:00
fi
#### CRÉATION DU FICHIER DE CONFIGURATION ####
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Création du fichier de configuration"
2018-04-25 11:14:31 +02:00
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 } "
2018-04-25 16:41:08 +02:00
printf '\e[1;33m%-6s\e[m\n' " DEBUG : Fichier de configuration créé ( ${ filepl } ) "
2018-04-25 11:14:31 +02:00
#### LE SERVEUR DOIT CONNAITRE ####
2018-04-25 16:41:08 +02:00
# Les commandes à exécuter avec sudo
2018-04-25 11:14:31 +02:00
# ssh_host_ecdsa_key.pub DU CLIENT
# L’ ADRESSE MAIL
# L'IP
# Le nom d'utilisateur
2018-04-25 16:31:54 +02:00
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
2018-04-25 16:41:08 +02:00
echo "\$Conf{RsyncClientCmd} = '\$sshPath -q -x -l backuppc \$host sudo \$rsyncPath \$argList+';" >> " ${ filepl } "
echo "\$Conf{RsyncClientRestoreCmd} = '\$sshPath -q -x -l backuppc \$host sudo \$rsyncPath \$argList+';" >> " ${ filepl } "
2018-04-25 11:14:31 +02:00
2018-04-25 16:41:08 +02:00
echo " # host: ${ fqdn } " >> " ${ filepl } "
2018-04-25 11:14:31 +02:00
cmd_hostkey = $( cat " ${ hostkey } " )
echo " # hostkey: ${ cmd_hostkey } " >> " ${ filepl } "
echo " # mail: ${ input_mail } " >> " ${ filepl } "
echo " # ip: $( IpAddress) " >> " ${ filepl } "
#### Déplacer le fichier dans le dossier de l'utilisateur (plus accessible)
2018-04-25 16:31:54 +02:00
mv -f -- " ${ filepl } " " ${ dir1 } "
2018-04-25 11:14:31 +02:00
2018-04-25 15:48:47 +02:00
#### CRÉATION DE L’ UTILISATEUR BACKUPPC ####
2018-04-25 16:31:54 +02:00
printf '\e[1;35m%-6s\e[m\n' "DEBUG : Partie - Création du compte backuppc"
2018-04-25 15:48:47 +02:00
homebackuppc = '/var/lib/backuppc'
AddUserBackuppc " ${ input_login } " " ${ homebackuppc } "
#mkdir -p -- "${homebackuppc}"/.ssh
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' " DEBUG : Création du répertoire .ssh de l'utilisateur : ${ homebackuppc } /.ssh "
2018-04-25 15:48:47 +02:00
#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
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' "DEBUG : Ajout de la clef SSH du serveur dans " ${ homebackuppc } "/.ssh/authorized_keys."
2018-04-25 15:48:47 +02:00
#chown -R backuppc "${homebackuppc}"/.ssh/
2018-04-25 16:31:54 +02:00
printf '\e[1;33m%-6s\e[m\n' " DEBUG : chown -R backuppc ${ homebackuppc } /.ssh/ "
2018-04-25 11:14:31 +02:00
#### END ####
2018-04-25 16:31:54 +02:00
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"
2018-04-25 16:03:23 +02:00
exit 0