aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/backup-mail
blob: c37af58461bcb3178bcd5a436791f6bd159a9117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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