Code Monkey home page Code Monkey logo

dovecot-backup's Introduction

dovecot-backup

This is a shell script for saving up all emails from the mailboxes of Dovecot (MDA) to an email mailbox specific archive file in tar.gz or tar.zst format.

This simple bash/shell script save the emails

  • DEFAULT from all mailboxes/user accounts, determined with doveadm user "*"
  • OR only from these mailboxes/user accounts, which are stored in a file.
  • every mailbox/user into a separate *.tar.gz or *.tar.zst file
  • reading the data from the filesystem
  • with configurable parameter
  • with automatic deletion of old backup-files
  • and with logging into a growing up log file under /var/log
  • with statistic summary at the end of the script execution
  • with runtime summary at the end of the script execution
  • on successful execution a LOG file will be written, or configurable a message will be send by e-mail.
  • on error while execution, a LOG file will be written and an error message will be send by e-mail.

A more confortable and detailed description is available under following link:

http://www.dokuwiki.tachtler.net/doku.php?id=tachtler:dovecot_backup_-_skript

(Sorry, by now, only avaliable in German language)

Full description of all the parameter to set to get the script to work, inside the top part of the script:

##############################################################################
# >>> Please edit following lines for personal settings and custom usages. ! #
##############################################################################

# CUSTOM - Script-Name.
SCRIPT_NAME='dovecot_backup'

# CUSTOM - Backup-Files compression method - (possible values: gz zst).
COMPRESSION='gz'

# CUSTOM - Backup-Files.
TMP_FOLDER='/srv/backup'
DIR_BACKUP='/srv/backup'
FILE_BACKUP=dovecot_backup_`date '+%Y%m%d_%H%M%S'`.tar.$COMPRESSION
FILE_DELETE=$(printf '*.tar.%s' $COMPRESSION)
BACKUPFILES_DELETE=14

# CUSTOM - dovecot Folders.
MAILDIR_TYPE='maildir'
MAILDIR_NAME='Maildir'
MAILDIR_USER='vmail'
MAILDIR_GROUP='vmail'

# CUSTOM - Path and file name of a file with e-mail addresses to backup, if
#          SET. If NOT, the script will determine all mailboxes by default.
# FILE_USERLIST='/path/and/file/name/of/user/list/with/one/user/per/line'
# - OR -
# FILE_USERLIST=''
FILE_USERLIST=''

# CUSTOM - Check when FILE_USERLIST was used, if the user per line was a
#          valid e-mail address [Y|N].
FILE_USERLIST_VALIDATE_EMAIL='N'

# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='[email protected]'

# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
Note: The script doesn't work with Multi-dbox (mdbox). BUT following changes can solve that issue:

Please change/replace the line

$DSYNC_COMMAND -o plugin/quota= -f -u $users backup $MAILDIR_TYPE:$LOCATION

with

doveadm backup -n inbox -f -u $users $MAILDIR_TYPE:$LOCATION:LAYOUT=fs

Issue #6 - Thanks to SvenSFS

dovecot-backup's People

Contributors

tachtler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dovecot-backup's Issues

backup file expiration

Hello,

If I'm reading:

(ls $users-$FILE_DELETE -t|head -n $DAYS_DELETE;ls $users-$FILE_DELETE )|sort|uniq -u|xargs rm

correctly I think run the backup script something other than once per day, it'll delete the wrong number of backup files. Unless you mean "DAYS_DELETE" to really mean number of files to keep, which would could also be a reasonable policy.

Perhaps something similar to find <dir> -name "$users-$FILE_DELETE" -a ctime +${DAYS_DELETE} might be a better test?

Suggestion: use mail_fsync=never

# rm -rf /srv/test && mkdir /srv/test && chown vmail:vmail /srv/test
# time doveadm sync -u [email protected] mdbox:/srv/test

real	213m51.795s
user	16m15.915s
sys	1m33.472s

# rm -rf /srv/test && mkdir /srv/test && chown vmail:vmail /srv/test
# time doveadm -o mail_fsync=never sync -u [email protected] mdbox:/srv/test

real	9m41.352s
user	9m0.082s
sys	0m30.458s

Also: creating a backup in the mdbox format is 15% faster in my tests than creating a backup using the maildir format backup, so that is also something that might make sense to change (beware of hierarchy separator changes though).

Feature Request: Add summary of failed mailboxes | add error-handling

Hello, Ich use this really nice script but i have 2 requests:

First Request: Can you add some error-handling? For example in row 298

$DSYNC_COMMAND -o plugin/quota= -f -u $users backup $MAILDIR_TYPE:$LOCATION
is no error handling. Due to an bug in dovecot in version 2.3.0 and higher, the dysnc command failed in our environment for shared mailboxes because of an problem with ACLs. The script gives no feedback no matter it failed or succeded. Can you change this? Especially for:

$DSYNC_COMMAND -o plugin/quota= -f -u $users backup $MAILDIR_TYPE:$LOCATION (row 298)
$TAR_COMMAND -cvzf $users-$FILE_BACKUP $USERPART --atime-preserve --preserve-permissions (row 303)

Second Request: Add an summary for failed mailboxes so the user get an output like:

INFO: +-----------------------------------------------------------------+
INFO: | End backup dovecot_backup ..................................... |
INFO: | Successfully saved mailboxes: 15
INFO: | Errors in mailboxes:
INFO: |    - [email protected]
INFO: |    - [email protected]
INFO: |    - [email protected]
......
INFO: +-----------------------------------------------------------------+

We save around 50 mailboxes with this script. If you save so many or more mailboxes this summary
helps to determine which mailboxes have issues or not.

sincerely yours

Logs are moved before sending status email - this leads to file not found error

Hi,

first of all - great script - thanks for sharing this.

Using

MAIL_STATUS='Y'

leads to

/usr/bin/cat: /tmp/dovecot_backup.log: No such file or directory

because of line 673.

Here you call movelog which remove the FILE_LAST_LOG before sending status email which trys to cat the FILE_LAST_LOG.

I just moved those line into the error if and everything works like a charn

# If errors occurred on user backups, exit with return code 1 instead of 0.
if [ "$VAR_COUNT_FAIL" -gt "0" ]; then
        sendmail ERROR
        # Move the log to the permanent log file.
        movelog
        exit 1
else
        # Status e-mail.
        if [ $MAIL_STATUS = 'Y' ]; then
                sendmail STATUS
        fi
        # Move the log to the permanent log file.
        movelog
        exit 0
fi

I'm not sure if you need movelog within the error part, but for the none error case it works.

regards
stefan

crontab error

I currently have this error
dovecot_backup.sh: line 231: -f: command not found
But I only have it when I roll via crontab
running via crontab
0 4 * * * bash /opt/dovecot-backup/dovecot_backup.sh
running manually

bash /opt/dovecot-backup/dovecot_backup.sh

Check if command 'Usage:' was found

I got an error in 1.11 for my Ubuntu1804.
Check if command '/bin/mktemp' was found ..................[ OK ]
Check if command 'Usage:' was found .......................[FAILED]

I find the error and removed the --skip-alias to make it works.
MV_COMMAND=which mv
#MV_COMMAND=which --skip-alias mv

Feature request: only backup selected mailboxes

Good morning,

I like your script, and have also learnt some generic bash techniques I can use in unrelated scripts. However, I want to be able to select which user mailboxes I backup.

So I have expanded your script by making the following modifications:

  • Moved personal options to a separate backup configuration file that is read by the script (this configuration file is now a command line option for backup_dovecot.sh).
  • Added backup list path and filename variable to the backup configuration file that points to a list of mailboxes to back for that backup configuration.
  • Created a file listing mailboxes to backup that is read by the configuration file as per added variable to backup configuration file.
  • Created a backup user function that backup only specified user's mailbox that is called via iterating a list generated either from your original all dovecot mailboxes command or a list of users to backup if it exists. Defaults to backing up all mailboxes.

This does add to the number of files but allows the following new features:

  • One can define and schedule multiple backups each with different options for the case where not all backups should have the same configuration.
  • One can backup individual mailboxes or groups of mailboxes, each with their own backup configuration options.
  • One can easily have different backups (e.g. weekly and monthly backups saving to different locations) that backup the same set of mailboxes. Or different mailboxes.

Would you be interested in this code and how should I get it to you?

I'm still very new to GitHub, but it seems the standard method of contributing is for me to fork your repository, make changes, and issue you with a pull request that you can then (if happy with changes) merge with your code. Is this what you would like me to do?

Thanks,

graue

Mailbox doesn't allow inferior mailboxes

Hi,

Just used your script to backup my imap/pop3 server mailboxes and I got the error above from dsync trying to backup a specific user mailbox. This happens only with this specific user because there is a folder created (by the user) using their mail client (they have created a Archives folder below Inbox).

Error: Can't create mailbox Archives/2018: Mailbox doesn't allow inferior mailboxes

In my dovecot file I have enabled the LAYOUT=fs option which supposedly should solve this issue but without any improvement.

Could you please help me ?

Thanks in advance.

Keep "workdir"

How about an option to avoid deleting then decompressing the tar file, but just keeping the workdir ?
Sure it takes more space, but makes backup much faster and uses less active resources.

License confusion

The LICENSE file in the root dir says GPL-3.0:

GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

While the actual script says GPL-2.0 or later:

# +----------------------------------------------------------------------+ #
# | This program is free software; you can redistribute it and/or modify | #
# | it under the terms of the GNU General Public License as published by | #
# | the Free Software Foundation; either version 2 of the License, or | #
# | (at your option) any later version. | #
# +----------------------------------------------------------------------+ #

Change VAR_EMAILDATE timezone format to offset in hours

Not all email UIs are ok with the three letter timezone notation. Proposal is to change the notation to offset in hours

Current format is: Sun, 17 May 2020 10:41:48 (BST)
Proposed format is Sun, 17 May 2020 10:41:48 (+0100)

Line 176 would then be updated to
VAR_EMAILDATE=$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%z)'

Maybe need to implement a "tmp" dir for dsync usage and separate it from final .tar.gz destination ? (permission problems over NFS / etc mounts)

Hey there! Just wanted to try / use your script to backup dovecot maildirs / user emails.

https://pastebin.com/CB3y9Lxu

Just check this, as you can see i want to backup to a remote NFS mount, which causing permission problems. As i read the script, dsync try to do the "extract" directly to the backup target directory (/mnt/NFS/...) then create .tar.gz backup file from there.

Is there any way / chance to specify a "tmp" directory for dsync, which is different from BACKUP_DIR target ? Because it should solve the permission problems.

dsync want to use / run / put extration via the actual email user (USER.NAME in pastebin example) that will fail, because that user has 0 permission for the backup target directory. And this is good.

Any way to implement a "TMP_DSYNC_EXTRACT" directory for dsync operations, and keep BACKUP_DIR for backup target where the .tar.gz file will lands.

Sorry for my english, not my native.

Thanks!

Regards
Christian Hamar
Hungary

Script funktioniert bei MDbox nicht

Hallo,
bei der Konfiguration mit Dovecot mit IPConfig und mdbox läuft das Script nicht
richtig.

Für das Backup kann man
doveadm backup -f -n inbox -u $user maildir:/srv/backup:LAYOUT=fs
benutzen.

Eingebaut in dein Script hat das Super Funktioniert.
DANKE!!!!! für deine Arbeit.

Das Tachtler Wiki gibt immer wieder Denkanstöße.

Gruss
Sven

TMP_FOLDER

Hallo,

bei mir funktioniert das Skript nicht, wenn der TMP_FOLDER nicht dem MAILDIR_USER gehört.

Ich hab also folgendes gemacht:
if [ ! -d "$TMP_FOLDER" ]; then
logline "Check if TMP_FOLDER exists " false
$MKDIR_COMMAND -p $TMP_FOLDER
$CHOWN_COMMAND -R $MAILDIR_USER:$MAILDIR_GROUP $TMP_FOLDER

OS: Ubuntu 20.04 -> umask 027 und ich benutze dsync

Error by Run Script

Hello,

if i start the script, the following error messages is comming:
"dovecot_backup.sh: 110: dovecot_backup.sh: Syntax error: "(" unexpected"

Can you help me?

sendmail was not found by crontab

I can run your by root but if I set the crontab to run your script, I got error below.

[...]
Check if command '/bin/mv' was found ......................[ OK ]
Check if command '' was found .............................[FAILED]

I found that cron job cannot find sendmail but if I run it from root, it finds the sendmail.

# command -v sendmail
/usr/sbin/sendmail

My crontab setting:
0 0 * * * /bin/sh /root/script/backup_dovecot.sh > /dev/null 2>&1

dsync error

Hi, I found your script yesterday.

Unfortunately I got this error
dsync(): Warning: Failed to copy source UID=3350 mail: Mail size is larger than the maximum size allowed by server configuration - falling back to regular saving

Do you happen to know what I can do to fix this?
I googled around but did not find a solution (yet).

Regards, M

crontab does not work

It works fine if i run the script from command manually, but it does not work when i run it in crontab. I checked it runs from the first line but not finish at the last line. Is there any permission I need to set?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.