Benutzer-Werkzeuge

Webseiten-Werkzeuge


tachtler:dovecot_backup_-_skript

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
tachtler:dovecot_backup_-_skript [2019/01/30 21:56]
klaus [Skript - Log]
tachtler:dovecot_backup_-_skript [2019/01/31 07:04] (aktuell)
klaus [Skript]
Zeile 126: Zeile 126:
 #               will be send by e-mail. ​                                     # #               will be send by e-mail. ​                                     #
 #                                                                            # #                                                                            #
-# Last update : 30.01.2019 ​                                                  # +# Last update : 31.01.2019 ​                                                  # 
-# Version ​    : 1.09                                                         #+# Version ​    : 1.10                                                         #
 #                                                                            # #                                                                            #
 # Author ​     : Klaus Tachtler, <​klaus@tachtler.net> ​                        # # Author ​     : Klaus Tachtler, <​klaus@tachtler.net> ​                        #
Zeile 204: Zeile 204:
 #               ​FILE_USERLIST was set and used.                              # #               ​FILE_USERLIST was set and used.                              #
 #               ​Thanks to kbridger. ​                                         # #               ​Thanks to kbridger. ​                                         #
 +# -------------------------------------------------------------------------- #
 +# Version ​    : 1.10                                                         #
 +# Description : Code redesign. ​                                              #
 # -------------------------------------------------------------------------- # # -------------------------------------------------------------------------- #
 # Version ​    : x.xx                                                         # # Version ​    : x.xx                                                         #
Zeile 321: Zeile 324:
 $RM_COMMAND -f $FILE_MAIL $RM_COMMAND -f $FILE_MAIL
  
 +}
 +
 +function error () {
 + # Parameters.
 + CODE_ERROR="​$1"​
 +
 +        sendmail ERROR
 + movelog
 + exit $CODE_ERROR
 +}
 +
 +function headerblock () {
 + # Parameters.
 + TEXT_INPUT="​$1"​
 + LINE_COUNT=68
 +
 +        # Help variables.
 +        WORD_COUNT=`echo $TEXT_INPUT | wc -c`
 +        CHAR_AFTER=`expr $LINE_COUNT - $WORD_COUNT - 5`
 +        LINE_SPACE=`expr $LINE_COUNT - 3`
 +
 + # Format placeholder.
 + if [ "​$CHAR_AFTER"​ -lt "​0"​ ]; then
 + CHAR_AFTER="​0"​
 + fi
 +
 + printf -v char '​%*s'​ $CHAR_AFTER ''​
 + printf -v line '​%*s'​ $LINE_SPACE ''​
 +
 + log "​+${line//​ /-}+"
 + log "| $TEXT_INPUT${char//​ /.} |"
 + log "​+${line//​ /-}+"
 +}
 +
 +function logline () {
 + # Parameters.
 + TEXT_INPUT="​$1"​
 + TRUE_FALSE="​$2"​
 + LINE_COUNT=68
 +
 +        # Help variables.
 +        WORD_COUNT=`echo $TEXT_INPUT | wc -c`
 +        CHAR_AFTER=`expr $LINE_COUNT - $WORD_COUNT - 9`
 +
 + # Format placeholder.
 + if [ "​$CHAR_AFTER"​ -lt "​0"​ ]; then
 + CHAR_AFTER="​0"​
 + fi
 +
 + printf -v char '​%*s'​ $CHAR_AFTER ''​
 +
 + if [ "​$TRUE_FALSE"​ == "​true"​ ]; then
 + log "​$TEXT_INPUT${char//​ /.}[  OK  ]"
 + else
 + log "​$TEXT_INPUT${char//​ /​.}[FAILED]"​
 + fi
 +}
 +
 +function checkcommand () {
 + # Parameters.
 +        CHECK_COMMAND="​$1"​
 +
 + if [ ! -s "​$1"​ ]; then
 + logline "Check if command '​$CHECK_COMMAND'​ was found " false
 + error 10
 + else
 + logline "Check if command '​$CHECK_COMMAND'​ was found " true
 + fi
 } }
  
Zeile 326: Zeile 397:
 log ""​ log ""​
 RUN_TIMESTAMP=`$DATE_COMMAND '​+%s'​` RUN_TIMESTAMP=`$DATE_COMMAND '​+%s'​`
-log "+-----------------------------------------------------------------+"​ +headerblock ​"Start backup of the mailboxes [`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'​`]"​
-log "​| ​Start backup of the mailboxes [`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'​`] ​|" +
-log "​+-----------------------------------------------------------------+"+
 log ""​ log ""​
 log "​SCRIPT_NAME.................:​ $SCRIPT_NAME"​ log "​SCRIPT_NAME.................:​ $SCRIPT_NAME"​
Zeile 342: Zeile 411:
  
 # Check if command (file) NOT exist OR IS empty. # Check if command (file) NOT exist OR IS empty.
-if [ ! -s "$DSYNC_COMMAND" ]; then +checkcommand ​$DSYNC_COMMAND 
-        log "Check if command '​$DSYNC_COMMAND'​ was found....................[FAILED]"​ +checkcommand ​$TAR_COMMAND 
-        sendmail ERROR +checkcommand ​$TOUCH_COMMAND 
- movelog +checkcommand ​$RM_COMMAND 
-        exit 11 +checkcommand ​$CAT_COMMAND 
-else +checkcommand ​$DATE_COMMAND 
-        log "Check if command '​$DSYNC_COMMAND'​ was found....................[ ​ OK  ]" +checkcommand ​$MKDIR_COMMAND 
-fi +checkcommand ​$CHOWN_COMMAND 
- +checkcommand ​$CHMOD_COMMAND 
-# Check if command (file) NOT exist OR IS empty. +checkcommand ​$GREP_COMMAND 
-if [ ! -s "$TAR_COMMAND" ]; then +checkcommand ​$PROG_SENDMAIL
-        log "Check if command '​$TAR_COMMAND'​ was found......................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 12 +
-else +
-        log "Check if command '​$TAR_COMMAND'​ was found......................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$TOUCH_COMMAND" ]; then +
-        log "Check if command '​$TOUCH_COMMAND'​ was found....................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 13 +
-else +
-        log "Check if command '​$TOUCH_COMMAND'​ was found....................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$RM_COMMAND" ]; then +
-        log "Check if command '​$RM_COMMAND'​ was found.......................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 14 +
-else +
-        log "Check if command '​$RM_COMMAND'​ was found.......................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$CAT_COMMAND" ]; then +
-        log "Check if command '​$CAT_COMMAND'​ was found......................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 15 +
-else +
-        log "Check if command '​$CAT_COMMAND'​ was found......................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$DATE_COMMAND" ]; then +
-        log "Check if command '​$DATE_COMMAND'​ was found.....................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 16 +
-else +
-        log "Check if command '​$DATE_COMMAND'​ was found.....................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$MKDIR_COMMAND" ]; then +
-        log "Check if command '​$MKDIR_COMMAND'​ was found....................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 17 +
-else +
-        log "Check if command '​$MKDIR_COMMAND'​ was found....................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$CHOWN_COMMAND" ]; then +
-        log "Check if command '​$CHOWN_COMMAND'​ was found....................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 18 +
-else +
-        log "Check if command '​$CHOWN_COMMAND'​ was found....................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$CHMOD_COMMAND" ]; then +
-        log "Check if command '​$CHMOD_COMMAND'​ was found....................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 19 +
-else +
-        log "Check if command '​$CHMOD_COMMAND'​ was found....................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$GREP_COMMAND" ]; then +
-        log "Check if command '​$GREP_COMMAND'​ was found.....................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 20 +
-else +
-        log "Check if command '​$GREP_COMMAND'​ was found.....................[ ​ OK  ]" +
-fi +
- +
-# Check if command (file) NOT exist OR IS empty. +
-if [ ! -s "$PROG_SENDMAIL" ]; then +
-        log "Check if command '​$PROG_SENDMAIL'​ was found................[FAILED]"​ +
-        sendmail ERROR +
- movelog +
-        exit 21 +
-else +
-        log "Check if command '​$PROG_SENDMAIL'​ was found................[ ​ OK  ]" +
-fi+
  
 # Check if LOCK file NOT exist. # Check if LOCK file NOT exist.
 if [ ! -e "​$FILE_LOCK"​ ]; then if [ ! -e "​$FILE_LOCK"​ ]; then
-        ​log "Check if script is NOT already runnig ​.....................[ ​ OK  ]"+        ​logline ​"Check if the script is NOT already runnig " ​true
  
         $TOUCH_COMMAND $FILE_LOCK         $TOUCH_COMMAND $FILE_LOCK
 else else
-        ​log "Check if script is NOT already runnig ​.....................[FAILED]"+        ​logline ​"Check if the script is NOT already runnig " ​false
         log ""​         log ""​
         log "​ERROR:​ The script was already running, or LOCK file already exists!"​         log "​ERROR:​ The script was already running, or LOCK file already exists!"​
         log ""​         log ""​
-        sendmail ERROR + error 20
- movelog +
-        exit 30+
 fi fi
  
 # Check if DIR_BACKUP Directory NOT exists. # Check if DIR_BACKUP Directory NOT exists.
 if [ ! -d "​$DIR_BACKUP"​ ]; then if [ ! -d "​$DIR_BACKUP"​ ]; then
-        ​log "Check if DIR_BACKUP exists.................................[FAILED]"+        ​logline ​"Check if DIR_BACKUP exists " ​false
  $MKDIR_COMMAND -p $DIR_BACKUP  $MKDIR_COMMAND -p $DIR_BACKUP
-        ​log "​DIR_BACKUP was now created.................................[ ​ OK  ]"+        ​logline ​"​DIR_BACKUP was now created " ​true
 else else
-        ​log "Check if DIR_BACKUP exists.................................[ ​ OK  ]"+        ​logline ​"Check if DIR_BACKUP exists " ​true
 fi fi
  
Zeile 478: Zeile 448:
 log ""​ log ""​
 if [ ! -n "​$FILE_USERLIST" ​ ]; then if [ ! -n "​$FILE_USERLIST" ​ ]; then
-        log "Check if the variable FILE_USERLIST is set.................[ ​ NO  ]"+        log "Check if the variable FILE_USERLIST is set ................[ ​ NO  ]"
         log "​Mailboxes to backup will be determined by doveadm user \"​*\"​."​         log "​Mailboxes to backup will be determined by doveadm user \"​*\"​."​
  
Zeile 485: Zeile 455:
  done  done
 else else
-        ​log "Check if the variable FILE_USERLIST is set.................[ ​ OK  ]+        ​logline ​"Check if the variable FILE_USERLIST is set " ​true 
-        log "​Mailboxes to backup will read from file."+        log "​Mailboxes to backup will be read from file."
         log ""​         log ""​
         log "- File: [$FILE_USERLIST]"​         log "- File: [$FILE_USERLIST]"​
Zeile 492: Zeile 462:
  # Check if file exists.  # Check if file exists.
  if [ -f "​$FILE_USERLIST"​ ]; then  if [ -f "​$FILE_USERLIST"​ ]; then
-        log "- Check if FILE_USERLIST exists............................[ ​ OK  ]"+        logline ​"- Check if FILE_USERLIST exists " ​true
  else  else
-        log "- Check if FILE_USERLIST exists............................[FAILED]"+        logline ​"- Check if FILE_USERLIST exists " ​false
         log ""​         log ""​
-         sendmail ERROR + error 30
- movelog +
-        exit 40+
  fi  fi
  
  # Check if file is readable.  # Check if file is readable.
  if [ -r "​$FILE_USERLIST"​ ]; then  if [ -r "​$FILE_USERLIST"​ ]; then
-        log "- Check if FILE_USERLIST is readable.......................[ ​ OK  ]"+        logline ​"- Check if FILE_USERLIST is readable " ​true
  else  else
-        log "- Check if FILE_USERLIST is readable.......................[FAILED]"+        logline ​"- Check if FILE_USERLIST is readable " ​false
         log ""​         log ""​
-         sendmail ERROR + error 31
- movelog +
-        exit 41+
  fi  fi
  
Zeile 540: Zeile 506:
 # Start backup. # Start backup.
 log ""​ log ""​
-log "+-----------------------------------------------------------------+"​ +headerblock ​"Run backup $SCRIPT_NAME "
-log "​| ​Run backup $SCRIPT_NAME ​..................................... |" +
-log "​+-----------------------------------------------------------------+"+
 log ""​ log ""​
  
Zeile 583: Zeile 547:
  (ls -t $users-$FILE_DELETE|head -n $BACKUPFILES_DELETE;​ls $users-$FILE_DELETE)|sort|uniq -u|xargs -r rm  (ls -t $users-$FILE_DELETE|head -n $BACKUPFILES_DELETE;​ls $users-$FILE_DELETE)|sort|uniq -u|xargs -r rm
  if [ "​$?"​ != "​0"​ ]; then  if [ "​$?"​ != "​0"​ ]; then
-        log "​Delete old archive files $DIR_BACKUP ​.....................[FAILED]"+        logline ​"​Delete old archive files $DIR_BACKUP " ​false
  else  else
-        log "​Delete old archive files $DIR_BACKUP ​.....................[ ​ OK  ]"+        logline ​"​Delete old archive files $DIR_BACKUP " ​true
  fi  fi
  
Zeile 591: Zeile 555:
  $RM_COMMAND "​$DIR_BACKUP/​$DOMAINPART"​ -rf  $RM_COMMAND "​$DIR_BACKUP/​$DOMAINPART"​ -rf
  if [ "​$?"​ != "​0"​ ]; then  if [ "​$?"​ != "​0"​ ]; then
-        log "​Delete mailbox files at: $DIR_BACKUP ​.....................[FAILED]"+        logline ​"​Delete mailbox files at: $DIR_BACKUP " ​false
  else  else
-        log "​Delete mailbox files at: $DIR_BACKUP ​.....................[ ​ OK  ]"+        logline ​"​Delete mailbox files at: $DIR_BACKUP " ​true
  fi  fi
  fi  fi
Zeile 611: Zeile 575:
         log ""​         log ""​
         $RM_COMMAND -f $FILE_LOCK         $RM_COMMAND -f $FILE_LOCK
-        sendmail ERROR + error ​99
-        movelog +
-        exit 99+
 else else
-        log "+-----------------------------------------------------------------+"​ + headerblock ​"End backup $SCRIPT_NAME "
-        log "​| ​End backup $SCRIPT_NAME ​..................................... |" +
-        log "​+-----------------------------------------------------------------+"+
         log ""​         log ""​
 fi fi
  
 # Finish syncing with runntime statistics. # Finish syncing with runntime statistics.
-log "+-----------------------------------------------------------------+"​ +headerblock ​"​Runtime statistics "
-log "​| ​Runtime statistics............................................. |" +
-log "​+-----------------------------------------------------------------+"+
 log ""​ log ""​
 log "- Number of determined users: $VAR_COUNT_USER"​ log "- Number of determined users: $VAR_COUNT_USER"​
Zeile 641: Zeile 599:
 log "​Runtime:​ `$DATE_COMMAND -u -d "0 $END_TIMESTAMP seconds - $RUN_TIMESTAMP seconds"​ +'​%H:​%M:​%S'​` time elapsed."​ log "​Runtime:​ `$DATE_COMMAND -u -d "0 $END_TIMESTAMP seconds - $RUN_TIMESTAMP seconds"​ +'​%H:​%M:​%S'​` time elapsed."​
 log ""​ log ""​
-log "+-----------------------------------------------------------------+"​ +headerblock ​"​Finished creating the backups [`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'​`]"​
-log "​| ​Finished creating the backups [`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'​`] ​|" +
-log "​+-----------------------------------------------------------------+"+
 log ""​ log ""​
  
tachtler/dovecot_backup_-_skript.txt · Zuletzt geändert: 2019/01/31 07:04 von klaus