Compare commits
6 Commits
0a3a705e33
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b094843bf1 | |||
| a991a67b16 | |||
| 1ff6a7f34e | |||
| dcf2ec39af | |||
| 696898d483 | |||
| cda4106c0a |
@@ -1,3 +1,15 @@
|
|||||||
# mailcow-scripts
|
# mailcow-scripts
|
||||||
|
|
||||||
Scripts to help manage mailcow-dockerized installations
|
A few scripts to help manage your mailcow-dockerized installation. I'll be adding to this repo as I find more common administrative tasks that can be easily scripted and automated.
|
||||||
|
|
||||||
|
## scripts
|
||||||
|
|
||||||
|
### mail_cleanup.sh
|
||||||
|
|
||||||
|
Delete messages in the *Junk* and *Trash* folders that are older than a certain amount of days. Usage details in the [wiki here](https://git.asifbacchus.app/asif/mailcow-scripts/wiki/1.-mail_cleanup.sh)
|
||||||
|
|
||||||
|
### mail_prune.sh
|
||||||
|
|
||||||
|
Delete messages from all mailboxes that are older than a certain number of days, provided those messages are *READ* and *UNflagged*. Usage details in the [wiki here](https://git.asifbacchus.app/asif/mailcow-scripts/wiki/2.-mail_prune.sh).
|
||||||
|
|
||||||
|
That's all for now!
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#######
|
||||||
|
### cleanup old messages in Junk and Trash mail folders
|
||||||
|
###
|
||||||
|
### This script is meant to be executed as a CRON job so there is no friendly
|
||||||
|
### user output or error trapping. Check logs for errors.
|
||||||
|
#######
|
||||||
|
|
||||||
|
|
||||||
|
### functions
|
||||||
|
|
||||||
|
# bad parameters
|
||||||
|
badParam () {
|
||||||
|
if [ "$1" = "empty" ]; then
|
||||||
|
printf "\n'%s' cannot have a NULL (empty) value.\n" "$2"
|
||||||
|
printf "Please use '--help' for assistance.\n\n"
|
||||||
|
elif [ "$1" = "nospecify" ]; then
|
||||||
|
printf "\n'%s' was not specified.\n" "$2"
|
||||||
|
printf "Please use '--help' for assistance.\n\n"
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# script help
|
||||||
|
scriptHelp () {
|
||||||
|
printf "\n*** Delete old messages in Junk and Trash folders ***\n\n"
|
||||||
|
printf "Usage: ./mail_cleanup.sh --junk-read x --junk-all y --trash z [-p|--path|--mailcow-path path]\n"
|
||||||
|
printf "\n\tWhere:\n"
|
||||||
|
printf "\t\tx: delete READ messages in the JUNK folder older than this many days\n"
|
||||||
|
printf "\t\ty: delete ALL messages in the JUNK folder older than this many days\n"
|
||||||
|
printf "\t\tz: delete ALL messages in the TRASH folder older than this many days\n"
|
||||||
|
printf "\t\tpath: path to your mailcow-dockerized installation\n"
|
||||||
|
printf "\t\t(default: /opt/mailcow-dockerized)\n\n"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### startup parameters
|
||||||
|
|
||||||
|
# set defaults
|
||||||
|
mailcowPath="/opt/mailcow-dockerized"
|
||||||
|
|
||||||
|
# no parameters given, show help
|
||||||
|
if [ $# -le 0 ]; then
|
||||||
|
scriptHelp
|
||||||
|
fi
|
||||||
|
|
||||||
|
# process startup parameters
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
-h|-\?|--help)
|
||||||
|
# display help
|
||||||
|
scriptHelp
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
--junk-read)
|
||||||
|
# expunge READ messages in Junk folder
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
deleteJunkRead="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--junk-all)
|
||||||
|
# expunge messages in Junk folder older than...
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
deleteJunkAll="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--trash)
|
||||||
|
# expunge messages in Trash folder older than...
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
deleteTrash="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-p|--path|--mailcow-path)
|
||||||
|
# path to mailcow directory
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
mailcowPath="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf "Unknown option: %s\n" "$1"
|
||||||
|
printf "Use '--help' for valid options\n\n"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
### check for missing parameters
|
||||||
|
if [ -z "$deleteJunkRead" ]; then
|
||||||
|
badParam nospecify '--junk-read'
|
||||||
|
elif [ -z "$deleteJunkAll" ]; then
|
||||||
|
badParam nospecify '--junk-all'
|
||||||
|
elif [ -z "$deleteTrash" ]; then
|
||||||
|
badParam nospecify '--trash'
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
### display parameters being used
|
||||||
|
printf "\nUsing parameters:\n"
|
||||||
|
printf "delete READ junk messages after %s days\n" "$deleteJunkRead"
|
||||||
|
printf "delete ALL junk messages after %s days\n" "$deleteJunkAll"
|
||||||
|
printf "delete ALL messages in trash older than %s days\n" "$deleteTrash"
|
||||||
|
printf "mailcow path: %s\n\n" "$mailcowPath"
|
||||||
|
|
||||||
|
|
||||||
|
### execute doveadm using parameters provided
|
||||||
|
if ! cd "$mailcowPath" > /dev/null 2>&1; then
|
||||||
|
printf "Can't change to specified mailcow-dockerized directory!\n"
|
||||||
|
printf "\t(%s)\n\n" "$mailcowPath"
|
||||||
|
printf "Exiting. No actions performed.\n\n"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# clean up read junk mail
|
||||||
|
printf "Cleaning up READ junk mail...\n"
|
||||||
|
/usr/local/bin/docker-compose exec -T dovecot-mailcow doveadm expunge -A mailbox 'Junk' SEEN NOT SINCE ${deleteJunkRead}d
|
||||||
|
# clean up all junk mail
|
||||||
|
printf "Cleaning up OLD junk mail...\n"
|
||||||
|
/usr/local/bin/docker-compose exec -T dovecot-mailcow doveadm expunge -A mailbox 'Junk' SAVEDBEFORE ${deleteJunkAll}d
|
||||||
|
# clean up trash
|
||||||
|
printf "Cleaning up TRASH...\n"
|
||||||
|
/usr/local/bin/docker-compose exec -T dovecot-mailcow doveadm expunge -A mailbox 'Trash' SAVEDBEFORE ${deleteTrash}d
|
||||||
|
|
||||||
|
|
||||||
|
### exit gracefully
|
||||||
|
printf "...done\n\n"
|
||||||
|
|
||||||
|
exit 0
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#######
|
||||||
|
### cleanup flagged messages for a certain account
|
||||||
|
###
|
||||||
|
### This script is meant to be executed as a CRON job so there is no friendly
|
||||||
|
### user output or error trapping. Check logs for errors.
|
||||||
|
#######
|
||||||
|
|
||||||
|
|
||||||
|
### functions
|
||||||
|
|
||||||
|
# bad parameters
|
||||||
|
badParam () {
|
||||||
|
if [ "$1" = "empty" ]; then
|
||||||
|
printf "\n'%s' cannot have a NULL (empty) value.\n" "$2"
|
||||||
|
printf "Please use '--help' for assistance.\n\n"
|
||||||
|
elif [ "$1" = "nospecify" ]; then
|
||||||
|
printf "\n'%s' was not specified.\n" "$2"
|
||||||
|
printf "Please use '--help' for assistance.\n\n"
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# script help
|
||||||
|
scriptHelp () {
|
||||||
|
printf "\n*** Delete FLAGGED messages in all folders for a certain user ***\n\n"
|
||||||
|
printf "Usage: ./mail_cleanup_flagged.sh --user x --older-than y [-p|--path|--mailcow-path path]\n"
|
||||||
|
printf "\n\tWhere:\n"
|
||||||
|
printf "\t\tx: target USER named 'x'\n"
|
||||||
|
printf "\t\ty: delete FLAGGED messages older than this many days\n"
|
||||||
|
printf "\t\tpath: path to your mailcow-dockerized installation\n"
|
||||||
|
printf "\t\t(default: /opt/mailcow-dockerized)\n\n"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### startup parameters
|
||||||
|
|
||||||
|
# set defaults
|
||||||
|
mailcowPath="/opt/mailcow-dockerized"
|
||||||
|
|
||||||
|
# no parameters given, show help
|
||||||
|
if [ $# -le 0 ]; then
|
||||||
|
scriptHelp
|
||||||
|
fi
|
||||||
|
|
||||||
|
# process startup parameters
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
-h|-\?|--help)
|
||||||
|
# display help
|
||||||
|
scriptHelp
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-u|--user)
|
||||||
|
# target this user
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
user="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-a|--age|--older-than)
|
||||||
|
# expunge messages older than...
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
deleteOlder="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-p|--path|--mailcow-path)
|
||||||
|
# path to mailcow directory
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
mailcowPath="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf "Unknown option: %s\n" "$1"
|
||||||
|
printf "Use '--help' for valid options\n\n"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
### check for missing parameters
|
||||||
|
if [ -z "$user" ]; then
|
||||||
|
badParam nospecify '--user'
|
||||||
|
elif [ -z "$deleteOlder" ]; then
|
||||||
|
badParam nospecify '--older-than'
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
### display parameters being used
|
||||||
|
printf "\nUsing parameters:\n"
|
||||||
|
printf "targeting %s\n" "$user"
|
||||||
|
printf "deleting FLAGGED messages older than %s days\n" "$deleteOlder"
|
||||||
|
printf "mailcow path: %s\n\n" "$mailcowPath"
|
||||||
|
|
||||||
|
|
||||||
|
### execute doveadm using parameters provided
|
||||||
|
if ! cd "$mailcowPath" > /dev/null 2>&1; then
|
||||||
|
printf "Can't change to specified mailcow-dockerized directory!\n"
|
||||||
|
printf "\t(%s)\n\n" "$mailcowPath"
|
||||||
|
printf "Exiting. No actions performed.\n\n"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# clean up flagged messages
|
||||||
|
printf "Cleaning up FLAGGED messages older than %s days...\n" "$deleteOlder"
|
||||||
|
/usr/local/bin/docker-compose exec -T dovecot-mailcow doveadm expunge -u "$user" mailbox '*' FLAGGED SAVEDBEFORE "${deleteOlder}d"
|
||||||
|
|
||||||
|
|
||||||
|
### exit gracefully
|
||||||
|
printf "...done\n\n"
|
||||||
|
|
||||||
|
exit 0
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#######
|
||||||
|
### cleanup old messages in ALL mail folders older than x days that are SEEN
|
||||||
|
### and UNflagged
|
||||||
|
###
|
||||||
|
### This script is meant to be executed as a CRON job so there is no friendly
|
||||||
|
### user output or error trapping. Check logs for errors.
|
||||||
|
#######
|
||||||
|
|
||||||
|
|
||||||
|
### functions
|
||||||
|
|
||||||
|
# bad parameters
|
||||||
|
badParam () {
|
||||||
|
if [ "$1" = "empty" ]; then
|
||||||
|
printf "\n'%s' cannot have a NULL (empty) value.\n" "$2"
|
||||||
|
printf "Please use '--help' for assistance.\n\n"
|
||||||
|
elif [ "$1" = "nospecify" ]; then
|
||||||
|
printf "\n'%s' was not specified.\n" "$2"
|
||||||
|
printf "Please use '--help' for assistance.\n\n"
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# script help
|
||||||
|
scriptHelp () {
|
||||||
|
printf "\n*** Delete ALL messages older than x days ***\n"
|
||||||
|
printf "*** only READ and UNFLAGGED messages will be removed ***\n\n"
|
||||||
|
printf "Usage: ./mail_prune.sh -d|--days x [-p|--path|--mailcow-path path]\n"
|
||||||
|
printf "\n\tWhere:\n"
|
||||||
|
printf "\t\tx: delete messages (ALL users, ALL folders) older than this many days\n"
|
||||||
|
printf "\t\tpath: path to your mailcow-dockerized installation\n"
|
||||||
|
printf "\t\t(default: /opt/mailcow-dockerized)\n\n"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### startup parameters
|
||||||
|
|
||||||
|
# set defaults
|
||||||
|
mailcowPath="/opt/mailcow-dockerized"
|
||||||
|
|
||||||
|
# no parameters given, show help
|
||||||
|
if [ $# -le 0 ]; then
|
||||||
|
scriptHelp
|
||||||
|
fi
|
||||||
|
|
||||||
|
# process startup parameters
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
-h|-\?|--help)
|
||||||
|
# display help
|
||||||
|
scriptHelp
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-d|--days)
|
||||||
|
# delete messages older than this many days
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
deleteOlder="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-p|--path|--mailcow-path)
|
||||||
|
# path to mailcow directory
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
mailcowPath="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf "Unknown option: %s\n" "$1"
|
||||||
|
printf "Use '--help' for valid options\n\n"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
### check for missing parameters
|
||||||
|
if [ -z "$deleteOlder" ]; then
|
||||||
|
badParam nospecify '--days'
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
### display parameters being used
|
||||||
|
printf "\nUsing parameters:\n"
|
||||||
|
printf "delete ALL messages older than %s days\n" "$deleteOlder"
|
||||||
|
printf "mailcow path: %s\n\n" "$mailcowPath"
|
||||||
|
|
||||||
|
|
||||||
|
### execute doveadm using parameters provided
|
||||||
|
if ! cd "$mailcowPath" > /dev/null 2>&1; then
|
||||||
|
printf "Can't change to specified mailcow-dockerized directory!\n"
|
||||||
|
printf "\t(%s)\n\n" "$mailcowPath"
|
||||||
|
printf "Exiting. No actions performed.\n\n"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# delete old messages
|
||||||
|
printf "Deleting OLD messages...\n"
|
||||||
|
/usr/local/bin/docker-compose exec -T dovecot-mailcow doveadm expunge -A mailbox '*' SEEN UNFLAGGED BEFORE ${deleteOlder}d
|
||||||
|
|
||||||
|
|
||||||
|
### exit gracefully
|
||||||
|
printf "...done\n\n"
|
||||||
|
|
||||||
|
exit 0
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#######
|
||||||
|
### cleanup a certain user's messages in ALL mail folders older than x days
|
||||||
|
### that are SEEN and UNflagged
|
||||||
|
###
|
||||||
|
### This script is meant to be executed as a CRON job so there is no friendly
|
||||||
|
### user output or error trapping. Check logs for errors.
|
||||||
|
#######
|
||||||
|
|
||||||
|
|
||||||
|
### functions
|
||||||
|
|
||||||
|
# bad parameters
|
||||||
|
badParam () {
|
||||||
|
if [ "$1" = "empty" ]; then
|
||||||
|
printf "\n'%s' cannot have a NULL (empty) value.\n" "$2"
|
||||||
|
printf "Please use '--help' for assistance.\n\n"
|
||||||
|
elif [ "$1" = "nospecify" ]; then
|
||||||
|
printf "\n'%s' was not specified.\n" "$2"
|
||||||
|
printf "Please use '--help' for assistance.\n\n"
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# script help
|
||||||
|
scriptHelp () {
|
||||||
|
printf "\n*** Delete ALL messages older than x days for a certain user ***\n"
|
||||||
|
printf "*** only READ and UNFLAGGED messages will be removed ***\n\n"
|
||||||
|
printf "Usage: ./mail_prune_user.sh -d|--days x -u|--user y [-p|--path|--mailcow-path path]\n"
|
||||||
|
printf "\n\tWhere:\n"
|
||||||
|
printf "\t\tx: delete messages (ALL folders) older than this many days\n"
|
||||||
|
printf "\t\ty: target USER y\n"
|
||||||
|
printf "\t\tpath: path to your mailcow-dockerized installation\n"
|
||||||
|
printf "\t\t(default: /opt/mailcow-dockerized)\n\n"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### startup parameters
|
||||||
|
|
||||||
|
# set defaults
|
||||||
|
mailcowPath="/opt/mailcow-dockerized"
|
||||||
|
|
||||||
|
# no parameters given, show help
|
||||||
|
if [ $# -le 0 ]; then
|
||||||
|
scriptHelp
|
||||||
|
fi
|
||||||
|
|
||||||
|
# process startup parameters
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
-h|-\?|--help)
|
||||||
|
# display help
|
||||||
|
scriptHelp
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-d|--days)
|
||||||
|
# delete messages older than this many days
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
deleteOlder="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-u|--user)
|
||||||
|
# target this specific user
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
user="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-p|--path|--mailcow-path)
|
||||||
|
# path to mailcow directory
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
mailcowPath="$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
badParam empty "$@"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf "Unknown option: %s\n" "$1"
|
||||||
|
printf "Use '--help' for valid options\n\n"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
### check for missing parameters
|
||||||
|
if [ -z "$deleteOlder" ]; then
|
||||||
|
badParam nospecify '--days'
|
||||||
|
elif [ -z "$user" ]; then
|
||||||
|
badParam nospecify '--user'
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
### display parameters being used
|
||||||
|
printf "\nUsing parameters:\n"
|
||||||
|
printf "targetting %s\n" "$user"
|
||||||
|
printf "delete ALL messages older than %s days\n" "$deleteOlder"
|
||||||
|
printf "mailcow path: %s\n\n" "$mailcowPath"
|
||||||
|
|
||||||
|
|
||||||
|
### execute doveadm using parameters provided
|
||||||
|
if ! cd "$mailcowPath" > /dev/null 2>&1; then
|
||||||
|
printf "Can't change to specified mailcow-dockerized directory!\n"
|
||||||
|
printf "\t(%s)\n\n" "$mailcowPath"
|
||||||
|
printf "Exiting. No actions performed.\n\n"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# delete messages for user
|
||||||
|
printf "Deleting OLD messages for %s...\n" "$user"
|
||||||
|
/usr/local/bin/docker-compose exec -T dovecot-mailcow doveadm expunge -u "$user" mailbox '*' SEEN UNFLAGGED BEFORE "${deleteOlder}d"
|
||||||
|
|
||||||
|
|
||||||
|
### exit gracefully
|
||||||
|
printf "...done\n\n"
|
||||||
|
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user