#!/bin/cat
$Id: FAQ.Archiving.txt,v 1.30 2024/08/11 11:13:32 gilles Exp gilles $

This documentation is also available online at
https://imapsync.lamiral.info/FAQ.d/
https://imapsync.lamiral.info/FAQ.d/FAQ.Archiving.txt

=======================================================================
          Imapsync issues and tips about archiving
=======================================================================

Questions answered in this FAQ are:

Q. How can I archive different accounts on the same destination account,
   each account on a separate folder?

Q. How can I restore a complete account backuped in a subfolder, like
   a backup done with the option --subfolder2?

Q. How can I merge two or more mailboxes into one?

Q. How to move emails from one IMAP folder to another, either on the 
   same IMAP server or on a different one? Example:
   How to move all messages older than 1 year, from INBOX to Archive?

Q. Can imapsync be used to maintain and restore a local offline copy 
   of a mailbox, eg for backup purposes, using Mbox or Maildir 
   format, so that if the server fails, then the mailbox could be 
   reinstated?

Q. How to archive a whole year in a subfolder?

Q. How to archive a whole year in subfolders but without subsubfolders?


Now the questions again with their answers.

https://imapsync.lamiral.info/FAQ.d/FAQ.Archiving.txt
=======================================================================
Q. How can I archive different accounts on the same destination account,
   each account on a separate folder?

A. Use --subfolder2 

 --subfolder2   str  : Syncs the whole host1 folders hierarchy under the
                       host2 given folder str.
                       (It does it internally by adding two --regextrans2 
                       options before all others.)

Examples:

To backup the account Foo in a subfolder called FooArchiv:

  imapsync ... --user1 Foo --subfolder2 FooArchiv

To backup the account Bar in a subfolder called BarArchiv:

  imapsync ... --user1 Bar --subfolder2 BarArchiv

In case you need a very strict sync, add options 
--delete2 --delete2foldersonly "/FooArchiv/",
(or "/BarArchiv/" for the second example).
It will delete on account2 what is not on account1 but only in
the right place, ie, the subfolder "FooArchiv".

Slashes around the name value of --delete2foldersonly are mandatory.
Technically, the value of option --delete2foldersonly is a regex,
a regular expression, with its Perl regex outbound delimiters, any pair 
of delimiters. It's why the slashes, or any pair, are mandatory.

  --delete2foldersonly "/FooArchiv/" 
  is equivalent to
  --delete2foldersonly "m{FooArchiv}"


Examples:

To mirror the account Foo in a subfolder called FooArchiv:

  imapsync ... --user1 Foo --subfolder2 FooArchiv --delete2 --delete2foldersonly "/FooArchiv/" 

To mirror the account Bar in a subfolder called BarArchiv:

  imapsync ... --user1 Bar --subfolder2 BarArchiv --delete2 --delete2foldersonly "/BarArchiv/" 

See how to restore those with the next Q/R.

=======================================================================
Q. How can I restore a complete account backuped in a subfolder, like
   a backup done with the option --subfolder2?

A. Use --subfolder1

 --subfolder1   str  : Syncs the host1 folders hierarchy under folder str
                        to the root hierarchy of host2.
                        It's the couterpart of a sync done by --subfolder2
                        when doing it in the reverse order. 
                        Backup/Restore scenario:
                        Use --subfolder2 str for a backup to the folder str
                        on host2. Then use --subfolder1 str for restoring 
                        from the folder str, after inverting 
                        host1/host2 user1/user2 values.

Examples:

To restore the whole folder FooArchiv hierarchy into the Foo account:

  imapsync ... --user2 Foo --subfolder1 FooArchiv

To restore the whole folder BarArchiv hierarchy into the Bar account:

  imapsync ... --user2 Bar --subfolder1 BarArchiv

Read the previous FAQ item "How can I archive different accounts on the same 
destination account, each account on a separate folder?"

=======================================================================
Q. How can I merge two or more mailboxes into one?

A. Run imapsync twice, one for each mailbox, with the same destination
   mailbox.

=======================================================================
Q. How to move emails from one IMAP folder to another, either on the 
   same IMAP server or on a different one? Example:
   How to move all messages older than 1 year, from INBOX to Archive?
 
R1. Solution:

  imapsync ... --minage 365 --folder INBOX --f1f2 INBOX=Archive --delete1 --noexpungeaftereach

Use the option --noexpungeaftereach if speed is a concern.
Use the option --delete1 if you want to move messages, instead of just copy/sync them.

R2. See also file FAQ.Folders_Mapping.txt


https://imapsync.lamiral.info/FAQ.d/FAQ.Archiving.txt
=======================================================================
Q. Can imapsync be used to maintain and restore a local offline copy 
   of a mailbox, eg for backup purposes, using Mbox or Maildir 
   format, so that if the server fails, then the mailbox could be 
   reinstated?

R1. No. Imapsync plays with IMAP servers only.

R2. Imapsync can't but rick-sanders-imap-tools can!
    See https://github.com/andrewnimmo/rick-sanders-imap-tools
    
    Back up and restore IMAP accounts with imapdump.pl and dumptoIMAP.pl
    It looks like imapdump.pl has options to avoid duplicates and be
    in an update mode. There are options -u -U
    [-u] Don't dump messages already dumped
    [-U] Don't dump message if it already exists in the dump directory
    I wonder the difference, I've never used Rick's tools.

    Remember that doing backups without trying the restore process
    is more dangerous than doing no backups at all. No backups makes
    people act in a safer way, well, usually...
    
R3. Use isync or mbsync
    https://isync.sourceforge.io/

R4. Use ImportExportTools for Mozilla Thunderbird by Paolo Kaosmos. 
    ImportExportTools does not do IMAP as a destination.

R5. There is a free online service (French language) allowing you to do
    a Maildir or EML or HTML backup at 
    https://www.powermail.fr/sauvegarder-une-boite-mail/
    Prefer Maildir or EML format if you want to be able to restore the 
    archive. HTML format will be ok to only view the messages, not to
    restore them to an imap server account.

I have never used any of this tools so asking me for support with them 
won't go far away.

=======================================================================
Q. How to backup a whole year in a subfolder?

A. To backup the whole year 2018 in a subfolder called ARCHIVE_2018:

  imapsync ... --search "SENTSINCE 1-Jan-2018 SENTBEFORE 31-Dec-2018" \
      --subfolder2 ARCHIVE_2018 

=======================================================================
Q. How to archive a whole year in subfolders but without subsubfolders?

A. Instead of the solution with --subfolder2, use:

  imapsync ... --search "SENTSINCE 1-Jan-2018 SENTBEFORE 31-Dec-2018" \
             --sep2 _ --regextrans2 's{(.*)}{Archive_$1_2018}' 

For each folder "FFFF" in the source account, it will create a destination
folder called "Archive_FFFF_2018", at the top level hierarchy.

=======================================================================
=======================================================================
