smb_utilities/remove_share.sh
2025-05-18 23:20:48 +00:00

38 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# Script to remove a Samba share
# Usage: ./remove_samba_share.sh <share_name>
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <share_name>"
exit 1
fi
SHARE_NAME="$1"
SHARE_CONFIG_DIR="/etc/samba/shares.d"
SMBCONF="/etc/samba/smb.conf"
SHARE_CONFIG_FILE="$SHARE_CONFIG_DIR/$SHARE_NAME.conf"
# 1. Remove share config file
if [ -f "$SHARE_CONFIG_FILE" ]; then
echo "Removing share config: $SHARE_CONFIG_FILE"
rm -f "$SHARE_CONFIG_FILE"
else
echo "Warning: Share config file does not exist: $SHARE_CONFIG_FILE"
fi
# 2. Remove include line from smb.conf
INCLUDE_LINE="include = $SHARE_CONFIG_FILE"
if grep -Fxq "$INCLUDE_LINE" "$SMBCONF"; then
echo "Removing include line from $SMBCONF"
# Remove both the comment and the include line if present
sed -i "/# Include share definition for $SHARE_NAME/d" "$SMBCONF"
sed -i "\|$INCLUDE_LINE|d" "$SMBCONF"
else
echo "Include line not found in $SMBCONF"
fi
# 3. Restart Samba to apply changes
echo "Restarting Samba service..."
systemctl restart smbd && echo "Samba restarted successfully." || echo "Failed to restart Samba. Check logs."