Using (cs-)imapsync to migrate your mailbox

Warning: Migrating your mailbox from one mail account to another in the same mail client (e.g. using Thunderbird or Apple Mail or Outlook) has shown not to be as robust and thrustworthy as you would think - we therefore advise against using your own mail client to migrate your mailbox unless you only have very few folders and very few messages in each folder.

Overview:

imapsync is an of-the-shelf tool that copies/moves mail from one IMAP mailbox to another in a safe and robust way - cs-imapsync is a in-house build wrapper-script building on imapsync to facilitate its use.

We have installed both imapsync and cs-imapsync on the departmental public ssh servers: ssh.cs.kuleuven.be, ssh1.cs.kuleuven.be and ssh2.cs.kuleuven.be.

You can use them on those servers to migrate your existing departmental mailbox to your KU Leuven Exchange mailbox.

If used in the right way, not much can go wrong with it:

  • there are several dry-run tests you can do before actually changing/copying
  • you can interrupt a copy-session and later on retry/resume it
  • you can first copy your mails and only if everything is copied OK, do a final, additional run in which you check and delete if OK

Some mails cannot be copied:

Because the KU Leuven Exchange has its own specific configuration, some e-mails cannot be copied: they might be too big (Exchange only allows 25 MB per e-mail) - they might contain virusses or malware or attachments that for some reason are not allowed - there might be other reasons, yet unknown to us, ...

(cs-)imapsync will tell you if things do not go as expected - more specifically, if you ask to delete mails that are copied correctly, all those non-copy-able mails will remain in the source folder, allowing you to inspect them and decide what to do with them.

imapsync versus cs-imapsync:

Because the base imapsync tool needs a lot of arguments (as shown below), we have created the wrapper script cs-imapsync that does all that for you. In the rest of this text, we assume you will be using that wrapper script so that the instructions are more concise. If you do not trust or cannot use the wrapper script, you are of course free to use the imapsync tool itself, but make sure you understand the command arguments and the additional arguments shown below.


What to do:

  • In a nutshell - more explanation below:
          # some dry-runs to build confidence:
          cs-imapsync --use-defaults --dry --justconnect
          cs-imapsync --use-defaults --dry --justlogin
          cs-imapsync --use-defaults --dry --justfolders
          # a first run, actually copying everything from your mailbox to the default prefix-folder in Exchange
          # this might take hours - don't wait for it - run it in a 'screen' if you know what that is
          # if it does get interrupted, no worries, you can just restart it
          cs-imapsync --use-defaults
          # a second run, this should go much faster and will not show succesful copies but only the problems
          cs-imapsync --use-defaults
          # repeat the above as many times as you need to build confidence that everything is indeed copied
          # finally do a last run and delete everything from your departmental mailbox that is copied correctly
          # (the '1' in the delete arguments means to delete from the source folders,
          #  nothing is deleted from the destination folders)
          cs-imapsync --use-defaults --delete1 --delete1emptyfolders
          # this will leave a completely empty departmental mailbox if all mails are copied succesfully
          # inspect the mails that are left behind because they failed to be copied
          # if all is OK, ask helpdesk to remove the last remains of your departmental mailbox
          # and to check, set and verify the forward of your @cs address to your @kuleuven address
      
  • Prepare and build confidence:

    • each of the above steps is described in more detail later on in this text - if all this seems too much, please contact helpdesk for advice.
    • the example commands above use default values/configuration - if you want more controle, you can decide to use your own configuration variables for the cs-imapsync command
    • do at least some dry-runs to build confidence
  • Copy your mail, but leave it on the departmental mail server for now:

    • do a first copy-run to transfer as much as possible - this might take hours - don't wait for it
    • do a second copy-run to clearly see which mails fail to be copied - this will take far less time - you can wait and see it progress
    • cs-imapsync puts everything it sees and reports in a log file per run - you can have a look there to see what happened - by default, these log files are put in the directory LOG_imapsync in your home directory
    • decide whether any failing mails are a reason to abort the migration or not - if you decide to stop, you might consider to delete the folder you are migrating to in your Exchange mailbox - by default, this prefix folder is called CS-Migrated
  • Check and delete your mail on the departmental mail server:

    • do a last copy-and-delete run to clean up your departmental mailbox as much as possible: all mails that were succesfully copied to your Exchange mailbox are deleted from your departmental mailbox
  • Clean up:

    • have a look in your Exchange mailbox to see if everything does indeed seem to be there
    • have a look at what's left in your departmental mailbox and decide what to do with it
    • let helpdesk know that you no longer need your departmental mailbox and that it can be removed completely now
      • helpdesk will also setup a forward for you if you not already have one, so that all new mail will be delivered in your Exchange mailbox
    • clean up the log files that were created by imapsync: rm -fr ~/LOG_imapsync
    • reorganise your mail folders in your Exchange mailbox (e.g. merge the contents of the migrated INBOX into your real INBOX there)

Optional:

  • If you do not want to migrate all of your mail folders to Exchange but only a single folder tree, you can use the --folderrec FolderName option to do so.

The cs-imapsync wrapper script

imapsync needs to know from where to where to copy the mails. The cs-imapsync wrapper script provides 2 modes of operation for this:

  • either you let cs-imapsync find those details itself
  • or you specify those details explicitly

Moreover we do advise to first migrate all e-mails into a separate folder tree (parameter: $myprefix, default value: CS-Migrated) and only if all has been transferred well, start to re-organise them in the way you like. In this way, you can easily restart the migration process by deleting everything in that separate folder tree and start over with a clean sheet.

You can use the wrapper script as:

  • Either: cs-imapsync --use-defaults ...

  • Or: cs-imapsync "$mymailserver" "$myuaccount" "$myprefix" ...

In the rest of this text we will assume that you let cs-imapsync find the mailserver, uaccount and prefix - in this way, the examples will be more concise. The default prefix that is used is 'CS-Migrated', the mailserver and uaccount are looked up in your account-details.

If you want to provide those details yourself, you can do so, just make sure that you replace --use-defaults in the examples above and below with the 3 required values as shown above. There is more information about these values below.

At the end of the command line, you can give extra command line arguments from imapsync to further instruct what exactly needs to be done - you can find what these are with: imapsync --help

Do take care not to interfere with the arguments that are already given in the wrapper script - always first try with a --dry run ;-)

Dry-run tests

  • just test the connection with:
      cs-imapsync --use-defaults --dry --justconnect
  • just test the connection and login with:
      cs-imapsync --use-defaults --dry --justlogin
  • just test the connection, login and see if folders would have been created with:
      cs-imapsync --use-defaults --dry --justfolders

Copy mails and folders

  • copy mails but do not delete on the source host:
      cs-imapsync --use-defaults

Delete mails if copied OK

  • copy mails and delete them on the source host if copied OK and delete folders that are empty after copying-and-deleting the mails:
      cs-imapsync --use-defaults --delete1 --delete1emptyfolders

Optional background information and/or extras

Check mails that (most probably) can't be copied because of size

If your mailserver is mail3.cs.kuleuven.be, then use /cw/mail3/$USER as $maildir
if your mailserver is mail4.cs.kuleuven.be, then use /cw/mail4/$USER
ask helpdesk for help if you have another mailserver.

  • find "$maildir" -size +25000000c -ls

Base imapsync syntax and arguments

imapsync --host1 "$mymailserver" --ssl1 --user1 "$USER" --host2 'imaps.kuleuven.be' --ssl2 --user2 "$myuaccount" --exchange2 --prefix2 "$myprefix/" --regextrans2 "s{^INBOX\$}{$myprefix/INBOX}" --noexpungeaftereach --maxsize 30000000 ...

The above command specifies:

  • the source and destination details (hosts and usernames, passwords will be asked for when needed)
  • that the destination host is a Microsoft Exchange server
  • the prefix to use on the destination host, for both the INBOX and all other folders
  • that imapsync should not expunge the mailboxes after each mail, but wait until the end to do so
  • a maximum mail size that is larger than that of the KU Leuven Exchange so that no mail is skipped by imapsync because of its size, that would otherwise be accepted

Because this is a whole lot of arguments, we advise to use the cs-imapsync wrapper script as shown above.

Important configuration items

The following configuration items can be specified explicitly if you want to override the defaults that are found automatically - you should initialise them with the appropriate values for yourself:

    mymailserver='mail3.cs.kuleuven.be'
    myuaccount='u0123456'
    myprefix='CS-Migrated'
    

We assume that you know your own u-account number/name - if not, ask helpdesk for help.
mymailserver can be found on your e-mail config overview - look for the Incoming Server.
myprefix is the top level folder on the destination IMAP server where all folders of the source IMAP server will be nested in - you are free to choose a name of course.

If you do not want to use a migration-prefix, you cannot use the cs-imapsync wrapper script but must use the base imapsync command yourself, but adjust the commands shown above accordingly (i.e. do not use the --prefix2 and --regextrans2 arguments).