Skip to content

Instantly share code, notes, and snippets.

@rustymyers
Last active July 5, 2017 17:26
Show Gist options
  • Save rustymyers/6f43829df8ba346c3975e12f47eba43a to your computer and use it in GitHub Desktop.
Save rustymyers/6f43829df8ba346c3975e12f47eba43a to your computer and use it in GitHub Desktop.
#!/bin/bash
#1.4
#--------------------------------------------------------------------------------------------------
#-- psuDeleteHomeDirs
#--------------------------------------------------------------------------------------------------
# Program : psuDeleteHomeDirs
# To Complie : n/a
#
# Purpose : Delete home directories
#
# Called By :
# Calls :
#
# Author : Rusty Myers <[email protected]>
# Based Upon :
#
# Note :
#
# Revisions :
# 2016-05-25 <rzm102> Initial Version
# 2017-06-30 <rzm102> Adding secure erase to home folders and running with fork
# 2017-07-05 <rzm102> Adding new path for temp homes being delete
#
#--------------------------------------------------------------------------------------------------
lastmod () {
last_mod_seconds=$(echo $(( $(date +%s) - $(stat -f%c "$user") )))
}
help () {
echo "Use $0 -t [hours] to call script with custom hour value or use defaults to set for system:"
echo "defaults write /Library/Preferences/edu.psu.deletehomes.plist delete_time 15"
echo "Use $0 -i [user] to call script with custom home dir to ignore or use defaults to set for system:"
echo "defaults write /Library/Preferences/edu.psu.deletehomes.plist ignored_users -array rzm102 jde6"
echo "Leave both previous options blank to use defaults"
echo "$0 -t 0 -i \" help dude test \""
echo "use -s to secure erase home folders"
echo "use -b to run erase in the background"
}
# internal default value
delete_home_hours=48
last_mod_seconds=0
background="false"
# path to users
user_path="/Users"
# list of ignored directories
ignored_folders=( "Shared" "psuguest" "Guest" )
# get plist values
preference_file="/Library/Preferences/edu.psu.deletehomes.plist"
if [[ -e "$preference_file" ]]; then
# get home dir delete hours from plist
delete_home_hours_pref=$(defaults read "$preference_file" "delete_time")
echo "Found pref delete time: $delete_home_hours_pref"
# set delete home hours to results of preference, or the defaut value if it fails
delete_home_hours=${delete_home_hours_pref:-$delete_home_hours}
# get ignored users into a bash style array
ignored_homes_pref=$(echo $(defaults read "$preference_file" "ignored_users" | tr -d \(\), | tr '\n' ' '))
echo "Found pref ignored homes: $ignored_homes_pref"
fi
# get arguments
while getopts t:i:sb opt; do
case "$opt" in
t) delete_home_hours="${OPTARG}";;
i) ignored_folders=( ${ignored_folders[@]} "$OPTARG" );;
s) secure="P";;
b) background="true";;
h)
help
exit 0;;
\?)
help
exit 0;;
esac
done
shift `expr $OPTIND - 1`
echo "Using the delete home directory time of $delete_home_hours hours"
if [[ "$delete_home_hours" < 0 ]]; then
echo "Received time to ignore deleting homes. Exiting..."
exit 0
fi
# turn hours to seconds
delete_home_seconds=$(( delete_home_hours * 60 * 60 ))
echo "Deleting home directories after $delete_home_seconds seconds"
ignored_folders=( ${ignored_folders[@]} ${ignored_homes_pref[@]} )
echo "Ignoring these homes: ${ignored_folders[@]}"
# List users on the computer
for user in "$user_path"/*;
do
# echo "Checking $user : $(basename "$user")"
ignore=0
for excluded in "${ignored_folders[@]}"
do
# echo "Excluded: $excluded"
if [[ "$excluded" == "$(basename "$user")" ]]; then
ignore=1
fi
done
if (( $ignore )); then
echo "--Ignoring $user"
else
echo "++Checking age of $user"
# check age of folders
lastmod "$user"
echo "---> Age of $user = $last_mod_seconds"
if [[ $last_mod_seconds -gt $delete_home_seconds ]]; then
shortUserPath=$(basename "$user")
# move home to temp location
tempLocation="/Library/CLMadmin/CLMtemp/DeleteHomes/"
mkdir -p "$tempLocation"
echo "---> Moving Home $user to $tempLocation$shortUserPath"
mv "$user" "$tempLocation$shortUserPath"
echo "---> Deleting Home Folder $tempLocation$shortUserPath"
if [[ "$background" == "true" ]]; then
nohup rm -Rf"$secure" "$tempLocation$shortUserPath" &
else
nohup rm -Rf"$secure" "$tempLocation$shortUserPath"
fi
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment