Skip to content

Commit 5e3c26d

Browse files
committed
Add script to handle soft deleted mailboxes, #6113
1 parent 93a3ed0 commit 5e3c26d

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

install/lib/installer_base.lib.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,6 +3703,10 @@ public function install_crontab() {
37033703
$root_cron_jobs[] = "0 0 * * * ".$install_dir."/server/scripts/create_daily_nginx_access_logs.sh &> /dev/null";
37043704
}
37053705

3706+
if ($conf['services']['mail'] == 1) {
3707+
$root_cron_jobs[] = "30 23 * * * ".$install_dir."/server/scripts/handle_mailbox_soft_deleted.sh &> /dev/null";
3708+
}
3709+
37063710
foreach($root_cron_jobs as $cron_job) {
37073711
if(!in_array($cron_job."\n", $existing_root_cron_jobs)) {
37083712
$existing_root_cron_jobs[] = $cron_job."\n";
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
# Archive directories for deleted mailboxes.
4+
delay_days=7
5+
6+
# Test if there is something to do... to avoid 'No such file or directory' from find later.
7+
ls /var/vmail/*/[a-z0-9.-]*-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] >/dev/null 2>&1
8+
if [ $? != 0 ]; then
9+
exit 0;
10+
fi
11+
12+
function remove_soft_deleted_mailbox {
13+
dir=$1
14+
15+
echo "Purging $dir"
16+
echo rm -r "$dir"
17+
}
18+
19+
function compress_soft_deleted_mailbox {
20+
dir=$1
21+
22+
backupfile="${dir}.tar.bz2"
23+
24+
# Test if backup file already exists
25+
if [ -f $backupfile ]; then
26+
# Skip
27+
echo "ERROR: Backupfile($backupfile) exists!" >&2
28+
continue
29+
fi
30+
31+
echo "Compressing for $dir"
32+
echo tar cvfj "$backupfile" --remove-files "$dir" 2> >( grep -v "tar: Removing leading" >&2)
33+
}
34+
35+
# List deleted mailboxs to archive
36+
# -mtime +7 ===> Only mailboxes deleted more then 7 days ago
37+
# Test that the last dir component matches e.g. xxx-20220101094242 (14 digits)
38+
# command: xxx-`date "+%Y%m%d%H%M%S"`
39+
find /var/vmail/*/[a-z0-9.-]*-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] -maxdepth 0 -type d -mtime +$delay_days | while read line; do
40+
# example $line: "/var/vmail/example.com/info-20220101094242"
41+
42+
dir=$line
43+
44+
# Uncomment the desired cleanup method below, or be creative and create your own.
45+
46+
remove_soft_deleted_mailbox $dir
47+
#compress_soft_deleted_mailbox $dir
48+
49+
done

0 commit comments

Comments
 (0)