aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xscripts/backup-mail78
-rw-r--r--system/backup-mail.service11
-rw-r--r--system/backup-mail.timer10
3 files changed, 99 insertions, 0 deletions
diff --git a/scripts/backup-mail b/scripts/backup-mail
new file mode 100755
index 0000000..c37af58
--- /dev/null
+++ b/scripts/backup-mail
@@ -0,0 +1,78 @@
+#!/usr/bin/env bash
+
+timestamp="$(date +"%Y%m%d-%H%M%S")-"
+
+# If the backup location doesn't exist yet, create it
+if [ ! -d $mail_folder_destination ]; then
+ mkdir -p $mail_folder_destination
+fi
+
+# If the parent directory of the backup source exists, enter it
+if [ -d $mail_folder_source ]; then
+ cd $mail_folder_source
+else
+ exit 1
+fi
+
+# Loop through all subdirectories of $mail_folder_source
+for subfolder in $mail_folder_source*
+do
+ # use basename
+ subfolder=$(basename $subfolder)
+ # If it is a folder
+ if [ -d $subfolder ]; then
+ cd $subfolder
+ # If the backup location for that domain doesn't exist yet, create it
+ if [ ! -d $mail_folder_destination$subfolder ]; then
+ mkdir -p $mail_folder_destination$subfolder
+ fi
+ # Loop through all subsubdirectories (mailboxes) of $subfolder (domain name)
+ for subsubfolder in $mail_folder_source$subfolder/*
+ do
+ # use basename
+ subsubfolder=$(basename $subsubfolder)
+ echo "$subsubfolder --> $mail_folder_destination$subfolder/$timestamp$subsubfolder$tar_suffix$gpg_suffix"
+ # tar to /tmp
+ tar cfJ $tmp$timestamp$subsubfolder$tar_suffix $subsubfolder
+ # gpg to backup location
+ gpg -e \
+ -r "$gpg_public_key" \
+ -o "$mail_folder_destination$subfolder/$timestamp$subsubfolder$tar_suffix$gpg_suffix" \
+ $tmp$timestamp$subsubfolder$tar_suffix
+ # remove tar in /tmp
+ rm -f $tmp$timestamp$subsubfolder$tar_suffix
+ done
+ fi
+done
+
+# Backup mailman mailing lists
+# If the backup location doesn't exist yet, create it
+if [ ! -d $mailman_folder_destination ]; then
+ mkdir -p $mailman_folder_destination
+fi
+
+# If the parent directory of the backup source exists, enter it
+if [ -d $mailman_folder_source ]; then
+ cd $mailman_folder_source
+else
+ exit 1
+fi
+# Loop through all subdirectories of $mailman_folder_source
+for subfolder in $mailman_folder_source*
+do
+ # use basename
+ subfolder=$(basename $subfolder)
+ if [ $subfolder = archives ] || [ $subfolder = data ] || [ $subfolder = lists ]; then
+ echo "$subfolder --> $mailman_folder_destination$timestamp$subfolder$tar_suffix$gpg_suffix"
+ # tar to /tmp
+ tar cfJ "$tmp$timestamp$subfolder$tar_suffix" $subfolder
+ # gpg to backup location
+ gpg -e \
+ -r "$gpg_public_key" \
+ -o $mailman_folder_destination$timestamp$subfolder$tar_suffix$gpg_suffix \
+ $tmp$timestamp$subfolder$tar_suffix
+ # remove tar in /tmp
+ rm -f $tmp$timestamp$subfolder$tar_suffix
+ fi
+done
+
diff --git a/system/backup-mail.service b/system/backup-mail.service
new file mode 100644
index 0000000..b211205
--- /dev/null
+++ b/system/backup-mail.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Backup mail accounts and mailman mailing lists
+ConditionFileIsExecutable=/usr/lib/systemd/scripts/backup-mail
+
+[Service]
+EnvironmentFile=/etc/backup-settings
+Nice=19
+IOSchedulingClass=2
+IOSchedulingPriority=7
+ExecStart=/usr/lib/systemd/scripts/backup-mail
+
diff --git a/system/backup-mail.timer b/system/backup-mail.timer
new file mode 100644
index 0000000..22367df
--- /dev/null
+++ b/system/backup-mail.timer
@@ -0,0 +1,10 @@
+[Unit]
+Description=Backup mail and mailman every first day of the month
+
+[Timer]
+# Every 1st of each month at 5:00
+OnCalendar=*-*-1 05:00
+Persistent=true
+
+[Install]
+WantedBy=timers.target