From 0d1d6cd012af0ee065c2c612b135e5f7a88dafaa Mon Sep 17 00:00:00 2001 From: Janos SUTO Date: Tue, 6 Apr 2021 10:24:42 +0200 Subject: [PATCH] Improved the export-attachments contrib script Signed-off-by: Janos SUTO --- config.php.in | 2 ++ .../{1.php => export-attachments.php} | 15 +++++++++-- webui/model/message/attachment.php | 27 ++++++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) rename contrib/export-attachments/{1.php => export-attachments.php} (75%) diff --git a/config.php.in b/config.php.in index 5c6fd08a..710dda4b 100644 --- a/config.php.in +++ b/config.php.in @@ -472,6 +472,8 @@ define('HEALTH_WORKER_URL', SITE_URL . 'index.php?route=health/worker'); define('LDAP_TYPE_GENERIC', 'generic_ldap'); +define('ATTACHMENT_DUMP_CHECKPOINT', 'attachment_dump_checkpoint'); + define('ACTION_ALL', 0); define('ACTION_UNKNOWN', 1); define('ACTION_LOGIN', 2); diff --git a/contrib/export-attachments/1.php b/contrib/export-attachments/export-attachments.php similarity index 75% rename from contrib/export-attachments/1.php rename to contrib/export-attachments/export-attachments.php index c8902ddd..dc347e0e 100644 --- a/contrib/export-attachments/1.php +++ b/contrib/export-attachments/export-attachments.php @@ -25,7 +25,9 @@ Registry::set('db', $db); Registry::set('auditor_user', 1); $outdir = "/path/to/attachments"; -$limit = 1000; + +openlog("export-attachments", LOG_PID, LOG_MAIL); + $domain = new ModelDomainDomain(); $attachment = new ModelMessageAttachment(); @@ -33,8 +35,13 @@ $message = new ModelSearchMessage(); $domains = $domain->get_mapped_domains(); +$last_id = $attachment->get_last_attachment_id(); +$start_id = $attachment->get_checkpoint(); -for($i=1; $i<$limit; $i++) { +syslog(LOG_INFO, "start: $start, limit: $limit"); + + +for($i=$start_id; $i<$last_id; $i++) { $a = $attachment->get_attachment_by_id($i); $m = $message->get_message_addresses_by_piler_id($a['piler_id'], $domains); @@ -43,4 +50,8 @@ for($i=1; $i<$limit; $i++) { foreach($m['rcpt'] as $rcpt) { $attachment->dump_attachment($outdir, "in", $rcpt, $i, $a); } + + if($i % 100 == 0) { $attachment->update_checkpoint($i); } } + +$attachment->update_checkpoint($i); diff --git a/webui/model/message/attachment.php b/webui/model/message/attachment.php index 22534284..175e951f 100644 --- a/webui/model/message/attachment.php +++ b/webui/model/message/attachment.php @@ -2,7 +2,6 @@ class ModelMessageAttachment extends Model { - public function get_attachment_by_id($id = 0) { if($id <= 0) { return []; } @@ -101,4 +100,30 @@ class ModelMessageAttachment extends Model { } } + + public function get_last_attachment_id() { + $query = $this->db->query("SELECT id FROM " . TABLE_ATTACHMENT . " ORDER BY id DESC LIMIT 1"); + + if(isset($query->row['id'])) { + return $query->row['id']; + } + + return 0; + } + + + public function get_checkpoint() { + $query = $this->db->query("SELECT value FROM `" . TABLE_OPTION . "` WHERE `key`=?", [ATTACHMENT_DUMP_CHECKPOINT]); + if(isset($query->row['value'])) { + return $query->row['value']; + } else { + $this->db->query("INSERT INTO `" . TABLE_OPTION . "` (`key`, value) VALUES(?,0)", [ATTACHMENT_DUMP_CHECKPOINT]); + return 1; + } + } + + + public function update_checkpoint($value=0) { + $this->db->query("UPDATE `" . TABLE_OPTION . "` SET value=? WHERE `key`=?", [$value, ATTACHMENT_DUMP_CHECKPOINT]); + } }