Backup (or export) all mails by year to selected destination and delete them (if you like)

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
Post Reply
large
Normal user
Normal user
Posts: 33
Joined: 2004-12-31 12:47
Contact:

Backup (or export) all mails by year to selected destination and delete them (if you like)

Post by large » 2020-06-19 23:38

hMailserver has been steady for running for now 8 years and I have users with mails from 2012 that should probably been cleaned :)
With over 400.000 mails on drive and database it is mature to be cleaned.

What I wanted was:
1. Clean everything by given year, eg 2018 and older are copied & wiped
2. Index in a directory hierarchy so it is readable for endusers (using for example http://emlreader.com )
3. Create a logfile so you know what happened during export

Directory structure example for export for mails 2018 and older
2018
|----- Domain1.com
|----------user@domain1.com
|---------------Inbox
|--------------------Any other subdirectory
|----- Domain2.com
|----------user@domain2.com
|---------------Inbox
All exported files have following structure and are placed under correct directory
2011-03-08 11.46.55 - 713260 - The subject of the email.eml
<date&time> - <hmailID> - <Subject>
Here is an report example
2020-06-19 21.53.14 Backupscript started 19.06.2020 21:53:14
2020-06-19 21.53.14 Selected year: 2018 (and older)
2020-06-19 21.53.14 ==========================================
2020-06-19 21.53.14
2020-06-19 21.53.14 --------------------------------------------------
2020-06-19 21.53.14 Account: user@domain1.com
2020-06-19 21.53.14 Total used space: 378,574 MB
2020-06-19 21.53.14
2020-06-19 21.53.14 Folders:
2020-06-19 21.53.16 INBOX: 33/108 (38,78 MB)
2020-06-19 21.53.17 Truncated filename from: D:\MailBackup\2018\domain1.com\user@domain1.com\INBOX\Sent\2011-03-08 11.46.55 - 713260 - This is a veeeeeery long subject above 255 chars so Windows complain.eml
2020-06-19 21.53.17 Truncated filename to: D:\MailBackup\2018\domain1.com\user@domain1.com\INBOX\Sent\2011-03-08 11.46.55 - 713260 - This is a veeeeeery long subject.eml
2020-06-19 21.53.21
2020-06-19 21.53.21 -Sent: 104/149 (26,06 MB)
Each directory are shown with <mailboxname>: <mail exported>/<total mails> (<size exported>)
So "Sent: 104/149 (26,06 MB)" exported 104 of the 149 mails from "Sent" with a total size of 26,06MB. Simple right?

Edit the script for your destination, and your host, admin / password to hMailserver
' #### CONFIG START
Const HMSADMINUSER = "Administrator" ' Admin username
Const HMSADMINPWD = "youpassword" ' Admin password
Const HMSSERVER = "localhost" ' hMailServer Server (host / DCOM)
Const DESTINATION = "D:\MailBackup\" ' Destination, remember to end with trail (\)
' #### CONFIG END
How to run script and parameters to avoid delete
cscript.exe hmailserver-backup.vbs 2018 NODELETE
(Copies everything from 2018 and older without cleaning the hMailserver database)

cscript.exe hmailserver-backup.vbs 2018
(Copies everything from 2018 and older, then deletes from database / harddrive after verification of file consistency)
While running each . represent a mail that are newer than year selected, x is mail that is older.

Tips to remove all empty directories afterwards, run this command
Remember that you have to change directory in cmd to your backup destination. in my case D:\MailBackup\
for /f "delims=" %i in ('dir /s /b /ad ^| sort /r') do rd "%i" 2>NUL
Download file
hmailserver-backup v1.0.zip
(3.49 KiB) Downloaded 112 times
Lars Werner
http://lars.werner.no
Check out my tools:
http://lars.werner.no/unpacker/ - 100% automated extraction tool
http://lars.werner.no/sizeme/ - Maximize the output on a given media (like CD/DVD ect)

large
Normal user
Normal user
Posts: 33
Joined: 2004-12-31 12:47
Contact:

Re: Backup (or export) all mails by year to selected destination and delete them (if you like) v1.1

Post by large » 2020-06-20 23:21

Updated version handles a bug when the Messages objects get smaller while deleting.

Changelog:
Fix: Crash when z = count (right before moving to next directory)
Update: Now informs which folder and how many mails it will scan before it actually scans.
hmailserver-backupv1.1.zip
(3.57 KiB) Downloaded 170 times
Lars Werner
http://lars.werner.no
Check out my tools:
http://lars.werner.no/unpacker/ - 100% automated extraction tool
http://lars.werner.no/sizeme/ - Maximize the output on a given media (like CD/DVD ect)

Post Reply