Backup mails – setup of a dovecot server
Over the years, I have received a lot of mails, not all of them were moved to trash. Time to think about a solution to backup them and have way to restore them, if ever needed.
After a little thinking, I decided to use the following technics:
- Setup a local imap server (Dovecot) in Docker
- Setup local users in the dovecot server to sync the mails into it.
- Backup the mails stored in Maildir format by Dovecot with incremental backups.
- Try to restore the backup into a testaccount, to validate, the this is working
Currently I will execute the backup and restore steps manually. If I think after some time, it is bulletprof, I will think about how to automate. Right now the focus is on setup the infrastructure and commands to make backups possible.
How to setup a local dovecot server with Docker
I will run the Dovecot instance on one of my computers, currently it is running on my notebook.
Dockerfile
I use Ubuntu 24.04 for the Dovecot instance.
FROM ubuntu:24.04
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y dovecot-imapd locales \
&& apt-get clean \
&& locale-gen de_DE.UTF-8 \
&& update-locale LANG=de_DE.UTF-8 LC_MESSAGES=POSIX
COPY ./files/dovecot.conf /etc/dovecot/dovecot.conf
COPY ./files/addDovecotUser.sh /addDovecotUser.sh
RUN chmod +x /addDovecotUser.sh
ENV LANG=de_DE.utf8
EXPOSE 143
ENTRYPOINT ["dovecot", "-F"]
addDovecotUser.sh
To setup a user in the Dovecot instance, I have written the following script, which should be placed in the folder files in the folder, where the Dockerfile is placed.
#!/bin/bash
adduser $1
usermod -aG users $1
usermod -aG dovecot $1
mkdir -p /home/$1/Maildir/cur
mkdir -p /home/$1/Maildir/new
mkdir -p /home/$1/Maildir/tmp
chown -R $1:$1 /home/$1
dovecot.conf
Because I run and access the Dovecot instance only locally, I did not setup a configuration with SSL.
listen = *
auth_mechanisms = plain login
#log_path = /var/log/dovecot.log
log_path = /dev/stdout
mail_location = maildir:~/Maildir
protocols = imap
service imap-login {
inet_listener imap {
port = 143
}
}
ssl = no
disable_plaintext_auth = no
userdb {
driver = passwd
}
passdb {
driver = pam
}
protocol imap {
imap_idle_notify_interval = 2 mins
imap_max_line_length = 64 k
imap_client_workarounds = tb-extra-mailbox-sep
}
mail_max_userip_connections = 20
Docker Compose
At the end I use Docker Compose to setup and run the Dovecot instance. I limit the ressources for the Dovecot, for me it was enough.
services:
dovecot-server:
image: dovecot-server:latest
restart: always
cpus: 0.50
mem_limit: 1024m
mem_reservation: 256m
volumes:
- dovecot-server-home-volume:/home
networks:
- dovecot-net
ports:
- "143:143"
networks:
dovecot-net:
volumes:
dovecot-server-home-volume:
With
docker compose up
you can start the local dovecot instance (I use Portainer with stacks to setup the infrastructure).
Setup a mailaccount in Dovecot
To create a new mailaccount in your Dovecot instance, you can execute bash inside the container and use it to execute the shell script. First execute the bash:
docker exec -it dovecot-server-dovecot-server-1 bash
Then execute the shell script and give the account name as an argument. It will ask you some more infos, which are not needed (except the password) and can be left empty (just press return):
root@f41d9f10fa99:/# ./addDovecotUser.sh testNow you should have an imap account which could be used.
info: Adding user `test' ...
info: Selecting UID/GID from range 1000 to 59999 ...
info: Adding new group `test' (1004) ...
info: Adding new user `test' (1004) with group `test (1004)' ...
info: Creating home directory `/home/test' ...
info: Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
Full Name []: testaccount
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
info: Adding new user `test' to supplemental / extra groups `users' ...
info: Adding user `test' to group `users' ...
root@f41d9f10fa99:/#
Server: localhost
Port: 143
Connection security: none
Auth method: password, unencrypted
Next step will be setup of imapsync, which will be used to sync the mails by imap protocol.
[…] For backup of my mails, I have started with setup a local Dovecot instance (Backup mails – setup of a dovecot server). […]
[…] setting up a Dovecot instance and having imapsync running, how could I do the main task, the […]