Created
May 9, 2025 01:46
-
-
Save xyzulu/da152bdb963bf6ba9e0481406dc50417 to your computer and use it in GitHub Desktop.
Enhance backup server - recreate user home folders (after wiping/replacing the backup disk/mount)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
declare -a users_to_create | |
echo "Scanning for users with missing home directories..." | |
while IFS=: read -r username _ uid gid _ homedir shell; do | |
# Ignore system users (UID < 1000), but include UID 0 (root) | |
# Also skip 'nobody' (UID 65534) and users with non-functional home directories | |
if [[ ("$uid" -ge 1000 || "$uid" -eq 0) && "$homedir" != "/nonexistent" && "$uid" -ne 65534 ]]; then | |
if [[ ! -d "$homedir" ]]; then | |
users_to_create+=("$username:$uid:$gid:$homedir") | |
fi | |
fi | |
done < /etc/passwd | |
missing_count=${#users_to_create[@]} | |
if [[ $missing_count -eq 0 ]]; then | |
echo "All user home directories are present. No action needed." | |
exit 0 | |
fi | |
echo "$missing_count home director$( [[ $missing_count -eq 1 ]] && echo 'y' || echo 'ies' ) missing:" | |
for entry in "${users_to_create[@]}"; do | |
IFS=: read -r username uid gid homedir <<< "$entry" | |
echo " - User: $username (UID: $uid, GID: $gid) -> $homedir" | |
done | |
read -p "Proceed with creating these directories and setting ownership? (y/N): " confirm | |
if [[ "$confirm" =~ ^[Yy]$ ]]; then | |
for entry in "${users_to_create[@]}"; do | |
IFS=: read -r username uid gid homedir <<< "$entry" | |
echo "Creating $homedir for $username..." | |
mkdir -p "$homedir" | |
chown "$uid:$gid" "$homedir" | |
done | |
echo "All missing home directories have been created and ownership set." | |
else | |
echo "No changes made." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment