Files
BackupScripts/mailserver_backup.sh
2025-05-18 17:54:25 +02:00

112 lines
4.3 KiB
Bash

#!/bin/bash
# Coded by Mike Peters <mike@skylake.me>
#
# Backup useable with docker-mailserver: https://github.com/tomav/docker-mailserver
#
# NOTE: This script need to be executed as root!
# If you change the backup user name the Backup should be readable for him.
# Every single detail is saved in the LOGFILE.
# It may happen that you receive errors in the mail-stats backup section, this is normal because tar can't access socket files.
# The Backup will delete Backup older than 3 days, this can be change in the specified section.
# !!! Pay attention that no third party should get your backup !!!
#
# TODO: Log error messages.
# Encrypt Backup with public GPG key.
#
# Security Issues:
# * The EMails of every user are in the backup accessible, so the backup user can see every email.
# A possibility to prevent that is that the backup would be encrypted with a Public Key.
# * Also the script needs to executed by root which is not a good idea, but only root has access to the mailboxes.
# * The encrypted passwords and the usernames will be backedup.
#
# I hope this can help somebody!
#
TIMESTAMP="$(date +'%Y-%m-%d')" # DATETIME like: 2019-09-08
BACKUP_DIR="/tmp/backup" # Folder where the Backup should be saved
LOGFILE="$BACKUP_DIR/"backup_log.txt # Logfile containing every detail
SRCDIR="/home/user/mail/mail/" # Location of docker-mailserver
BACKUP_USER="backup" # Backup user, the backup is after execution available for him.
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] automatic mail backup started" >> "$LOGFILE"
# Creating backup folder and tmp folder
mkdir -p $BACKUP_DIR
mkdir -p $BACKUP_DIR/tmp
# Naigating to Source Directory
cd $SRCDIR
# Backup Mailboxes
for domain in *;
do
cd $SRCDIR
# Search for domains
if [ -d "$domain" ]; then
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] folder for domain $domain found" >> "$LOGFILE"
cd $domain/
else
continue
fi
# Search for users in the domain
for mailbox in *;
do
cd $SRCDIR/$domain/
if [ -d "$mailbox" ]; then
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] folder for mailbox $mailbox found" >> "$LOGFILE"
EMAIL="$mailbox"@"$domain"
FILENAME="$TIMESTAMP"_mailbox_backup_"$EMAIL".tar.gz
cd $mailbox/
else
continue
fi
# Backup Mails
echo Create backup for user "$EMAIL".
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] mail backup for $EMAIL started" >> "$LOGFILE"
tar -cpzf $BACKUP_DIR/tmp/$FILENAME .
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] mail backup for $EMAIL finished" >> "$LOGFILE"
done
done
# Creating Config Backup
cd $SRCDIR/../config/
FILENAME="$TIMESTAMP"_mail_config_backup.tar.gz
echo Create backup of config directory
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] config backup started" >> "$LOGFILE"
tar -cpzf $BACKUP_DIR/tmp/$FILENAME .
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] config backup finished" >> "$LOGFILE"
# Creating Config Backup
cd $SRCDIR/../mail-state/
FILENAME="$TIMESTAMP"_mail_stats_backup.tar.gz
echo Create backup of mail stats directory
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] mail-stats backup started" >> "$LOGFILE"
tar -cpzf $BACKUP_DIR/tmp/$FILENAME .
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] mail-stats backup finished" >> "$LOGFILE"
# Creating Archive of Backups
cd $BACKUP_DIR/tmp/
FILENAME="$TIMESTAMP"_mail_backup.tar.gz
echo Packing everything in a single archive
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] creating backup archive" >> "$LOGFILE"
tar -cpzf $BACKUP_DIR/$FILENAME .
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] creation of backup archive has been finished" >> "$LOGFILE"
# Delete entries older than 3 days: mtime always 3-1 so in this case +2
# SOURCE: https://unix.stackexchange.com/questions/92346/why-does-find-mtime-1-only-return-files-older-than-2-days
cd $BACKUP_DIR
find "$BACKUP_DIR" -name '*mail_backup*' -type f -mtime +7 -exec rm -f {} \;
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] old files deleted" >> "$LOGFILE"
# Remove tmp directory
rm -rf $BACKUP_DIR/tmp
# Set Correct File Permissions
chown -R $BACKUP_USER:$BACKUP_USER "$BACKUP_DIR"
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] file permission changed" >> "$LOGFILE"
echo $(date +'%d-%m-%Y %H:%M:%S') "[MAIL] operation finished" >> "$LOGFILE"