From 698688f998899f40c8fce934361233d6a07118a0 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 15 Feb 2019 04:24:20 +0000 Subject: [PATCH] etc/cron/nb --- bin/nb-install | 331 ++++++++++++++++++++++++++-------------- etc/cron/nb | 9 +- etc/profile.d/functions | 105 ------------- etc/profile.d/ldap.sh | 4 - 4 files changed, 222 insertions(+), 227 deletions(-) diff --git a/bin/nb-install b/bin/nb-install index 6c0bfd77..14f98265 100755 --- a/bin/nb-install +++ b/bin/nb-install @@ -5,119 +5,224 @@ # ############################################################################## main() { - # NB 13.12.16 . "${BASH_SOURCE%/*}/../etc/profile" || exit - . $(dirname "$0")/../etc/profile || exit 1; [ -z "$NB_ROOT" ] && exit - - # Export all - ( - shopt -s expand_aliases - export -f $(typeset -f | awk '/^[a-z]+[a-zA-Z0-9_]+ \(\)/{print $1}') || return 1 - ) >/dev/null 2>&1 - - NAME="$(basename "${0}")" - TMP=/tmp/$NAME.$$ - INSTALL="rsync --archive --checksum --force" - BY="Generated by $0" - - if [ "$UID" != "0" ] ; then - echo "$NAME: please login as root." 1>&2 - exit 1 - fi - - ############################################################################## - # - # Args - # - ############################################################################## - case "$@" in *-h*|*-help*) exec echo "Usage: $NAME [-v] [-n]";; esac - case "$@" in *-v*) INSTALL="$INSTALL -v";; esac - case "$@" in *-n*) INSTALL="$INSTALL -n";; esac - if ! which rsync >/dev/null; then - INSTALL="cp -a" - case "$@" in *-v*) INSTALL="$INSTALL -v";; esac - case "$@" in *-n*) INSTALL="echo DRY:";; esac - fi - - ############################################################################## - # - # Functions - # - ############################################################################## - verbose() { - [ -z "$@" ] && return - printf '> %s\n' "$@" - } - - fdiff() { - [ -e "$1" ] || return 0 - [ -e "$2" ] || return 0 - [ -n "$(diff -q $1 $2 2>/dev/null)" ] && return 0 - return 1 - } - - ############################################################################## - # - # Cron - # - if [ -e /etc/nb/cron.no ]; then - rm -f /etc/cron.d/nb /var/log/nb.log - else - #shell_help_noarg "zaza" "$@" && exit 0 - cron_d_install "$NB_ROOT/etc/cron/nb" "nb-update|$NB_ROOT|NB_ROOT|NB_LOG" && verbose "Cron: $NB_ROOT/etc/cron/nb" - #cron_d_install "test" "" "nico.*true" && verbose "Cron: nico" - fi - - # - # Profile - # - echo "[ -r $NB_ROOT/etc/profile ] && . $NB_ROOT/etc/profile" > $TMP.profile - if [ -d /etc/profile.d ]; then - - if fdiff $TMP.profile /etc/profile.d/nb.sh 1>/dev/null 2>&1; then - verbose "Install /etc/profile.d/nb.sh" - $INSTALL $TMP.profile /etc/profile.d/nb.sh - chmod 755 /etc/profile.d/nb.sh - fi - - elif [ -w /etc/profile ]; then - - grep -vF "$NB_ROOT/etc/profile.sh" /etc/profile > $TMP.clean - if fdiff /etc/profile $TMP.clean; then - verbose "Clean /etc/profile" - cat $TMP.clean > /etc/profile - fi - - if ! grep -qm1 -F "$(cat $TMP.profile)" /etc/profile; then - verbose "Install /etc/profile" - cat $TMP.profile >> /etc/profile - fi - - else - echo "Can't install /etc/profile. Directory not exists or wrong permission" 1>&2 - - fi - - # - # Log - # - NB_USER=`(ls -dl $NB_ROOT 2>/dev/null || printf '- 0 root') | awk '{print $3}'` - verbose $(chown "$NB_USER" /var/log/nb.log 2> /dev/null) - - # - # Git hooks - # - cd "$NB_ROOT" || exit - - if [ -w .git/hooks -a -r etc/git/hooks/pre-commit ]; then - fdiff etc/git/hooks/pre-commit .git/hooks/pre-commit >/dev/null 2>&1 \ - && cp -va etc/git/hooks/pre-commit .git/hooks/ - fi - - # - # Bye - # - rm -f $TMP* - exit 0 + # NB 13.12.16 . "${BASH_SOURCE%/*}/../etc/profile" || exit + . $(dirname "$0")/../etc/profile || exit 1; [ -z "$NB_ROOT" ] && exit + + # Export all + ( + shopt -s expand_aliases + export -f $(typeset -f | awk '/^[a-z]+[a-zA-Z0-9_]+ \(\)/{print $1}') || return 1 + ) >/dev/null 2>&1 + + NAME="$(basename "${0}")" + TMP=/tmp/$NAME.$$ + INSTALL="rsync --archive --checksum --force" + BY="Generated by $0" + + if [ "$UID" != "0" ] ; then + echo "$NAME: please login as root." 1>&2 + exit 1 + fi + + ############################################################################## + # + # Args + # + ############################################################################## + case "$@" in *-h*|*-help*) exec echo "Usage: $NAME [-v] [-n]";; esac + case "$@" in *-v*) INSTALL="$INSTALL -v";; esac + case "$@" in *-n*) INSTALL="$INSTALL -n";; esac + if ! which rsync >/dev/null; then + INSTALL="cp -a" + case "$@" in *-v*) INSTALL="$INSTALL -v";; esac + case "$@" in *-n*) INSTALL="echo DRY:";; esac + fi + + ############################################################################## + # + # Functions + # + ############################################################################## + verbose() { + [ -z "$@" ] && return + printf '> %s\n' "$@" + } + + fdiff() { + [ -e "$1" ] || return 0 + [ -e "$2" ] || return 0 + [ -n "$(diff -q $1 $2 2>/dev/null)" ] && return 0 + return 1 + } + + ############################################################################## + # + # Cron + # + if [ -e /etc/nb/cron.no ]; then + rm -f /etc/cron.d/nb /var/log/nb.log + else + #shell_help_noarg "zaza" "$@" && exit 0 + cron_d_install "$NB_ROOT/etc/cron/nb" "nb-update|$NB_ROOT|NB_ROOT|NB_LOG" && verbose "Cron: $NB_ROOT/etc/cron/nb" + #cron_d_install "test" "" "nico.*true" && verbose "Cron: nico" + fi + + # + # Profile + # + echo "[ -r $NB_ROOT/etc/profile ] && . $NB_ROOT/etc/profile" > $TMP.profile + if [ -d /etc/profile.d ]; then + + if fdiff $TMP.profile /etc/profile.d/nb.sh 1>/dev/null 2>&1; then + verbose "Install /etc/profile.d/nb.sh" + $INSTALL $TMP.profile /etc/profile.d/nb.sh + chmod 755 /etc/profile.d/nb.sh + fi + + elif [ -w /etc/profile ]; then + + grep -vF "$NB_ROOT/etc/profile.sh" /etc/profile > $TMP.clean + if fdiff /etc/profile $TMP.clean; then + verbose "Clean /etc/profile" + cat $TMP.clean > /etc/profile + fi + + if ! grep -qm1 -F "$(cat $TMP.profile)" /etc/profile; then + verbose "Install /etc/profile" + cat $TMP.profile >> /etc/profile + fi + + else + echo "Can't install /etc/profile. Directory not exists or wrong permission" 1>&2 + + fi + + # + # Log + # + NB_USER=`(ls -dl $NB_ROOT 2>/dev/null || printf '- 0 root') | awk '{print $3}'` + verbose $(chown "$NB_USER" /var/log/nb.log 2> /dev/null) + + # + # Git hooks + # + cd "$NB_ROOT" || exit + + if [ -w .git/hooks -a -r etc/git/hooks/pre-commit ]; then + fdiff etc/git/hooks/pre-commit .git/hooks/pre-commit >/dev/null 2>&1 \ + && cp -va etc/git/hooks/pre-commit .git/hooks/ + fi + + # + # Bye + # + rm -f $TMP* + exit 0 } + +cron_d_install() { + local usage fname file exp cron tmp test + usage="Usage: $FUNCNAME [-t|--test] [CRON_D_FILE] [IGNORE_REGEXP] or [NAME] [CRON_D_CONTENT] [IGNORE_REGEXP]" + #shell_help_noarg "$usage" "$@" && return 1 + #shell_help "$usage" "$@" && return 1 + [ -z "$*" ] && echo "$usage" && return 1 + if ! perl -e '' 2>/dev/null; then + echo "$FUNCNAME: please install perl" 1>&2 + return 1 + fi + + if [ "$#" -gt 0 ]; then + case "$1" in -t|--test) test=1; shift ;; esac + fi + + #echo "$#"; return + if [ $# -gt 4 -o $# = 0 ]; then + shell_help_noarg "$usage" "--help" && return 1 + + #elif [ $# -eq 3 ]; then + elif [ ! -e "$1" ]; then + fname=$1; shift + tmp="/tmp/$FUNCNAME.$$" + trap "rm -f $tmp*" EXIT + file="$tmp.cron" +# NB 31.08.16 cat <<< "$1" > "$file"; shift + echo "$1" > "$file"; shift + exp=$1; shift + + else + file=$1; shift + exp=$1; shift + fname=$(basename "$file") + + fi + + # + # Case 1 - /etc/cron.d + # + if [ -d /etc/cron.d ]; then + + if [ "$test" = "1" ]; then + printf '%s' "$FUNCNAME: /etc/cron.d: $fname" + [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return + echo ': Not changed' + return 1 + fi + + [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && cp "$file" "/etc/cron.d/$fname" + return + fi + + # + # Case 2 - crontab + # + + cron=$(perl -e ' +$diff = 0; +$e = shift @ARGV; + +@f = map {chomp; $_} grep {!/^(#|\s*$)/} <>; +for ($i=0;$i<@f;$i++) { + @F = split(" ",$f[$i]); + next if $f[$i] !~ /^[\*\d]/ or @F<6; + if ($F[5] ne "root"){ + $_ = join(" ",@F[6..$#F]); + s/"/\\"/g; + @_=qq|su -l "$F[5]" -c "$_"|; + @F = (@F[0..4],"",@_); + }; + $f[$i] = join(" ",@F[0..4,6..$#F]); +} +%f = map {$_=>1} @f; + +@ARGV = ("crontab -l|"); +@c = map {chomp; $_} <>; +%c = map {$_=>1} @c; + +for (@c) { + 0 and print STDERR "-$_\n" if $e and !$f{$_} and /$e/; + $diff=1 and next if $e and !$f{$_} and /$e/; + print "$_\n"; +} + +for (@f) { + $diff=2 and print "$_\n" if !$c{$_}; +} + +exit $diff ? 0 : 1; +' "$exp" "$file" +) + + if [ "$test" = "1" ]; then + printf '%s' "$FUNCNAME: crontab: $fname" + #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n" + [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return + echo ": Not changed; return 1" + fi + +# NB 31.08.16 [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron" + [ "$cron" != "$(crontab -l)" ] && echo "$cron" | crontab + +} + main diff --git a/etc/cron/nb b/etc/cron/nb index 013fd8ba..a8bf6996 100644 --- a/etc/cron/nb +++ b/etc/cron/nb @@ -4,8 +4,6 @@ #LANG=C SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin -# NB 25.05.18 :/opt/nb/bin -#NB_INIT='[ -z "$NB_ROOT" ] || . /etc/profile' ##################################################################### # m h dom mon dow user command @@ -14,6 +12,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 0 00 * * * root . /etc/profile && nb-update -cleanup >/var/log/nb.log 2>&1 | true 0 01-08 * * * root . /etc/profile && nb-update >/var/log/nb.log 2>&1 | true -25 5 * * * root which run-parts &> /dev/null && . /etc/profile && [ -d "$NB_ROOT/etc/cron.daily" ] && cd / && run-parts --report $NB_ROOT/etc/cron.daily -47 5 * * 7 root which run-parts &> /dev/null && . /etc/profile && [ -d "$NB_ROOT/etc/cron.weekly" ] && cd / && run-parts --report $NB_ROOT/etc/cron.weekly -52 5 1 * * root which run-parts &> /dev/null && . /etc/profile && [ -d "$NB_ROOT/etc/cron.monthly" ] && cd / && run-parts --report $NB_ROOT/etc/cron.monthly +25 5 * * * root which run-parts &> /dev/null && . /etc/profile && [ -d "$NB_ROOT" -a -d "$NB_ROOT/etc/cron.daily" ] && cd / && run-parts --report $NB_ROOT/etc/cron.daily +47 4 * * 7 root which run-parts &> /dev/null && . /etc/profile && [ -d "$NB_ROOT" -a -d "$NB_ROOT/etc/cron.weekly" ] && cd / && run-parts --report $NB_ROOT/etc/cron.weekly +52 3 1 * * root which run-parts &> /dev/null && . /etc/profile && [ -d "$NB_ROOT" -a -d "$NB_ROOT/etc/cron.monthly" ] && cd / && run-parts --report $NB_ROOT/etc/cron.monthly +52 1 1 * * root which run-parts &> /dev/null && . /etc/profile && [ -d "$NB_ROOT" -a -d "$NB_ROOT/etc/cron.yearly" ] && cd / && run-parts --report $NB_ROOT/etc/cron.yearly diff --git a/etc/profile.d/functions b/etc/profile.d/functions index dc643bae..39303c5c 100755 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -467,111 +467,6 @@ jpg2avi() { rm -f "$still" } -cron_d_install() { - local usage fname file exp cron tmp test - usage="Usage: $FUNCNAME [-t|--test] [CRON_D_FILE] [IGNORE_REGEXP] or [NAME] [CRON_D_CONTENT] [IGNORE_REGEXP]" - #shell_help_noarg "$usage" "$@" && return 1 - #shell_help "$usage" "$@" && return 1 - [ -z "$*" ] && echo "$usage" && return 1 - if ! perl -e '' 2>/dev/null; then - echo "cron_d_install: please install perl" 1>&2 - return 1 - fi - - if [ "$#" -gt 0 ]; then - case "$1" in -t|--test) test=1; shift ;; esac - fi - - #echo "$#"; return - if [ $# -gt 3 ]; then - shell_help_noarg "$usage" "--help" && return 1 - - #elif [ $# -eq 3 ]; then - elif [ ! -e "$1" ]; then - fname=$1; shift - tmp="/tmp/$FUNCNAME.$$" - trap "rm -f $tmp*" EXIT - file="$tmp.cron" -# NB 31.08.16 cat <<< "$1" > "$file"; shift - echo "$1" > "$file"; shift - exp=$1; shift - - else - file=$1; shift - exp=$1; shift - fname=$(basename "$file") - - fi - - # - # Case 1 - /etc/cron.d - # - if [ -d /etc/cron.d ]; then - - if [ "$test" = "1" ]; then - printf '%s' "$FUNCNAME: /etc/cron.d: $fname" - [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return - echo ': Not changed' - return 1 - fi - - [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && cp "$file" "/etc/cron.d/$fname" - return - fi - - # - # Case 2 - crontab - # - - cron=$( - perl -e ' -$diff = 0; -$e = shift @ARGV; - -@f = map {chomp; $_} grep {!/^(#|\s*$)/} <>; -for ($i=0;$i<@f;$i++) { - @F = split(" ",$f[$i]); - next if $f[$i] !~ /^[\*\d]/ or @F<6; - if ($F[5] ne "root"){ - $_ = join(" ",@F[6..$#F]); - s/"/\\"/g; - @_=qq|su -l "$F[5]" -c "$_"|; - @F = (@F[0..4],"",@_); - }; - $f[$i] = join(" ",@F[0..4,6..$#F]); -} -%f = map {$_=>1} @f; - -@ARGV = ("crontab -l|"); -@c = map {chomp; $_} <>; -%c = map {$_=>1} @c; - -for (@c) { - 0 and print STDERR "-$_\n" if $e and !$f{$_} and /$e/; - $diff=1 and next if $e and !$f{$_} and /$e/; - print "$_\n"; -} - -for (@f) { - $diff=2 and print "$_\n" if !$c{$_}; -} - -exit $diff ? 0 : 1; -' "$exp" "$file" -) - - if [ "$test" = "1" ]; then - printf '%s' "$FUNCNAME: crontab: $fname" - #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n" - [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return - echo ": Not changed; return 1" - fi - -# NB 31.08.16 [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron" - [ "$cron" != "$(crontab -l)" ] && echo "$cron" | crontab - -} - mail_relay_closed() { shell_help "Usage: $FUNCNAME MAIL_SERVER" "$@" && return; local out=$( diff --git a/etc/profile.d/ldap.sh b/etc/profile.d/ldap.sh index bf391fa1..65fd986b 100644 --- a/etc/profile.d/ldap.sh +++ b/etc/profile.d/ldap.sh @@ -160,10 +160,6 @@ ldap_backup() { return 1 fi - ( - echo "ldap_backup: $*" - set - ) 2>&1 | mail -s ldap_backup root slapcat -n 0 -l $dir/$preff-config.ldif slapcat -n 1 -l $dir/$preff-data.ldif gzip $dir/$preff-*.ldif -- 2.47.3