]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Finishing bin/nfs-clean-homedirs
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 18 Mar 2019 13:47:05 +0000 (14:47 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 18 Mar 2019 13:47:05 +0000 (14:47 +0100)
bin/nfs-clean-homedirs
lib/functions.rc

index c33f92b2dbe5bea27deb8793182acccc91e75fe5..a5c0425cb968f137f0197bbd063e66c22cb92141 100755 (executable)
@@ -9,6 +9,115 @@ BIN_DIR=$( dirname "${MY_REAL_NAME}" )
 BASE_DIR=$( dirname "${BIN_DIR}" )
 LIB_DIR="${BASE_DIR}/lib"
 CONF_DIR="${BASE_DIR}/etc"
+OLD_HOMES_DIR="_old_homes"
+
+EXCLUDE_DIRS="regina
+noemi
+elmar
+miriam
+nick
+stefanie
+esther
+juliane
+claudia
+anja
+ellen
+justus
+kira
+oliva
+anja.o
+erika
+mailbox
+barbara
+nicole
+alexandra
+lost+found
+patches
+TT_DB
+stuttgart
+momamemberships
+faxserver
+amdraht
+MM
+creative_vp1
+creative_vp2
+creative_vp3
+issybase
+hohes-c
+eventure
+surfpool
+db1n
+qbic
+test-ftd-de
+s+s
+conradmail
+CPAN
+newspool
+bms
+netuser
+ipunkt
+hamburg
+superagent
+cyberline
+slowtiger
+rsync
+db1n1
+db1n2
+unit2
+conrad
+sekt
+db2n2
+concheck
+oestereich
+teamkonzept
+work
+sachsenhausen
+scimone
+con_rep_2
+goodbye
+usenet
+cegame
+info
+newshabitat
+wodef
+barmermesse
+services
+Christina.Hoschka
+ahrens
+bachem
+bartels
+baumgarten
+brandis
+dietsch
+diezmann
+fisahn
+gamer
+hanusch
+head
+johnson
+keating
+kleffmann
+kotra
+kueppers
+kuppi
+luciana
+schade
+stehle
+steinbach
+tautenhahn
+thuerwaechter
+vorst
+CFO
+CEO
+_old_homes
+alf
+ceo
+idm
+info_visioplant
+pp.user
+pts.user
+COO
+Investorrelations"
 
 if [[ -f "${LIB_DIR}/functions.rc" ]] ; then
     . "${LIB_DIR}/functions.rc"
@@ -27,7 +136,7 @@ detect_color
 
 DESCRIPTION=$( cat <<-EOF
        Removes orphaned NFS home directories under ${CYAN}${NFS_HOMEDIR_PARENT}${NORMAL}
-       and archives them under ${CYAN}${NFS_HOMEDIR_PARENT}/_old_homes${NORMAL}.
+       and archives them under ${CYAN}${NFS_HOMEDIR_PARENT}/${OLD_HOMES_DIR}${NORMAL}.
        EOF
 )
 
@@ -94,10 +203,92 @@ get_options() {
 
 }
 
+#------------------------------------------------------------------------------
+check_dir() {
+
+    local hdir="${1}"
+    local bname=$( basename "${hdir}" )
+    local ex=
+    local skip="n"
+
+    debug "Checking directory '${CYAN}${NFS_HOMEDIRS}/${hdir}${NORMAL}' ..."
+
+    local owner=$( stat --format="%U" "${hdir}" )
+    if [[ "${owner}" != 'UNKNOWN' ]] ; then
+        debug "Owner: '${owner}'"
+        return
+    fi
+
+    for ex in $( echo "${EXCLUDE_DIRS}" | egrep -v "^[         ]*(#|$)" | sed -e 's/^[         ]*//' -e 's/[   ]*$//' ) ; do
+        if [[ "${bname}" == "${ex}" ]] ; then
+            skip="y"
+            break
+        fi
+    done
+    if [[ "${skip}" == "y" ]] ; then
+        info "Skipping '${CYAN}${NFS_HOMEDIRS}/${hdir}${NORMAL}'."
+        return
+    fi
+
+    info "Directory '${CYAN}${NFS_HOMEDIRS}/${hdir}${NORMAL}' is orphaned, cleaning it up"
+    ls -ld "${hdir}"
+    du -sm "${hdir}"
+
+    local j=0
+    local tarfile="${NFS_HOMEDIRS}/${OLD_HOMES_DIR}/$bname.$j.tar.gz"
+    while [[ -e "${tarfile}" ]] ; do
+        j=$(( $j + 1 ))
+        tarfile="${NFS_HOMEDIRS}/${OLD_HOMES_DIR}/$bname.$j.tar.gz"
+    done
+    debug "Creating tarfile: '${CYAN}${tarfile}${NORMAL}'"
+    local cmd="tar cfz \"${tarfile}\" \"${hdir}\""
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd="tar cfzv \"${tarfile}\" \"${hdir}\""
+    fi
+    debug "Executing: ${cmd}"
+    if [[ "${SIMULATE}" != "y" ]] ; then
+        eval ${cmd}
+        ls -l "${tarfile}"
+    else
+        debug "Tarfile '${tarfile}' not created."
+    fi
+
+    info "Removing directory '${CYAN}${NFS_HOMEDIRS}/${hdir}${NORMAL}' ..."
+    RM --recursive "${hdir}"
+
+}
+
+#------------------------------------------------------------------------------
+do_cleanup() {
+
+    cd "${NFS_HOMEDIRS}"
+
+    local dir=
+    local oifs="${IFS}"
+    IFS="
+"
+
+    for dir in $( ls -1 -U ) ; do
+        if [[ ! -d "${dir}" ]] ; then
+            continue
+        fi
+        if [[ "${dir}" == "${OLD_HOMES_DIR}" ]] ; then
+            continue
+        fi
+        check_dir "${dir}"
+    done
+    IFS="${oifs}"
+
+}
+
 #------------------------------------------------------------------------------
 main() {
 
     get_options "$@"
+    set_locale "en_US.utf8"
+    info "Starting cleanup homedirs ..."
+    do_cleanup
+    info "Finished cleanup homedirs."
 
 }
 
index f6048780d70a965be8871514f47517318b2befcb..9d8ea38b7107d8d46e9215a91a3631d05f89541e 100644 (file)
@@ -10,11 +10,12 @@ BLUE=""
 CYAN=""
 NORMAL=""
 
-VERSION="0.1.0"
+VERSION="0.2.0"
 
-STD_SHORT_OPTIONS="dvhV"
-STD_LONG_OPTIONS="debug,verbose,nocolor,help,version"
+STD_SHORT_OPTIONS="sdvhV"
+STD_LONG_OPTIONS="simulate,debug,verbose,nocolor,help,version"
 STD_USAGE_MSG=$( cat <<-EOF
+            -s|--simulate   Simulation mode - dont apply any changes.
                -d|--debug      Debug output (bash -x).
                -v|--verbose    Set verbosity on.
                --nocolor       Dont use colors on display.
@@ -88,6 +89,10 @@ eval_common_options() {
 
     while true ; do
         case "$1" in
+            -s|--simulate)
+                SIMULATE="y"
+                shift
+                ;;
             -d|--debug)
                 DEBUG="y"
                 shift
@@ -135,7 +140,13 @@ eval_common_options() {
             shift
         done
     fi
-        
+
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        echo
+        echo -e "${CYAN}Simulation mode!${NORMAL}"
+        echo "Nothing is really done."
+        echo
+    fi
 
 }
 
@@ -167,4 +178,58 @@ error() {
     echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $@" >&2
 }
 
+#------------------------------------------------------------------------------
+RM() {
+
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        debug "Simulated removing of: $*"
+        return
+    fi
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        rm "$@"
+    else
+        rm --verbose "$@"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+set_locale() {
+
+    local new_locale="$1"
+    local loc=
+    local found="n"
+
+    local oifs="${IFS}"
+    IFS="
+"
+    for loc in $( locale -a ); do
+        if [[ "${loc}" == "${new_locale}" ]] ; then
+            found="y"
+            break
+        fi
+    done
+    IFS="${oifs}"
+
+    if [[ "${found}" != "y" ]] ; then
+        error "Locale '${RED}${new_locale}${NORMAL}' not found."
+    else
+        LANG="${new_locale}"
+        LC_ALL=
+        LC_CTYPE="${new_locale}"
+        LC_NUMERIC="${new_locale}"
+        LC_TIME="${new_locale}"
+        LC_COLLATE="${new_locale}"
+        LC_MONETARY="${new_locale}"
+        LC_MESSAGES="${new_locale}"
+        LC_PAPER="${new_locale}"
+        LC_NAME="${new_locale}"
+        LC_ADDRESS="${new_locale}"
+        LC_TELEPHONE="${new_locale}"
+        LC_MEASUREMENT="${new_locale}"
+        LC_IDENTIFICATION="${new_locale}"
+    fi
+
+}
+
 # vim: filetype=sh ts=4 et