# Best way to do a remote backup of a mailserver



## JackOfAllTrades (May 26, 2014)

I'm currently running a mail server on FreeBSD 10.0-RELEASE-p3 with sendmail and dovecot. I have been running this configuration for a while now and everything works fine. Because *I* have been the only user on the server i have done the backup of the mail through my MUA on my local computer. But now *I* have the opportunity to host the mail for another domain and am therefore thinking about implementing some kind of remote backup solution.

What *I* can see is that the mailboxes are stored in the default directory /var/mail and the IMAP specific files in the ~/mail directory. Would it be that simple to just copy those directories and then just restore those files in the same places as before in case of a server reinstall? I read something about a tool called dsync that is included in the dovecot program that is used to simplify synchronizing of mailboxes on two different mailservers.  But *I*'m not sure if that´s the right tool for me. 

Let´s say *I* wrote a script that is running through cron every night to remotely fetch the mailboxes on the mail server with use of the scp program. To be able to login without using a password *I* would have to do the authentication with key-based authentication without using a password on the private key. Would this be a good solution or would it be a security risk?

What is the best practice in this case?


----------



## l008com (Jan 23, 2019)

* bump * I've spent the day trying to answer essentially this same question. I have yet to find any definitive answers. Which seems very odd.


----------



## nicky (Jan 23, 2019)

Please note that I use Postfix and Dovecot, and never have touched sendmail. And I don't know what best practice is, just what works for me.

I have a remote backup server, where cron runs rsync to every night. rsync only copies the differences so you don't have to transfer each file more than once. But be aware that rsync strictly speaking is not a backup tool, but a synchronizing tool, meaning that your mail store and backup are identical after each run. So if you by accident deletes a mail, and is three days to find out, you can't use this kind of backup to restore it. For that you would need incremental backups.

rsync runs like this
`rsync <options> -e 'ssh -p<port>' /srv/smtp <user>@backup:/home/<user>/backup`

The configuration and mail addresses is backed up on it's own. Note that Dovecot has some cache files in the mail store you would need to remove if you ever make use of the backup. And please test the system you start to use. Ideally by restoring from scratch.


----------



## gkontos (Jan 23, 2019)

Using rsync to backup /var/mail/ is perfectly safe and sufficient enough. Just make sure that you also use --delete flag. Example:

`rsync -avz --delete <source dir/>  <destination dir/>`


----------



## l008com (Jan 30, 2019)

I found my solution to this problem. You can use the `doveadm` command to dump each mailbox one by one without stopping the mailserver. But it's documentation is not comprehensive. It took a lot of help posts and trial and error to get it to work.

So first, my script gets a list of all mail users. I hard code this array into the script for simplicity. Then I loop through mail user array, running this command for each user:

```
sudo /path/to/doveadm -c /path/to/dovecot.conf backup -u $mail_user maildir:"/backupname/email/$mail_user"
```

Works well. A little slow but I don't have to shut down the mailserver so I'm happy with this solution.


----------

