Benutzer-Werkzeuge

Webseiten-Werkzeuge


tachtler:dovecot_backup_-_skript

Dovecot Backup - Skript

:!: HINWEIS - Die Nachfolgende Konfiguration von Dovecot setzt eine lauffähige Installation von Dovecot voraus, wie unter nachfolgendem internen Link beschrieben !!!

Dovecot ist ein Open-Source-IMAP-und POP3-E-Mail-Server für Linux bzw. UNIX-ähnlichen Systeme, entwickelt mit dem Hauptaugenmerk auf Sicherheit. Dovecot ist eine ausgezeichnete Wahl für kleine und große Installationen. Dovecot ist schnell und einfach zu installieren, erfordert keine besonderen Voraussetzungen und ist Ressourcenschonend.

Dovecot wird von Timo Sirainen entwickelt.

Ab hier werden root-Rechte zur Ausführung der nachfolgenden Befehle benötigt. Um root zu werden geben Sie bitte folgenden Befehl ein:

$ su -
Password: 

Zstandard (zstd)

Für den Einsatz der Komprimierungsmethode Zstandard (zstd) ist es ggf. erforderlich, das Programm zstd zu installieren.

CentOS 7

Zur Installation von Zstandard (zstd) über ein rpm-Paket, soll hier das Repository eines Drittanbieters genutzt werden, da Zstandard (zstd) nicht im CentOS-Repository enthalten ist.

Nachfolgend soll das Repository des Drittanbieters EPEL genutzt werden. Eine Anleitung, wie das Repository des Drittanbieters EPEL eingebunden werden könnte, kann unter nachfolgendem internen Link nachgelesen werden:

Zur Installation der Komprimierungsmethode Zstandard (zstd) wird nachfolgendes Paket benötigt:

  • zstd - ist im epel-Repository des Drittanbieters EPEL enthalten

Mit nachfolgendem Befehl, werden die benötigten Pakete installiert:

# yum install zstd
Loaded plugins: changelog, priorities
234 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package zstd.x86_64 0:1.5.0-1.el7 will be installed
--> Finished Dependency Resolution

Changes in packages about to be updated:


Dependencies Resolved

================================================================================
 Package         Arch              Version                Repository       Size
================================================================================
Installing:
 zstd            x86_64            1.5.0-1.el7            epel            649 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 649 k
Installed size: 2.1 M
Is this ok [y/d/N]: y
Downloading packages:
zstd-1.5.0-1.el7.x86_64.rpm                                | 649 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : zstd-1.5.0-1.el7.x86_64                                      1/1 
  Verifying  : zstd-1.5.0-1.el7.x86_64                                      1/1 

Installed:
  zstd.x86_64 0:1.5.0-1.el7                                                     

Complete!

Mit nachfolgendem Befehl kann überprüft werden, welcher Inhalt mit dem Paket installiert wurde.

Paket zstd:

# rpm -qil zstd
Name        : zstd
Version     : 1.5.0
Release     : 1.el7
Architecture: x86_64
Install Date: Sat 02 Oct 2021 07:40:55 AM CEST
Group       : Unspecified
Size        : 2178120
License     : BSD and GPLv2
Signature   : RSA/SHA256, Mon 17 May 2021 03:12:58 AM CEST, Key ID 6a2faea2352c64e5
Source RPM  : zstd-1.5.0-1.el7.src.rpm
Build Date  : Sun 16 May 2021 10:13:19 PM CEST
Build Host  : buildhw-x86-12.iad2.fedoraproject.org
Relocations : (not relocatable)
Packager    : Fedora Project
Vendor      : Fedora Project
URL         : https://github.com/facebook/zstd
Bug URL     : https://bugz.fedoraproject.org/zstd
Summary     : Zstd compression library
Description :
Zstd, short for Zstandard, is a fast lossless compression algorithm,
targeting real-time compression scenarios at zlib-level compression ratio.
/usr/bin/pzstd
/usr/bin/unzstd
/usr/bin/zstd
/usr/bin/zstdcat
/usr/bin/zstdgrep
/usr/bin/zstdless
/usr/bin/zstdmt
/usr/share/doc/zstd-1.5.0
/usr/share/doc/zstd-1.5.0/CHANGELOG
/usr/share/doc/zstd-1.5.0/README.md
/usr/share/licenses/zstd-1.5.0
/usr/share/licenses/zstd-1.5.0/COPYING
/usr/share/licenses/zstd-1.5.0/LICENSE
/usr/share/man/man1/pzstd.1.gz
/usr/share/man/man1/unzstd.1.gz
/usr/share/man/man1/zstd.1.gz
/usr/share/man/man1/zstdcat.1.gz
/usr/share/man/man1/zstdgrep.1.gz
/usr/share/man/man1/zstdless.1.gz

Backup

Nachfolgendes Skript ermöglicht die Erstellung einer *.tar.gz-Datei oder eine *.tar.zst-Datei

  • von allen Benutzerkonten
  • oder über eine Datei mit E-Mail-Adressen, der zu sichernden Benutzerkonten
  • pro Benutzer eine eigene *.tar.gz-Datei oder *.tar.zst-Datei
  • auf Dateiebene
  • mit konfigurierbaren Parametern
  • und automatischer Löschung von älteren Sicherungsdateien
  • und Erstellung einer fortlaufenden Log-Datei unter /var/log/

Nachfolgende Parameter sind in der aktuellen Version konfigurierbar: (Nur relevanter Ausschnitt)

...
##############################################################################
# >>> Please edit following lines for personal settings and custom usages. ! #
##############################################################################
 
# CUSTOM - Script-Name.
SCRIPT_NAME='dovecot_backup'
 
# CUSTOM - Backup-Files compression method - (possible values: gz zst).
COMPRESSION='gz'
 
# CUSTOM - Backup-Files.
TMP_FOLDER='/srv/backup'
DIR_BACKUP='/srv/backup'
FILE_BACKUP=dovecot_backup_`date '+%Y%m%d_%H%M%S'`.tar.$COMPRESSION
FILE_DELETE=$(printf '*.tar.%s' $COMPRESSION)
BACKUPFILES_DELETE=14
 
# CUSTOM - dovecot Folders.
MAILDIR_TYPE='maildir'
MAILDIR_NAME='Maildir'
MAILDIR_USER='vmail'
MAILDIR_GROUP='vmail'
 
# CUSTOM - Path and file name of a file with e-mail addresses to backup, if
#          SET. If NOT, the script will determine all mailboxes by default.
# FILE_USERLIST='/path/and/file/name/of/user/list/with/one/user/per/line'
# - OR -
# FILE_USERLIST=''
FILE_USERLIST=''
 
# CUSTOM - Check when FILE_USERLIST was used, if the user per line was a
#          valid e-mail address [Y|N].
FILE_USERLIST_VALIDATE_EMAIL='N'
 
# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='you@example.com'
 
# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
 
##############################################################################
# >>> Normaly there is no need to change anything below this comment line. ! #
##############################################################################
...
Parametername Beschreibung
SCRIPT_NAME Name des Skriptes
COMPRESSION Art der zu verwendenden Komprimierungsmethode, je nach Endung (zst = zstd oder gz = gzip)
TMP_FOLDER Verzeichnis in dem ein temporärer Ordner erstellt werden soll, um die Backup-Dateien temporär zu extrahiert, damit diese dann abschliessend in ein Archivformat (tar.gz) zu packen
DIR_BACKUP Verzeichnis in dem die Backup-Dateien abgelegt werden sollen
FILE_BACKUP Allgemeiner Teil der Bezeichnung für die Backup-Dateien
FILE_DELETE Endung zur Löschung von älteren Archivdateien, muss wie FILE_BACKUP enden !
BACKUPFILES_DELETE Anzahl der Backup-Dateien pro Benutzer die gespeichert bleiben sollen
MAILDIR_TYPE Maildir-Format - :!: ohne Doppelpunkt
MAILDIR_NAME Name des Maildir-Verzeichnisses unterhalb des home-Verzeichnisses des Benutzers
MAILDIR_USER Benutzername unter dem alle Postfächer (Mailboxes) verwaltet werden, siehe auch:
Dovecot CentOS 6 - Konfiguration Authentifizierung - Authentifizierungsbenutzer
Dovecot CentOS 7 - Konfiguration Authentifizierung - Authentifizierungsbenutzer
MAILDIR_GROUP Benutzergruppe unter dem alle Postfächer (Mailboxes) verwaltet werden, siehe auch:
Dovecot CentOS 6 - Konfiguration Authentifizierung - Authentifizierungsbenutzer
Dovecot CentOS 7 - Konfiguration Authentifizierung - Authentifizierungsbenutzer
FILE_USERLIST Datei mit Benutzernamen (einer pro Zeile), welche Mailboxen/Benutzer gesichert werden, anstelle der Ermittlung durch doveadm user ”*”, um nicht alle Mailboxen/Benutzer zwangsweise zu sichern
FILE_USERLIST_VALIDATE_EMAIL Überprüfung des Benutzernamens, ob dieser eine gültige E-Mail Adresse darstellt. Muss dann deaktiviert werden, wenn der Benutzername keine E-Mail Adresse ist!
MAIL_RECIPIENT Empfänger E-Mail-Adresse für E-Mails des Skripts
MAIL_STATUS Versand einer Status E-Mail nach erfolgreicher Skript Ausführung

/var/lib/dovecot

Damit nachfolgendes Backup-Skript die nötigen Rechte zum lesen der Konfigurationsdatei

  • /var/lib/dovecot/db/shared-mailboxes.db

hat, müssen die Besitzrechte am Verzeichnis

  • /var/lib/dovecot

wie folgt angepasst werden:

# chown dovecot:vmail /var/lib/dovecot

Ein Überprüfung der entsprechenden Besitzrechte sollte dann wie folgt aussehen:

# ls -ld /var/lib/dovecot/
drwxr-x--- 3 dovecot vmail 56 Mar 16 15:01 /var/lib/dovecot/

:!: WICHTIG - Vorhergehend werden der Gruppe vmail die Rechte: lesen und ausführen eingeräumt.

Skript

#!/usr/bin/env bash
 
##############################################################################
# Script-Name : dovecot_backup.sh                                            #
# Description : Script to backup the mailboxes from dovecot.                 #
#               On successful execution only a LOG file will be written.     #
#               On error while execution, a LOG file and a error message     #
#               will be send by e-mail.                                      #
#                                                                            #
# Last update : 20.09.2024                                                   #
# Version     : 1.21                                                         #
#                                                                            #
# Author      : Klaus Tachtler, <klaus@tachtler.net>                         #
# DokuWiki    : http://www.dokuwiki.tachtler.net                             #
# Homepage    : http://www.tachtler.net                                      #
#                                                                            #
#  +----------------------------------------------------------------------+  #
#  | This program is free software; you can redistribute it and/or modify |  #
#  | it under the terms of the GNU General Public License as published by |  #
#  | the Free Software Foundation; either version 3 of the License, or    |  #
#  | (at your option) any later version.                                  |  #
#  +----------------------------------------------------------------------+  #
#                                                                            #
# Copyright (c) 2024 by Klaus Tachtler.                                      #
#                                                                            #
##############################################################################
 
##############################################################################
#                                H I S T O R Y                               #
##############################################################################
# Version     : 1.01                                                         #
# Description : Bugfix: Delete all temporary domain directories not only the #
#               last one. Thanks to Guenther J. Niederwimmer.                #
# -------------------------------------------------------------------------- #
# Version     : 1.02                                                         #
# Description : GitHub: Issue #1                                             #
#               The name of the variable to delete the number of old backup  #
#               files $DAYS_DELETE was renamed to $BACKUPFILES_DELETE. This  #
#               was done for better understanding, because if the script was #
#               running more than once a day, this could be misunderstood.   #
#               Thanks to Diane Trout.                                       #
# -------------------------------------------------------------------------- #
# Version     : 1.03                                                         #
# Description : Quota calculation double the calculated size of a mailbox,   #
#               when dict was used. See also following mailing-list entry:   #
#                                                                            #
#               https://www.dovecot.org/list/dovecot/2012-February/          #
#               063585.html                                                  #
#                                                                            #
#               Thanks to André Peters.                                      #
# -------------------------------------------------------------------------- #
# Version     : 1.04                                                         #
# Description : Typo: Correction of the return code query of                 #
#               "# Delete LOCK file." in a pure string comparison.           #
#               Thanks to Oli Sennhauser.                                    #
# -------------------------------------------------------------------------- #
# Version     : 1.05                                                         #
# Description : GitHub: Issue #4                                             #
#               Add error handling for dsync command.                        #
#               Add runtime statistics.                                      #
#               Thanks to HenrikWMG.                                         #
# -------------------------------------------------------------------------- #
# Version     : 1.06                                                         #
# Description : Avoid an error when trying to delete backup files, if the    #
#               $BACKUPFILES_DELETE count is NOT reached.                    #
#               Change file owner, after backup was created.                 #
#               Change file permissions to 600, after backup was created.    #
#               Thanks to Seep1959.                                          #
# -------------------------------------------------------------------------- #
# Version     : 1.07                                                         #
# Description : Compatibility: Change the parameter order for the step       #
#               "Delete archive files for user" for better compatibility     #
#               with FreeBSD.                                                #
#               Thanks to Alexander Preyer.                                  #
# -------------------------------------------------------------------------- #
# Version     : 1.08                                                         #
# Description : GitHub Issue #9                                              #
#               Add ability to only backup specific mailboxes, by using the  # 
#               variable FILE_USERLIST with the file path and file name as   #
#               content. The file must contain one e-mail address per line.  #
#               Add the calculation of the script runtime.                   #
#               Thanks to graue Ritter.                                      #
# -------------------------------------------------------------------------- #
# Version     : 1.09                                                         #
# Description : Add a switch to enable or disable e-mail address check, when #
#               FILE_USERLIST was set and used.                              #
#               Thanks to kbridger.                                          #
# -------------------------------------------------------------------------- #
# Version     : 1.10                                                         #
# Description : Code redesign.                                               #
# -------------------------------------------------------------------------- #
# Version     : 1.11                                                         #
# Description : GitHub Issue #12                                             #
#               Change of the temporary storage medium from DIR_BACKUP to    #
#               TMP_FOLDER for temporary storage of extracted emails from    #
#               the mailboxes was introduced. This allows the use of a       #
#               temporary storage of the extracted emails from the mailboxes #
#               on a faster storage medium, or also on a local storage       #
#               medium, which avoids rights problems if DIR_BACKUP is e.g.   #
#               an NFS mounted storage.                                      #
#               Thanks to Krisztián Hamar.                                   #
# -------------------------------------------------------------------------- #
# Version     : 1.12                                                         #
# Description : GitHub: Issue #13                                            #
#               Change in mv command detection due to initial problems with  #
#               Ubuntu 18.04 LTS.                                            #
#               Thanks to hatted.                                            #
# -------------------------------------------------------------------------- #
# Version     : 1.13                                                         #
# Description : GitHub: Issue #16                                            #
#               Changed the timezone format to hours: for example (+0100) at #
#               VAR_EMAILDATE, because not all e-Mail user interfaces can    #
#               handle the letter time zone notation.                        #
#               Thanks to velzebop.                                          #
# -------------------------------------------------------------------------- #
# Version     : 1.14                                                         #
# Description : GitHub: Issue #18                                            #
#               Add dash '-' and dot '.' to the list of valid chars for the  #
#               e-Mail address validation for the localpart and the          #
#               domainpart.                                                  #
#               Thanks to Henrocker.                                         #
# -------------------------------------------------------------------------- #
# Version     : 1.15                                                         #
# Description : GitHub: Issue #21                                            #
#               Set the required ownership on TMP_FOLDER before running the  #
#               script.                                                      #
#               Thanks to LarsBel.                                           #
# -------------------------------------------------------------------------- #
# Version     : 1.16                                                         #
# Description : Optimize ownership settings for TMP_FOLDER and DIR_BACKUP.   #
# -------------------------------------------------------------------------- #
# Version     : 1.17                                                         #
# Description : GitHub: Issue #22.                                           #
#               Bugfix - movelog does not work properly when an email is to  #
#               be sent due to an error, or a status email has been          #
#               requested.                                                   #
#               Thanks to selbitschka.                                       #
# -------------------------------------------------------------------------- #
# Version     : 1.18                                                         #
# Description : Introduction of zstd compression as an alternative choice to #
#               gzip compression. So now by setting the variable COMPRESSION #
#               the type of compression can be selected between zst and gz.  #
#               The zstd compression can lower the execution time by half.   #
#               The design of the code was also revised.                     #
#               The error handling was also been improved.                   #
#               Thanks to Marco De Lellis.                                   #
# -------------------------------------------------------------------------- #
# Version     : 1.19                                                         #
# Description : GitHub: Issue #24                                            #
#               Correct the license mismatch between GitHub and the script.  #
#               Thanks to David Haerdeman (Alphix).                          #
# -------------------------------------------------------------------------- #
# Version     : 1.20                                                         #
# Description : GitHub: Pull request #26                                     #
#               Improved FreeBSD compatibility.                              #
#               Thanks to wombelix (Dominik Wombacher)                       #
# -------------------------------------------------------------------------- #
# Version     : 1.21                                                         #
# Description : GitHub: Issue #27                                            #
#               Extension for OpenBSD compatibility.                         #
#               Thanks to ozgurkazancci (Konstantin) and                     #
#               renaudallard (Renaud Allard)                                 #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
##############################################################################
 
##############################################################################
# >>> Please edit following lines for personal settings and custom usages. ! #
##############################################################################
 
# CUSTOM - Script-Name.
SCRIPT_NAME='dovecot_backup'
 
# CUSTOM - Backup-Files compression method - (possible values: gz zst).
COMPRESSION='gz'
 
# CUSTOM - Backup-Files.
TMP_FOLDER='/srv/backup'
DIR_BACKUP='/srv/backup'
FILE_BACKUP=dovecot_backup_`date '+%Y%m%d_%H%M%S'`.tar.$COMPRESSION
FILE_DELETE=$(printf '*.tar.%s' $COMPRESSION)
BACKUPFILES_DELETE=14
 
# CUSTOM - dovecot Folders.
MAILDIR_TYPE='maildir'
MAILDIR_NAME='Maildir'
MAILDIR_USER='vmail'
MAILDIR_GROUP='vmail'
 
# CUSTOM - Path and file name of a file with e-mail addresses to backup, if
#          SET. If NOT, the script will determine all mailboxes by default.
# FILE_USERLIST='/path/and/file/name/of/user/list/with/one/user/per/line'
# - OR -
# FILE_USERLIST=''
FILE_USERLIST=''
 
# CUSTOM - Check when FILE_USERLIST was used, if the user per line was a
#          valid e-mail address [Y|N].
FILE_USERLIST_VALIDATE_EMAIL='N'
 
# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='root@tachtler.net'
 
# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
 
##############################################################################
# >>> Normaly there is no need to change anything below this comment line. ! #
##############################################################################
 
# Variables.
TAR_COMMAND=`command -v tar`
GZIP_COMMAND=`command -v gzip`
ZSTD_COMMAND=`command -v zstd`
TOUCH_COMMAND=`command -v touch`
RM_COMMAND=`command -v rm`
PROG_SENDMAIL=`command -v sendmail`
CAT_COMMAND=`command -v cat`
DATE_COMMAND=`command -v date`
MKDIR_COMMAND=`command -v mkdir`
CHOWN_COMMAND=`command -v chown`
CHMOD_COMMAND=`command -v chmod`
MKTEMP_COMMAND=`command -v mktemp`
GREP_COMMAND=`command -v grep`
MV_COMMAND=`command which mv`
STAT_COMMAND=`command -v stat`
FILE_LOCK='/tmp/'$SCRIPT_NAME'.lock'
FILE_LOG='/var/log/'$SCRIPT_NAME'.log'
FILE_LAST_LOG='/tmp/'$SCRIPT_NAME'.log'
FILE_MAIL='/tmp/'$SCRIPT_NAME'.mail'
FILE_MBOXLIST='/tmp/'$SCRIPT_NAME'.mboxlist'
VAR_OS=`uname -s`
VAR_HOSTNAME=`uname -n`
VAR_SENDER='root@'$VAR_HOSTNAME
VAR_EMAILDATE=`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%z)'`
declare -a VAR_LISTED_USER=()
declare -a VAR_FAILED_USER=()
VAR_COUNT_USER=0
VAR_COUNT_FAIL=0
 
# FreeBSD and OpenBSD specific commands
if [ "${VAR_OS,,}" = "freebsd" ] || [ "${VAR_OS,,}" = "openbsd" ] ; then
        DSYNC_COMMAND=`command -v doveadm`
        STAT_COMMAND_PARAM_FORMAT='-f'
        STAT_COMMAND_ARG_FORMAT_USER='%Su'
        STAT_COMMAND_ARG_FORMAT_GROUP='%Sg'
        MKTEMP_COMMAND_PARAM_ARG="-d ${TMP_FOLDER}/${SCRIPT_NAME}-XXXXXXXXXXXX"
else
	DSYNC_COMMAND=`command -v dsync`
        STAT_COMMAND_PARAM_FORMAT='-c'
        STAT_COMMAND_ARG_FORMAT_USER='%U'
        STAT_COMMAND_ARG_FORMAT_GROUP='%G'
        MKTEMP_COMMAND_PARAM_ARG="-d -p ${TMP_FOLDER} -t ${SCRIPT_NAME}-XXXXXXXXXXXX"
fi
 
# Functions.
function log() {
        echo $1
        echo `$DATE_COMMAND '+%Y/%m/%d %H:%M:%S'` " INFO:" $1 >>${FILE_LAST_LOG}
}
 
function retval() {
if [ "$?" != "0" ]; then
        case "$?" in
        *)
                log "ERROR: Unknown error $?"
        ;;
        esac
fi
}
 
function movelog() {
	$CAT_COMMAND $FILE_LAST_LOG >> $FILE_LOG
	$RM_COMMAND -f $FILE_LAST_LOG	
	$RM_COMMAND -f $FILE_LOCK
}
 
function sendmail() {
        case "$1" in
        'STATUS')
                MAIL_SUBJECT='Status execution '$SCRIPT_NAME' script.'
        ;;
        *)
                MAIL_SUBJECT='ERROR while execution '$SCRIPT_NAME' script !!!'
        ;;
        esac
 
$CAT_COMMAND <<MAIL >$FILE_MAIL
Subject: $MAIL_SUBJECT
Date: $VAR_EMAILDATE
From: $VAR_SENDER
To: $MAIL_RECIPIENT
 
MAIL
 
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_MAIL
 
$PROG_SENDMAIL -f $VAR_SENDER -t $MAIL_RECIPIENT < $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
}
 
# Main.
log ""
RUN_TIMESTAMP=`$DATE_COMMAND '+%s'`
headerblock "Start backup of the mailboxes [`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%z)'`]"
log ""
log "SCRIPT_NAME.................: $SCRIPT_NAME"
log ""
log "OS_TYPE.....................: $VAR_OS"
log ""
log "COMPRESSION.................: $COMPRESSION"
log ""
log "TMP_FOLDER..................: $TMP_FOLDER"
log "DIR_BACKUP..................: $DIR_BACKUP"
log ""
log "MAIL_RECIPIENT..............: $MAIL_RECIPIENT"
log "MAIL_STATUS.................: $MAIL_STATUS"
log ""
log "FILE_USERLIST...............: $FILE_USERLIST"
log "FILE_USERLIST_VALIDATE_EMAIL: $FILE_USERLIST_VALIDATE_EMAIL"
log ""
 
# Check if compress extension is allowed.
if [[ $COMPRESSION != 'zst' && $COMPRESSION != 'gz' ]]; then
        logline "Check compression extension" false
        log ""
        log "ERROR: Compression extension $COMPRESSION unsupported: choose between gz and zst"
        log ""
        error 19
fi
 
# Check if command (file) NOT exist OR IS empty.
checkcommand $DSYNC_COMMAND
checkcommand $TAR_COMMAND
checkcommand $TOUCH_COMMAND
checkcommand $RM_COMMAND
checkcommand $CAT_COMMAND
checkcommand $DATE_COMMAND
checkcommand $MKDIR_COMMAND
checkcommand $CHOWN_COMMAND
checkcommand $CHMOD_COMMAND
checkcommand $GREP_COMMAND
checkcommand $MKTEMP_COMMAND
checkcommand $MV_COMMAND
checkcommand $STAT_COMMAND
checkcommand $PROG_SENDMAIL
 
if [ $COMPRESSION = 'gz' ]; then
        checkcommand $GZIP_COMMAND
fi
 
if [ $COMPRESSION = 'zst' ]; then
        checkcommand $ZSTD_COMMAND
fi
 
# Check if LOCK file NOT exist.
if [ ! -e "$FILE_LOCK" ]; then
        logline "Check if the script is NOT already runnig " true
 
        $TOUCH_COMMAND $FILE_LOCK
else
        logline "Check if the script is NOT already runnig " false
        log ""
        log "ERROR: The script was already running, or LOCK file already exists!"
        log ""
	error 20
fi
 
# Check if TMP_FOLDER directory path NOT exists, else create it.
if [ ! -d "$TMP_FOLDER" ]; then
        logline "Check if TMP_FOLDER exists " false
	$MKDIR_COMMAND -p $TMP_FOLDER
	if [ "$?" != "0" ]; then
		logline "Create temporary '$TMP_FOLDER' folder " false
		error 21
	else
		logline "Create temporary '$TMP_FOLDER' folder " true
	fi
else
        logline "Check if TMP_FOLDER exists " true
fi
 
# Check if TMP_FOLDER is owned by $MAILDIR_USER.
if [ "$MAILDIR_USER" != `$STAT_COMMAND $STAT_COMMAND_PARAM_FORMAT "$STAT_COMMAND_ARG_FORMAT_USER" $TMP_FOLDER` ]; then
        logline "Check if TMP_FOLDER owner is $MAILDIR_USER " false
	$CHOWN_COMMAND -R $MAILDIR_USER:$MAILDIR_GROUP $TMP_FOLDER
	if [ "$?" != "0" ]; then
        	logline "Set ownership of TMP_FOLDER to $MAILDIR_USER:$MAILDIR_GROUP " false
		error 22
	else
        	logline "Set ownership of TMP_FOLDER to $MAILDIR_USER:$MAILDIR_GROUP " true
	fi
else
        logline "Check if TMP_FOLDER owner is $MAILDIR_USER " true
fi
 
# Check if TMP_FOLDER group is $MAILDIR_GROUP.
if [ "$MAILDIR_GROUP" != `$STAT_COMMAND $STAT_COMMAND_PARAM_FORMAT "$STAT_COMMAND_ARG_FORMAT_GROUP" $TMP_FOLDER` ]; then
        logline "Check if TMP_FOLDER group is $MAILDIR_GROUP " false
	$CHOWN_COMMAND -R $MAILDIR_USER:$MAILDIR_GROUP $TMP_FOLDER
	if [ "$?" != "0" ]; then
        	logline "Set ownership of TMP_FOLDER to $MAILDIR_USER:$MAILDIR_GROUP " false
		error 23
	else
        	logline "Set ownership of TMP_FOLDER to $MAILDIR_USER:$MAILDIR_GROUP " true
	fi
else
        logline "Check if TMP_FOLDER group is $MAILDIR_GROUP " true
fi
 
# Check if DIR_BACKUP directory NOT exists, else create it.
if [ ! -d "$DIR_BACKUP" ]; then
        logline "Check if DIR_BACKUP exists " false
	$MKDIR_COMMAND -p $DIR_BACKUP
	if [ "$?" != "0" ]; then
		logline "Create backup '$DIR_BACKUP' folder " false
		error 24
	else
		logline "Create backup '$DIR_BACKUP' folder " true
	fi
else
        logline "Check if DIR_BACKUP exists " true
fi
 
# Check if DIR_BACKUP is owned by $MAILDIR_USER.
if [ "$MAILDIR_USER" != `$STAT_COMMAND $STAT_COMMAND_PARAM_FORMAT "$STAT_COMMAND_ARG_FORMAT_USER" $DIR_BACKUP` ]; then
        logline "Check if DIR_BACKUP owner is $MAILDIR_USER " false
	$CHOWN_COMMAND -R $MAILDIR_USER:$MAILDIR_GROUP $DIR_BACKUP
	if [ "$?" != "0" ]; then
        	logline "Set ownership of DIR_BACKUP to $MAILDIR_USER:$MAILDIR_GROUP " false
		error 25
	else
        	logline "Set ownership of DIR_BACKUP to $MAILDIR_USER:$MAILDIR_GROUP " true
	fi
else
        logline "Check if DIR_BACKUP owner is $MAILDIR_USER " true
fi
 
# Check if DIR_BACKUP group is $MAILDIR_GROUP.
if [ "$MAILDIR_GROUP" != `$STAT_COMMAND $STAT_COMMAND_PARAM_FORMAT "$STAT_COMMAND_ARG_FORMAT_GROUP" $DIR_BACKUP` ]; then
        logline "Check if DIR_BACKUP group is $MAILDIR_GROUP " false
	$CHOWN_COMMAND -R $MAILDIR_USER:$MAILDIR_GROUP $DIR_BACKUP
	if [ "$?" != "0" ]; then
        	logline "Set ownership of DIR_BACKUP to $MAILDIR_USER:$MAILDIR_GROUP " false
		error 26
	else
        	logline "Set ownership of DIR_BACKUP to $MAILDIR_USER:$MAILDIR_GROUP " true
	fi
else
        logline "Check if DIR_BACKUP group is $MAILDIR_GROUP " true
fi
 
# Check if FILE_USERLIST NOT set OR IS empty.
log ""
if [ ! -n "$FILE_USERLIST"  ]; then
        log "Check if the variable FILE_USERLIST is set ................[  NO  ]"
        log "Mailboxes to backup will be determined by doveadm user \"*\"."
 
	for users in `doveadm user "*"`; do
		VAR_LISTED_USER+=($users);
	done
else
        logline "Check if the variable FILE_USERLIST is set " true
        log "Mailboxes to backup will be read from file."
        log ""
        log "- File: [$FILE_USERLIST]"
 
	# Check if file exists.
	if [ -f "$FILE_USERLIST" ]; then
        	logline "- Check if FILE_USERLIST exists " true
	else
        	logline "- Check if FILE_USERLIST exists " false
        	log ""
		error 30
	fi
 
	# Check if file is readable.
	if [ -r "$FILE_USERLIST" ]; then
        	logline "- Check if FILE_USERLIST is readable " true
	else
        	logline "- Check if FILE_USERLIST is readable " false
        	log ""
		error 31
	fi
 
	# Read file into variable.
	while IFS= read -r line
	do	
		# Check for valid e-mail address.
		if [ $FILE_USERLIST_VALIDATE_EMAIL = 'Y' ]; then
			# Check if basic email address syntax is valid.
			if echo "${line}" | $GREP_COMMAND '^[a-zA-Z0-9.-]*@[a-zA-Z0-9.-]*\.[a-zA-Z0-9]*$' >/dev/null; then
				VAR_LISTED_USER+=($line);
			else
        			log ""
		        	log "ERROR: The user: $line is NOT valid e-mail address!"
 
	                	((VAR_COUNT_FAIL++))
	                	VAR_FAILED_USER+=($line);
			fi
		else
			VAR_LISTED_USER+=($line);
		fi
	done <"$FILE_USERLIST"
 
	# Check if VAR_COUNT_FAIL is greater than zero. If YES, set VAR_COUNT_USER to VAR_COUNT_FAIL.
	if [ "$VAR_COUNT_FAIL" -ne "0" ]; then
		VAR_COUNT_USER=$VAR_COUNT_FAIL
	fi
fi
 
# Start backup.
log ""
headerblock "Run backup $SCRIPT_NAME "
log ""
 
# Make temporary directory DIR_TEMP inside TMP_FOLDER.
DIR_TEMP=$($MKTEMP_COMMAND $MKTEMP_COMMAND_PARAM_ARG)
if [ "$?" != "0" ]; then
	logline "Create temporary '$DIR_TEMP' folder " false
	error 40
else
	logline "Create temporary '$DIR_TEMP' folder " true
	log ""
fi
 
# Set ownership to DIR_TEMP.
$CHOWN_COMMAND -R $MAILDIR_USER:$MAILDIR_GROUP $DIR_TEMP
if [ "$?" != "0" ]; then
       	logline "Set ownership of DIR_TEMP to $MAILDIR_USER:$MAILDIR_GROUP " false
	error 41
else
       	logline "Set ownership of DIR_TEMP to $MAILDIR_USER:$MAILDIR_GROUP " true
	log ""
fi
 
# Start real backup process for all users.
for users in "${VAR_LISTED_USER[@]}"; do
	log "Start backup process for user: $users ..."
 
	((VAR_COUNT_USER++))
	DOMAINPART=${users#*@}
	LOCALPART=${users%%@*}
	LOCATION="$DIR_TEMP/$DOMAINPART/$LOCALPART/$MAILDIR_NAME"
	USERPART="$DOMAINPART/$LOCALPART"
 
	log "Extract mailbox data for user: $users ..."
 
        if [ "${VAR_OS,,}" = "freebsd" ] || [ "${VAR_OS,,}" = "openbsd" ] ; then
	        $DSYNC_COMMAND -o plugin/quota= backup -u $users $MAILDIR_TYPE:$LOCATION
	else
		$DSYNC_COMMAND -o plugin/quota= -f -u $users backup $MAILDIR_TYPE:$LOCATION
	fi
 
	# Check the status of dsync and continue the script depending on the result.
	if [ "$?" != "0" ]; then
		case "$?" in
		1)	log "Synchronization failed > user: $users !!!"
			;;
		2)	log "Synchronization was done without errors, but some changes couldn't be done, so the mailboxes aren't perfectly synchronized for user: $users !!!"
			;;
		esac
		if [ "$?" -gt "3" ]; then
			log "Synchronization failed > user: $users !!!"
		fi
 
		((VAR_COUNT_FAIL++))
		VAR_FAILED_USER+=($users);
	else
        	log "Synchronization done for user: $users ..."
 
		cd $DIR_TEMP
 
		log "Packaging to archive for user: $users ..."
		if [ "${VAR_OS,,}" = "freebsd" ] || [ "${VAR_OS,,}" = "openbsd" ] ; then
			$TAR_COMMAND -cvzf $users-$FILE_BACKUP $USERPART
		else
			$TAR_COMMAND -cvzf $users-$FILE_BACKUP $USERPART --atime-preserve --preserve-permissions
		fi
 
		log "Delete mailbox files for user: $users ..."
		$RM_COMMAND -rf "$DIR_TEMP/$DOMAINPART"
		if [ "$?" != "0" ]; then
        		logline "Delete mailbox files at: $DIR_TEMP " false
		else
        		logline "Delete mailbox files at: $DIR_TEMP " true
		fi
 
		log "Copying archive file for user: $users ..."
		$MV_COMMAND "$DIR_TEMP/$users-$FILE_BACKUP" "$DIR_BACKUP"
		if [ "$?" != "0" ]; then
        		logline "Move archive file for user to: $DIR_BACKUP " false
		else
        		logline "Move archive file for user to: $DIR_BACKUP " true
		fi
 
		cd $DIR_BACKUP
 
		log "Delete archive files for user: $users ..."
		(ls -t $users-$FILE_DELETE|head -n $BACKUPFILES_DELETE;ls $users-$FILE_DELETE)|sort|uniq -u|xargs -r rm
		if [ "$?" != "0" ]; then
        		logline "Delete old archive files from: $DIR_BACKUP " false
		else
        		logline "Delete old archive files from: $DIR_BACKUP " true
		fi
	fi
 
	log "Ended backup process for user: $users ..."
        log ""
done
 
# Delete the temporary folder DIR_TEMP.
$RM_COMMAND -rf $DIR_TEMP
if [ "$?" != "0" ]; then
	logline "Delete temporary '$DIR_TEMP' folder " false
	error 42
else
	logline "Delete temporary '$DIR_TEMP' folder " true
	log ""
fi
 
# Set ownership to backup directory, again.
$CHOWN_COMMAND -R $MAILDIR_USER:$MAILDIR_GROUP $DIR_BACKUP
if [ "$?" != "0" ]; then
       	logline "Set ownership of DIR_BACKUP to $MAILDIR_USER:$MAILDIR_GROUP " false
	error 43
else
       	logline "Set ownership of DIR_BACKUP to $MAILDIR_USER:$MAILDIR_GROUP " true
fi
 
# Set rights permission to backup directory.
$CHMOD_COMMAND 700 $DIR_BACKUP
if [ "$?" != "0" ]; then
       	logline "Set permission of DIR_BACKUP to drwx------ " false
	error 44
else
       	logline "Set permission of DIR_BACKUP to drwx------ " true
fi
 
# Set rights permissions to backup files.
$CHMOD_COMMAND -R 600 $DIR_BACKUP/*
if [ "$?" != "0" ]; then
       	logline "Set file permissions in DIR_BACKUP to -rw------- " false
	error 45
else
       	logline "Set file permissions in DIR_BACKUP to -rw------- " true
	log ""
fi
 
# Delete LOCK file.
if [ "$?" != "0" ]; then
        retval $?
        log ""
        $RM_COMMAND -f $FILE_LOCK
	error 99
else
	headerblock "End backup $SCRIPT_NAME "
        log ""
fi
 
# Finish syncing with runntime statistics.
headerblock "Runtime statistics "
log ""
log "- Number of determined users: $VAR_COUNT_USER"
log "- ...Summary of failed users: $VAR_COUNT_FAIL"
 
if [ "$VAR_COUNT_FAIL" -gt "0" ]; then
	log "- ...Mailbox of failed users: "
	for i in "${VAR_FAILED_USER[@]}"
	do
		log "- ... $i"
	done
fi
 
log ""
END_TIMESTAMP=`$DATE_COMMAND '+%s'`
if [ "${VAR_OS,,}" = "freebsd" ] || [ "${VAR_OS,,}" = "openbsd" ] ; then
        DELTA=$((END_TIMESTAMP-RUN_TIMESTAMP))
        log "$(printf 'Runtime: %02d:%02d:%02d time elapsed.\n' $((DELTA/3600)) $((DELTA%3600/60)) $((DELTA%60)))"
else
	log "Runtime: `$DATE_COMMAND -u -d "0 $END_TIMESTAMP seconds - $RUN_TIMESTAMP seconds" +'%H:%M:%S'` time elapsed."
fi
log ""
headerblock "Finished creating the backups [`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%z)'`]"
log ""
 
# If errors occurred on user backups, exit with return code 1 instead of 0.
if [ "$VAR_COUNT_FAIL" -gt "0" ]; then
        sendmail ERROR
	# Move the log to the permanent log file.
	movelog
	exit 1
else
	# Status e-mail.
	if [ $MAIL_STATUS = 'Y' ]; then
        	sendmail STATUS
	fi
	# Move the log to the permanent log file.
	movelog
	exit 0
fi

Beispieldatei: FILE_USERLIST

Nachfolgend eine Beispiel für den Inhalt einer Datei, welche unter der Variable:

# CUSTOM - Path and file name of a file with e-mail addresses to backup, if
#          SET. If NOT, the script will determine all mailboxes by default.
# FILE_USERLIST='/path/and/file/name/of/user/list/with/one/email/per/line'
# - OR -
# FILE_USERLIST=''
FILE_USERLIST='/srv/dovecot_backup.userlist'

definiert werden kann:

klaus@tachtler.net
petra@tachtler.net

oder

klaus
petra

Skript - Log

Nachfolgende Log-Datei entsteht im Verzeichnis

  • /var/log/ - hier z.B. /var/log/dovecot_backup.log

OHNE FILE_USERLIST:

# /root/Install/scripts/dovecot_backup.sh

+-------------------------------------------------------------------+
| Start backup of the mailboxes [Sat, 02 Oct 2021 08:18:37 (+0200)] |
+-------------------------------------------------------------------+

SCRIPT_NAME.................: dovecot_backup

OS_TYPE.....................: linux-gnu

COMPRESSION.................: gz

TMP_FOLDER..................: /srv/backup
DIR_BACKUP..................: /srv/backup

MAIL_RECIPIENT..............: you@example.com
MAIL_STATUS.................: N

FILE_USERLIST...............:
FILE_USERLIST_VALIDATE_EMAIL: N

Check if command 'dsync'|'/bin/dsync' was found ...........[ OK ]
Check if command 'tar'|'/bin/tar' was found ...............[ OK ]
Check if command 'touch'|'/bin/touch' was found ...........[ OK ]
Check if command 'rm'|'/bin/rm' was found .................[ OK ]
Check if command 'cat'|'/bin/cat' was found ...............[ OK ]
Check if command 'date'|'/bin/date' was found .............[ OK ]
Check if command 'mkdir'|'/bin/mkdir' was found ...........[ OK ]
Check if command 'chown'|'/bin/chown' was found ...........[ OK ]
Check if command 'chmod'|'/bin/chmod' was found ...........[ OK ]
Check if command 'grep'|'/bin/grep' was found .............[ OK ]
Check if command 'mktemp'|'/bin/mktemp' was found .........[ OK ]
Check if command 'mv'|'/bin/mv' was found .................[ OK ]
Check if command 'stat'|'/bin/stat' was found .............[ OK ]
Check if command 'sendmail'|'/sbin/sendmail' was found ....[ OK ]
Check if command 'gzip'|'/bin/gzip' was found .............[ OK ]
Check if the script is NOT already runnig .................[ OK ]
Check if TMP_FOLDER exists ................................[ OK ]
Check if TMP_FOLDER owner is vmail ........................[ OK ]
Check if TMP_FOLDER group is vmail ........................[ OK ]
Check if DIR_BACKUP exists ................................[ OK ]
Check if DIR_BACKUP owner is vmail ........................[ OK ]
Check if DIR_BACKUP group is vmail ........................[ OK ]

Check if the variable FILE_USERLIST is set ................[ NO ]
Mailboxes to backup will be determined by doveadm user "*".

+-----------------------------------------------------------------+
| Run backup dovecot_backup ..................................... |
+-----------------------------------------------------------------+

Create temporary '/srv/backup/dovecot_backup-0UCtpQfhdsiH' folder [ OK ]

Set ownership of DIR_TEMP to vmail:vmail ..................[ OK ]

Start backup process for user: ...
...
...
...
...
Set ownership of DIR_BACKUP to vmail:vmail ................[ OK ]
Set permission of DIR_BACKUP to drwx------ ................[ OK ]
Set file permissions in DIR_BACKUP to -rw------- ..........[ OK ]

+-----------------------------------------------------------------+
| End backup dovecot_backup ..................................... |
+-----------------------------------------------------------------+

+-----------------------------------------------------------------+
| Runtime statistics ............................................ |
+-----------------------------------------------------------------+

- Number of determined users: 4
- ...Summary of failed users: 0

Runtime: 00:01:54 time elapsed.

+-------------------------------------------------------------------+
| Finished creating the backups [Sat, 02 Oct 2021 08:20:31 (+0200)] |
+-------------------------------------------------------------------+

MIT FILE_USERLIST:

# /root/Install/scripts/dovecot_backup.sh

+-------------------------------------------------------------------+
| Start backup of the mailboxes [Sat, 02 Oct 2021 08:06:04 (+0200)] |
+-------------------------------------------------------------------+

SCRIPT_NAME.................: dovecot_backup

OS_TYPE.....................: linux-gnu

COMPRESSION.................: gz

TMP_FOLDER..................: /srv/backup
DIR_BACKUP..................: /srv/backup

MAIL_RECIPIENT..............: you@example.com
MAIL_STATUS.................: N

FILE_USERLIST...............: /srv/dovecot_backup.userlist
FILE_USERLIST_VALIDATE_EMAIL: N

Check if command 'dsync'|'/bin/dsync' was found ...........[ OK ]
Check if command 'tar'|'/bin/tar' was found ...............[ OK ]
Check if command 'touch'|'/bin/touch' was found ...........[ OK ]
Check if command 'rm'|'/bin/rm' was found .................[ OK ]
Check if command 'cat'|'/bin/cat' was found ...............[ OK ]
Check if command 'date'|'/bin/date' was found .............[ OK ]
Check if command 'mkdir'|'/bin/mkdir' was found ...........[ OK ]
Check if command 'chown'|'/bin/chown' was found ...........[ OK ]
Check if command 'chmod'|'/bin/chmod' was found ...........[ OK ]
Check if command 'grep'|'/bin/grep' was found .............[ OK ]
Check if command 'mktemp'|'/bin/mktemp' was found .........[ OK ]
Check if command 'mv'|'/bin/mv' was found .................[ OK ]
Check if command 'stat'|'/bin/stat' was found .............[ OK ]
Check if command 'sendmail'|'/sbin/sendmail' was found ....[ OK ]
Check if command 'gzip'|'/bin/gzip' was found .............[ OK ]
Check if the script is NOT already runnig .................[ OK ]
Check if TMP_FOLDER exists ................................[ OK ]
Check if TMP_FOLDER owner is vmail ........................[ OK ]
Check if TMP_FOLDER group is vmail ........................[ OK ]
Check if DIR_BACKUP exists ................................[ OK ]
Check if DIR_BACKUP owner is vmail ........................[ OK ]
Check if DIR_BACKUP group is vmail ........................[ OK ]

Check if the variable FILE_USERLIST is set ................[ OK ]
Mailboxes to backup will be read from file.

- File: [/srv/dovecot_backup.userlist]
- Check if FILE_USERLIST exists ...........................[ OK ]
- Check if FILE_USERLIST is readable ......................[ OK ]

+-----------------------------------------------------------------+
| Run backup dovecot_backup ..................................... |
+-----------------------------------------------------------------+

Create temporary '/srv/backup/dovecot_backup-OnUxredsCgti' folder [ OK ]

Set ownership of DIR_TEMP to vmail:vmail ..................[ OK ]

Start backup process for user: klaus@tachtler.net ...
Extract mailbox data for user: klaus@tachtler.net ...
...
...
Synchronization done for user: klaus@tachtler.net ...
Packaging to archive for user: klaus@tachtler.net ...
...
...
Delete mailbox files for user: klaus@tachtler.net ...
Delete mailbox files at: /srv/backup/dovecot_backup-OnUxredsCgti [ OK ]
Copying archive file for user: klaus@tachtler.net ...
Move archive file for user to: /srv/backup ................[ OK ]
Delete archive files for user: klaus@tachtler.net ...
Delete old archive files from: /srv/backup ................[ OK ]
Ended backup process for user: klaus@tachtler.net ...

Start backup process for user: petra@tachtler.net ...
Extract mailbox data for user: petra@tachtler.net ...
...
...
Synchronization done for user: petra@tachtler.net ...
Packaging to archive for user: petra@tachtler.net ...
...
...
Delete mailbox files for user: petra@tachtler.net ...
Delete mailbox files at: /srv/backup/dovecot_backup-OnUxredsCgti [ OK ]
Copying archive file for user: petra@tachtler.net ...
Move archive file for user to: /srv/backup ................[ OK ]
Delete archive files for user: petra@tachtler.net ...
Delete old archive files from: /srv/backup ................[ OK ]
Ended backup process for user: petra@tachtler.net ...

Delete temporary '/srv/backup/dovecot_backup-OnUxredsCgti' folder [ OK ]

Set ownership of DIR_BACKUP to vmail:vmail ................[ OK ]
Set permission of DIR_BACKUP to drwx------ ................[ OK ]
Set file permissions in DIR_BACKUP to -rw------- ..........[ OK ]

+-----------------------------------------------------------------+
| End backup dovecot_backup ..................................... |
+-----------------------------------------------------------------+

+-----------------------------------------------------------------+
| Runtime statistics ............................................ |
+-----------------------------------------------------------------+

- Number of determined users: 2
- ...Summary of failed users: 0

Runtime: 00:01:46 time elapsed.

+-------------------------------------------------------------------+
| Finished creating the backups [Sat, 02 Oct 2021 08:07:50 (+0200)] |
+-------------------------------------------------------------------+

Anmerkung zur Ausführung:

:!: HINWEIS - Bis Version 1.05 !

:!: HINWEIS - Falls nachfolgende Fehlermeldung auftreten sollte:

...  INFO: Delete old archive files /srv/backup ......................[FAILED]

bedeutet dies nur, dass noch nicht genug alte Archivdateien vorhanden sind, damit diese gelöscht werden können!

Skript - Logrotate

Damit die LOG-Datei des Skriptes nicht ins unendliche wächst, sollte diese ebenfalls, wie auch die Log-Dateien von Dovecot selbst, rotiert werden.

Um die LOG-Dateien des Skriptes ebenfalls wie die LOG-Dateien von Dovecot selbst zu rotieren ist nachfolgende Ergänzung in der Kofigurationsdatei

  • /etc/logrotate.d/dovecot

erforderlich:

VORHER: (Komplette Konfigurationsdatei)

# dovecot SIGUSR1: Re-opens the log files.
/var/log/dovecot.log {
  missingok
  notifempty
  delaycompress
  sharedscripts
  postrotate
    /bin/kill -USR1 `cat /var/run/dovecot/master.pid 2>/dev/null` 2> /dev/null || true
  endscript
}

NACHHER: (Komplette Konfigurationsdatei)

# dovecot SIGUSR1: Re-opens the log files.
/var/log/dovecot.log /var/log/dovecot_backup.log {
  missingok
  notifempty
  delaycompress
  sharedscripts
  postrotate
    /bin/kill -USR1 `cat /var/run/dovecot/master.pid 2>/dev/null` 2> /dev/null || true
  endscript
}

Nachfolgend die geänderte Zeile (Nur relevanter Ausschnitt):

...
/var/log/dovecot.log /var/log/dovecot_backup.log {
...

Skript - cron-Job

Nachfolgend kann auch ein cron-Job zur z.B. nächtlichen Ausführung eingerichtet werden. Dazu können nachfolgende zwei Konfigurationszeilen an die bestehende Konfigurationsdatei

  • /etc/crontab

angehängt werden:

# Backup dovecot mailboxes
5 03 * * * root /usr/local/bin/dovecot_backup.sh > /dev/null 2>&1

* Ausführung nächtlich um 03:05 Uhr

Skript - Dateien

Nachfolgend sollten nachfolgende Dateien im Backup-Verzeichnis, welches im Skript definiert ist, erstellt worden sein, nachdem das Skript zur Ausführung gekommen ist. Hier z.B. /srv/backup/, was mit nachfolgendem Befehl überprüft werden kann:

# ls -la /srv/backup/
total 50108
drwx------  2 vmail vmail     4096 Apr 29 15:11 .
drwxr-xr-x. 4 root  root      4096 Apr 29 13:03 ..
-rw-------  1 vmail vmail 51290558 Apr 29 15:11 klaus@tachtler.net-dovecot_backup_20140429_151044.tar.gz
-rw-------  1 vmail vmail 21230432 Apr 29 15:12 petra@tachtler.net-dovecot_backup_20140429_151044.tar.gz

Recovery

Die Erstellung eines Backups (Sicherung) ist eine Sache, jedoch wie können im Fall eines Datenverlusts, die Daten wiederhergestellt werden.

Entpacken

Nachfolgendes Beispiel zeigt, wie zuerst die im Backup enthaltenen Daten in eine Verzeichnis unterhalb von

  • /tmp

wiederhergestellt werden können.

Dazu kann mit nachfolgendem Befehl ein Verzeichnis mit dem Namen recovery unterhalb des Verzeichnisses /tmp angelegt werden:

# mkdir /tmp/recovery

Anschließend kann dann mit nachfolgendem Befehl das komplette Maildir-Verzeichnis aller Benutzer unterhalb einer ganzen Domäne wiederhergestellt werden:

Als Beispiel sind hier nachfolgende Gegebenheiten angenommen:

  1. Das Backup befindet sich unter /srv/backup
  2. Der Benutzer lautet klaus@tachtler.net

*.tar.gz-Komprimierungsmethode

# tar -avzf /srv/backup/klaus@tachtler.net-dovecot_backup_20140429_151044.tar.gz -C /tmp/recovery --atime-preserve --preserve-permissions

*.tar.zst-Komprimierungsmethode

# tar -avzf /srv/backup/klaus@tachtler.net-dovecot_backup_20140429_151044.tar.zst -C /tmp/recovery --atime-preserve --preserve-permissions

:!: HINWEIS - Der Paramter -a beim Befehl tar hat nachfolgende Bedeutung:

-a, --auto-compress
              use archive suffix to determine the compression program

Nach erfolgreicher Ausführung des oben gezeigten Befehls, kann mit nachfolgendem Befehl überprüft werden, ob alle Daten aus der Backup-Datei erfolgreich extrahiert werden konnten:

# ls -la /tmp/recovery/tachtler.net/klaus/Maildir
total 464
drwx------ 30 vmail vmail   4096 Apr 29 15:11 .
drwx------  3 vmail vmail   4096 Apr 29 15:10 ..
drwx------  2 vmail vmail  36864 Apr 29 15:11 cur
-rw-------  1 vmail vmail      0 Apr 29 15:10 dovecot-acl-list
-rw-------  1 vmail vmail 260484 Apr 29 15:11 dovecot.index.cache
-rw-------  1 vmail vmail   8640 Apr 29 15:11 dovecot.index.log
-rw-------  1 vmail vmail     24 Apr 29 15:11 dovecot-keywords
-rw-------  1 vmail vmail    648 Apr 29 15:10 dovecot.mailbox.log
-rw-------  1 vmail vmail  20649 Apr 29 15:11 dovecot-uidlist
-rw-------  1 vmail vmail      8 Apr 29 15:10 dovecot-uidvalidity
-r--r--r--  1 vmail vmail      0 Apr 29 15:10 dovecot-uidvalidity.535fa4ee
drwx------  5 vmail vmail   4096 Apr 29 15:11 .Drafts
drwx------  5 vmail vmail   4096 Apr 29 15:11 .Junk
-rw-------  1 vmail vmail      0 Apr 29 15:10 maildirfolder
-rw-------  1 vmail vmail     81 Apr 29 15:11 maildirsize
drwx------  2 vmail vmail   4096 Apr 29 15:10 new
drwx------  5 vmail vmail   4096 Apr 29 15:11 .Sent
-rw-------  1 vmail vmail    574 Apr 29 15:10 subscriptions
drwx------  2 vmail vmail   4096 Apr 29 15:11 tmp
drwx------  5 vmail vmail   4096 Apr 29 15:11 .Trash

dsync-Befehl

Nachfolgender Befehl kopiert dann im laufenden Betrieb, die Daten in das Maildir-Verzeichnis von Dovecot zurück.

Als Beispiel sind hier nachfolgende Gegebenheiten angenommen:

  1. Das Backup befindet sich unter /tmp/recovery/tachtler.net/klaus/Maildir/
  2. Der Benutzer lautet klaus@tachtler.net
# dsync -R -f -u klaus@tachtler.net backup maildir:/tmp/recovery/tachtler.net/klaus/Maildir

Mit nachfolgendem Befehl kann nun überprüft werden, ob das recovery (zurück kopieren) erfolgreich war.

Als Beispiel sind hier nachfolgende Gegebenheiten angenommen:

  1. Das Backup befindet sich unter /tmp/recovery/tachtler.net/klaus/Maildir/
  2. Der Benutzer lautet klaus@tachtler.net
  3. Das Maildir-Verzeichnis unter Dovecot befindet sich unter /var/spool/vmail/tachtler.net/klaus/Maildir
# ls -la /var/spool/vmail/tachtler.net/klaus/Maildir
total 480
drwx------ 30 vmail vmail   4096 Apr 29 16:27 .
drwx------  4 vmail vmail   4096 Apr 29 12:27 ..
drwx------  2 vmail vmail  36864 Apr 29 16:27 cur
-rw-------  1 vmail vmail      0 Apr 29 16:26 dovecot-acl-list
-rw-------  1 vmail vmail 260484 Apr 29 16:27 dovecot.index.cache
-rw-------  1 vmail vmail   8556 Apr 29 16:27 dovecot.index.log
-rw-------  1 vmail vmail    648 Apr 29 16:26 dovecot.mailbox.log
-rw-------  1 vmail vmail  20649 Apr 29 16:27 dovecot-uidlist
-rw-------  1 vmail vmail      8 Apr 29 16:26 dovecot-uidvalidity
-r--r--r--  1 vmail vmail      0 Apr 29 16:26 dovecot-uidvalidity.535fb6a3
drwx------  5 vmail vmail   4096 Apr 29 16:27 .Drafts
drwx------  6 vmail vmail   4096 Apr 29 16:27 .Junk
-rw-------  1 vmail vmail      0 Apr 29 16:26 maildirfolder
-rw-------  1 vmail vmail     25 Apr 29 16:27 maildirsize
drwx------  2 vmail vmail   4096 Apr 29 16:26 new
drwx------  5 vmail vmail   4096 Apr 29 16:27 .Sent
-rw-------  1 vmail vmail    574 Apr 29 16:26 subscriptions
drwx------  2 vmail vmail  24576 Apr 29 16:27 tmp
drwx------  5 vmail vmail   4096 Apr 29 16:27 .Trash

Login-Test mit telnet

Um zu Überprüfen, ob eine Anmeldung als Benutzer von einem entfernten Rechner möglich ist, kann nachfolgender Befehl genutzt werden:

# telnet 192.168.0.80 143
Trying 192.168.0.80...
Connected to 192.168.0.80.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE STARTTLS AUTH=PLAIN AUTH=DIGEST-MD5
AUTH=CRAM-MD5 AUTH=LOGIN] Dovecot ready.
a1 login klaus@tachtler.net geheim
a1 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY
THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN
NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH
LIST-STATUS SPECIAL-USE BINARY MOVE COMPRESS=DEFLATE QUOTA ACL RIGHTS=texk] Logged in
a2 list "" "*"
* LIST (\HasChildren) "/" INBOX
* LIST (\HasNoChildren \Junk) "/" INBOX/Junk
* LIST (\HasNoChildren \Sent) "/" INBOX/Sent
* LIST (\HasNoChildren \Trash) "/" INBOX/Trash
* LIST (\HasNoChildren \Drafts) "/" INBOX/Drafts
a2 OK List completed.
a3 logout
* BYE Logging out
a3 OK Logout completed.
Connection closed by foreign host.

Erforderliche Benutzereingaben:

  1. telnet 192.168.0.80 143
  2. a1 login klaus@tachtler.net geheim
  3. a2 list "" "*"
  4. a3 logout
Diese Website verwendet Cookies. Durch die Nutzung der Website stimmen Sie dem Speichern von Cookies auf Ihrem Computer zu. Außerdem bestätigen Sie, dass Sie unsere Datenschutzbestimmungen gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website.Weitere Information
tachtler/dovecot_backup_-_skript.txt · Zuletzt geändert: 2024/09/20 16:56 von klaus