Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Last active October 2, 2023 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CHERTS/0fb77ac61ccde0622ebbf59af1d556f4 to your computer and use it in GitHub Desktop.
Save CHERTS/0fb77ac61ccde0622ebbf59af1d556f4 to your computer and use it in GitHub Desktop.
Changing GID (gid>1000) to system (gid<1000)
#!/bin/bash
#
# Program: Changing GID (gid>1000) to system (gid<1000) <change_group_id.sh>
#
# Author: Mikhail Grigorev <sleuthhound at gmail dot com>
#
# Current Version: 1.0
#
# Revision History:
#
# Version 1.0
# Initial Release
#
# License:
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
GROUP=mysql
DIR_LIST="/etc /var/lib/mysql /var/log/mysql"
SERVICE_NAME_LIST="mysql mysql@test1 mysql@test2"
MULTI_SERVICE_PREFIX=mysql-multi
MULTI_SERVICE_DIR="/etc/mysql-multi"
_service() {
local SERVICE_ACTION=$1
for SRV in ${SERVICE_NAME_LIST}; do
echo "Service '${SRV}', execute '${SERVICE_ACTION}'"
systemctl ${SERVICE_ACTION} ${SRV}
done
}
_multi_service() {
local SERVICE_ACTION=$1
for CLUSTER in $(find ${MULTI_SERVICE_DIR} -type f -iname "*.cnf" -exec basename {} \;); do
local M_SERVICE=${MULTI_SERVICE_PREFIX}@${CLUSTER%.*}
echo "Service '${M_SERVICE}', execute '${SERVICE_ACTION}'"
systemctl ${SERVICE_ACTION} ${M_SERVICE}
done
}
OLD_G_ID=$(getent group | grep ${GROUP} | awk -F':' '{print $3}')
if [ -n "${OLD_G_ID}" ]; then
echo "Group: ${GROUP}, GID: ${OLD_G_ID}"
if [ ${OLD_G_ID} -ge 1000 ]; then
echo "Init changing GID for group '${GROUP}'"
NEW_GID=$(getent group | cut -d ":" -f 3 | grep "^1..$" | sort -n | tail -n 1 | awk '{ print $1+1 }')
echo "Set new GID for group '${GROUP}' = ${NEW_GID}"
read -n 1 -s -r -p "Press any key to run changing GID or press Ctrl+C to abort"
echo ""
_service "stop"
#_multi_service "stop"
echo "Changing GID..."
groupmod -g ${NEW_GID} ${GROUP}
echo "Changing file and directory owner..."
find ${DIR_LIST} -group ${OLD_G_ID} -exec chgrp -h ${GROUP} {} \;
#_multi_service "start"
_service "start"
else
echo "GID of the group '${GROUP}' is less than 1000"
exit 1
fi
else
echo "Group '${GROUP}' not found."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment