Backup GMail

尝试使用courier-imap和imapsync备份GMail的All Mail文件夹,到了一定阶段,mutt登录总是失败,报内存不足的错误。后来检查imapsync的log,发现也出现无法分配内存的错误。于是上网搜,发现docker并没有对内存进行任何限制。那问题出在哪里呢?

原来是courier-imap在限制内存使用,以下是一段在/etc/courier-imap/imapd里面关于参数IMAP_ULIMITD的注释。

The default value of 65536 sets a very generous limit of 64 megabytes, which should be more than plenty for anyone.

… It is theoretically possible that obscenely huge folders will also result in the server running out of memory when doing server-side sorting (by my calculations you have to have at least 100,000 messages in a single folder, for that to happen)

在出现问题的时候All Mail文件夹里已经有31万多封邮件了,全部备份完毕的时候有41万多。于是将限额增加到1G,同时将imapsync timout时间从2分钟增加到10分钟,终于备份了完整的All Mail。

IO重定向高级用法

如何比较两个程序的输出,但不使用临时文件?

diff <(/usr/bin/gcc --help) <(/usr/local/bin/gcc --help)

避免使用临时文件有两个好处

  • 无需给文件命名。程序员最喜欢的变量名是什么?是i!文件名呢?我喜欢aaaaa
  • 无需清理

下面是个更复杂的例子(这是我第一次用paste)

三个文件,A.txtB.txtC.txt分别保存了三本书的信息,我们希望提取数据到电子表格做分析。三个文件的内容如下:

$ cat A.txt
Chapters: 13
Pages: 400

$ cat B.txt
Chapters: 9
Pages: 700

$ cat C.txt
Chapters: 27
Pages: 1400

使用如下命令,即可得到一个csv输出,导入到电子表格。

$ paste -d, <(ls *.txt | sed -e 's/.txt//') \
  <(grep -h Chapters *.txt | sed -r 's/^.*: ([0-9]+)$/\1/') \
  <(grep -h Pages *.txt | sed -r 's/^.*: ([0-9]+)$/\1/')
A,13,400
B,9,700
C,27,1400