2023-01-30 16:27:06 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-22 15:40:36 +03:00
|
|
|
"context"
|
2023-02-16 15:22:00 +03:00
|
|
|
"log"
|
2023-01-30 16:27:06 +03:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mjl-/bstore"
|
|
|
|
|
2023-02-14 00:37:25 +03:00
|
|
|
"github.com/mjl-/mox/mlog"
|
2023-01-30 16:27:06 +03:00
|
|
|
"github.com/mjl-/mox/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cmdExportMaildir(c *cmd) {
|
2023-02-14 00:37:25 +03:00
|
|
|
c.params = "dst-dir account-path [mailbox]"
|
2023-01-30 16:27:06 +03:00
|
|
|
c.help = `Export one or all mailboxes from an account in maildir format.
|
|
|
|
|
|
|
|
Export bypasses a running mox instance. It opens the account mailbox/message
|
|
|
|
database file directly. This may block if a running mox instance also has the
|
2023-02-13 20:04:05 +03:00
|
|
|
database open, e.g. for IMAP connections. To export from a running instance, use
|
|
|
|
the accounts web page.
|
2023-01-30 16:27:06 +03:00
|
|
|
`
|
|
|
|
args := c.Parse()
|
|
|
|
xcmdExport(false, args, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdExportMbox(c *cmd) {
|
2023-02-14 00:37:25 +03:00
|
|
|
c.params = "dst-dir account-path [mailbox]"
|
2023-01-30 16:27:06 +03:00
|
|
|
c.help = `Export messages from one or all mailboxes in an account in mbox format.
|
|
|
|
|
|
|
|
Using mbox is not recommended. Maildir is a better format.
|
|
|
|
|
|
|
|
Export bypasses a running mox instance. It opens the account mailbox/message
|
|
|
|
database file directly. This may block if a running mox instance also has the
|
2023-02-13 20:04:05 +03:00
|
|
|
database open, e.g. for IMAP connections. To export from a running instance, use
|
|
|
|
the accounts web page.
|
2023-01-30 16:27:06 +03:00
|
|
|
|
2023-02-14 00:37:25 +03:00
|
|
|
For mbox export, "mboxrd" is used where message lines starting with the magic
|
|
|
|
"From " string are escaped by prepending a >. All ">*From " are escaped,
|
2023-01-30 16:27:06 +03:00
|
|
|
otherwise reconstructing the original could lose a ">".
|
|
|
|
`
|
|
|
|
args := c.Parse()
|
|
|
|
xcmdExport(true, args, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func xcmdExport(mbox bool, args []string, c *cmd) {
|
|
|
|
if len(args) != 2 && len(args) != 3 {
|
|
|
|
c.Usage()
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := args[0]
|
|
|
|
accountDir := args[1]
|
|
|
|
var mailbox string
|
|
|
|
if len(args) == 3 {
|
|
|
|
mailbox = args[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
dbpath := filepath.Join(accountDir, "index.db")
|
add a "backup" subcommand to make consistent backups, and a "verifydata" subcommand to verify a backup before restoring, and add tests for future upgrades
the backup command will make consistent snapshots of all the database files. i
had been copying the db files before, and it usually works. but if the file is
modified during the backup, it is inconsistent and is likely to generate errors
when reading (can be at any moment in the future, when reading some db page).
"mox backup" opens the database file and writes out a copy in a transaction.
it also duplicates the message files.
before doing a restore, you could run "mox verifydata" on the to-be-restored
"data" directory. it check the database files, and compares the message files
with the database.
the new "gentestdata" subcommand generates a basic "data" directory, with a
queue and a few accounts. we will use it in the future along with "verifydata"
to test upgrades from old version to the latest version. both when going to the
next version, and when skipping several versions. the script test-upgrades.sh
executes these tests and doesn't do anything at the moment, because no releases
have this subcommand yet.
inspired by a failed upgrade attempt of a pre-release version.
2023-05-26 20:26:51 +03:00
|
|
|
db, err := bstore.Open(context.Background(), dbpath, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, store.DBTypes...)
|
2023-01-30 16:27:06 +03:00
|
|
|
xcheckf(err, "open database %q", dbpath)
|
2023-02-16 15:22:00 +03:00
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
log.Printf("closing db after export: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-01-30 16:27:06 +03:00
|
|
|
|
2023-02-14 00:37:25 +03:00
|
|
|
a := store.DirArchiver{Dir: dst}
|
2023-05-22 15:40:36 +03:00
|
|
|
err = store.ExportMessages(context.Background(), mlog.New("export"), db, accountDir, a, !mbox, mailbox)
|
2023-02-14 00:37:25 +03:00
|
|
|
xcheckf(err, "exporting messages")
|
|
|
|
err = a.Close()
|
|
|
|
xcheckf(err, "closing archiver")
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|