Files
server-scripts/nc_import_users.sh
2026-01-05 23:35:04 +01:00

51 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
# ========================================================================
# CSV file structure (users.csv):
#
# username,password,display_name,email,group
#
# Example:
# username,password,display_name,email,group
# jsmith,Passw0rd123,John Smith,jsmith@example.com,staff
# amina,Amina!2024,Amina Khan,amina.khan@example.com,teachers
# guest1,Guest123,Guest User,,guests
#
# Notes:
# - The first row MUST be the header
# - email and group fields can be left empty
# - Only ONE group per user (extend script if multiple groups are needed)
# =========================================================================
CSV="users.csv"
NC_CONTAINER="nextcloud-aio-nextcloud"
while IFS=, read -r user pass display email groups; do
# Skip header
if [[ "$user" == "username" ]]; then
continue
fi
echo "➡ Creating user: $user"
# Create user with password
docker exec -e NC_PASS="$pass" -u www-data $NC_CONTAINER \
php occ user:add --password-from-env --display-name="$display" "$user"
# Set email if provided
if [[ -n "$email" ]]; then
docker exec -u www-data $NC_CONTAINER \
php occ user:setting "$user" settings email "$email"
fi
# Add to group if provided
if [[ -n "$groups" ]]; then
docker exec -u www-data $NC_CONTAINER \
php occ group:adduser "$groups" "$user"
fi
done < "$CSV"
echo "✅ Import complete!"