#!/usr/bin/env bash # Remove a user from the server # Copyright 2021-2023 Bryan C. Roessler parent="${BASH_SOURCE[0]}" parent=${parent%/*} [[ -f $parent/script-functions ]] && . "$parent"/script-functions || exit 1 is_root echo "This script supports one optional argument, a username" if [[ $# -eq 1 ]]; then user="$1" else prompt user fi if ! id -u "$user" &>/dev/null; then echo "User $user does not exist" exit 1 fi ask_ok "Remove user $user?" || exit 1 killall -u "$user" if ask_ok "Backup /home/$user?" && [[ -d /home/$user ]]; then if [[ ! -d /mnt/array/home-retired/$user ]]; then btrfs subvolume create /mnt/array/home-retired/"$user" || exit 1 fi rsync -av /home/"$user"/ /mnt/array/home-retired/"$user" || exit 1 fi smbpasswd -x "$user" sed -i "/$user/d" /etc/subuid sed -i "/$user/d" /etc/subgid if ! userdel -fr "$user"; then ask_ok "Userdel failed, kill processes again and retry?" || exit 1 killall -u "$user" -s SIGKILL userdel -fr "$user" fi exit $?