From bc653258c95394382b83bd04c3844e3e0eaa9097 Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 2 Apr 2018 17:51:11 +0200 Subject: [PATCH] Reorganise Repositiry. --- .gitignore | 2 + .../check_samba_rights.sh | 0 OLD/cleanup_samba_rights.sh | 29 +++ clean_samba_trash.sh | 180 ++++++++++++++++++ clean_trash.sh | 34 ---- cleanup_samba_rights.sh | 6 + conf/clean_samba_trash.conf.sample | 20 ++ 7 files changed, 237 insertions(+), 34 deletions(-) rename check_samba_rights.sh => OLD/check_samba_rights.sh (100%) create mode 100755 OLD/cleanup_samba_rights.sh create mode 100755 clean_samba_trash.sh delete mode 100755 clean_trash.sh create mode 100644 conf/clean_samba_trash.conf.sample diff --git a/.gitignore b/.gitignore index a149b6f..a3b58e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ +*.swp +/conf/*.conf /BAK/* diff --git a/check_samba_rights.sh b/OLD/check_samba_rights.sh similarity index 100% rename from check_samba_rights.sh rename to OLD/check_samba_rights.sh diff --git a/OLD/cleanup_samba_rights.sh b/OLD/cleanup_samba_rights.sh new file mode 100755 index 0000000..e0aede1 --- /dev/null +++ b/OLD/cleanup_samba_rights.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +base_dir=$1 +group=$2 + +file_perm=${3:-664} +dir_perm=${4:-2775} + +chmod $dir_perm $base_dir +chgrp $group $base_dir + +while IFS='' read -r -d '' filename ; do + + [[ -f "$filename" ]] && ( chmod $file_perm "$filename"; chgrp $group "$filename") + [[ ! -d "$filename" ]] && continue + + [[ "$filename" =~ .Trash ]] && continue + [[ "$filename" = "lost+found" ]] && continue + + chmod $dir_perm "$filename" ; chgrp $group "$filename" + + find "$filename" ! -group $group -exec chgrp -R $group {} \; + find "$filename" -type d ! -perm $dir_perm -exec chmod -R $dir_perm {} \; + find "$filename" -type f ! -perm $file_perm -exec chmod -R $file_perm {} \; + + +done < <(find $base_dir -mindepth 1 -maxdepth 1 -print0) + +exit 0 diff --git a/clean_samba_trash.sh b/clean_samba_trash.sh new file mode 100755 index 0000000..4cda5c7 --- /dev/null +++ b/clean_samba_trash.sh @@ -0,0 +1,180 @@ +#!/usr/bin/env bash + +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/clean_samba_trash.conf" + +LOCK_DIR="/tmp/clean_trash.LOCK" + + +# ------------- +# - Some functions +# ------------- + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + exit $1 +} + +echononl(){ + if $terminal ; then + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n "$*\\c" 1>&2 + else + echo -e -n "$*" 1>&2 + fi + rm /tmp/shprompt$$ + fi +} + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" + echo "" + echo -e " \033[31m\033[1mScript was terminated\033[m!" + else + echo " [ Fatal ]: $*" + echo "" + echo " Script was terminated...." + fi + echo "" + clean_up 1 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ]: $*" + else + echo "[ Error ]: $*" + fi + echo "" +} + +warn (){ + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" + else + echo "[ Warning ]: $*" + fi + echo "" +} +info (){ + + if $terminal ; then + echo "" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" + echo "" + fi +} + +echo_done() { + if $terminal ; then + echo -e "\033[75G[ \033[32mdone\033[m ]" + fi +} +echo_failed(){ + if $terminal && $LOGGING ; then + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal && $LOGGING ; then + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + + +# ------------- +# - Running in a terminal? +# ------------- + +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + +# ------------- +# - Read Configurations from $conf_file +# ------------- + +if [[ ! -f "$conf_file" ]]; then + fatal " Configuration file '$(basename ${conf_file})' not found!" +else + source "$conf_file" +fi + +[[ -z "$(trim $trash_dirs)" ]] && fatal "No directory given!" +[[ -z "$days" ]] && days=31 + + +# ------------- +# - Job is already running? +# ------------- + +# - If job already runs, stop execution.. +# - +if mkdir "$LOCK_DIR" 2> /dev/null ; then + + # - Remove lockdir when the script finishes, or when it receives a signal + # - + trap clean_up SIGHUP SIGINT SIGTERM + +else + + datum="$(date +"%d.%m.%Y %H:%M")" + + msg=" [ Error ]: A previos instance of '$(basename $0)' seems already be running.\n\n Exiting now.." + + error "A previos instance of the script '$(basename $0)' seems already be running." + + exit 1 + +fi + + +traverse() { + + while IFS='' read -r -d '' name ; do + + if [[ -d $name ]] ; then + [[ "$name" = ".." ]] && continue + [[ "$name" = "." ]] && continue + traverse "$name" + fi + + find "$1" -type f -mtime +${days} -exec rm -f "{}" \; > /dev/null 2>&1 + + done < <(find "$1" -mindepth 1 -maxdepth 1 -print0) + +} + +for dir in $trash_dirs ; do + info "Delete files from trash directory \033[1m${dir}\033[m older than \033[1m${days}\033[m days .." + traverse "$dir" +done + +# - Delete empty sub-directories +# - +if $terminal ; then + echo -e "\033[32m\033[1m-----\033[m" +fi +for dir in $trash_dirs ; do + info "Cleanup trash directory \033[1m${dir}\033[m from empty directories .." + find $dir -depth -mindepth 2 -type d -empty -delete +done + +clean_up 0 diff --git a/clean_trash.sh b/clean_trash.sh deleted file mode 100755 index cb1aab8..0000000 --- a/clean_trash.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash - -trash_dirs="/data/buero/.Trash /data/ajur/.Trash /data/scan/.Trash /data/datev/.Trash" -days=31 - -traverse() { - - for file in `ls -a $1` ; do - if [ -d $1/$file ] ; then - [ "$1/$file" = "$1/.." ] && continue - [ "$1/$file" = "$1/." ] && continue - traverse $1/$file - fi - done - - find $1 -type f -mtime +${days} -exec rm -f "{}" \; - - ## - leere Unterverzeichnisse löschen - #find $1 -depth -mindepth 1 -type d -empty -exec rmdir "{}" \; - -} - -for dir in $trash_dirs ; do - traverse "$dir" -done - -## - lösche leere (Unter-)Verzeichnisse -## - -for dir in $trash_dirs ; do - #find $dir -depth -mindepth 2 -type d -empty -exec rmdir "{}" \; - find $dir -depth -mindepth 2 -type d -empty -delete -done - -exit diff --git a/cleanup_samba_rights.sh b/cleanup_samba_rights.sh index e0aede1..c3f571f 100755 --- a/cleanup_samba_rights.sh +++ b/cleanup_samba_rights.sh @@ -1,5 +1,11 @@ #!/usr/bin/env bash +echo "" +echo " Moved to git repository 'admin-stuff'. Scriptname: set_directory_permissions.sh" +echo "" + +exit 0 + base_dir=$1 group=$2 diff --git a/conf/clean_samba_trash.conf.sample b/conf/clean_samba_trash.conf.sample new file mode 100644 index 0000000..28f25f4 --- /dev/null +++ b/conf/clean_samba_trash.conf.sample @@ -0,0 +1,20 @@ +# ------------------------------------ +# - Settings for script clean_trash.sh +# ------------------------------------ + +# - days +# - +# - Files older then 'days' will be deleted. +# - +# - Defaults to: days=31 +# - +#days=31 + +# - trash_dirs +# - +# - Directories where files older than given days will be deleted. +# - +# - Example: +# - trash_dirs="/data/samba/transfer/.Trash /data/samba/no-backup-share/multimedia/.Trash" +# - +#trash_dirs=""