From fcda9808eeb079568e8ab616b999db1dc3eccc27 Mon Sep 17 00:00:00 2001 From: kywo Date: Sun, 18 May 2025 17:54:25 +0200 Subject: [PATCH] first commit --- README.md | 17 +++++++ mailserver_backup.sh | 111 +++++++++++++++++++++++++++++++++++++++++++ ts3_backup.sh | 17 +++++++ 3 files changed, 145 insertions(+) create mode 100644 README.md create mode 100644 mailserver_backup.sh create mode 100644 ts3_backup.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..ec4a837 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Backup Scripts + +This repo holds a collection of backup scripts + +# Installation + +Create a cronjob that runs every day at 2:00 + +```bash +0 2 * * * /home/kywo/voice/ts3/ts3_backup.sh 2>&1 +``` + +Make sure that the file is executable + +```bash +chmod 755 /home/kywo/voice/ts3/ts3_backup.sh +``` diff --git a/mailserver_backup.sh b/mailserver_backup.sh new file mode 100644 index 0000000..9d117a0 --- /dev/null +++ b/mailserver_backup.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# Coded by Mike Peters +# +# 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" diff --git a/ts3_backup.sh b/ts3_backup.sh new file mode 100644 index 0000000..419e15e --- /dev/null +++ b/ts3_backup.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# === KONFIGURATION === +SRC="/home/kywo/voice/ts3/data" +DEST="/home/kywo/voice/ts3/backups" +DATE=$(date +%Y-%m-%d) +BACKUP_NAME="ts3_backup_$DATE.tar.gz" + +# === VERZECIHNISS ERSTELLEN === +mkdir -p $DEST + +# === BACKUP AUSFÜHREN === +tar --exclude-from=./exclude.txt -czf "$DEST/$BACKUP_NAME" -C "$SRC" . +chown kywo:kywo "$DEST/$BACKUP_NAME" + +# Optional: Alte Backups löschen (älter als 7 Tage) +find "$DEST" -name "ts3_backup_*.tar.gz" -mtime +30 -delete