Backup GMail (or other IMAP) accounts to disk
With versions above 2.x, this gems stores IMAP metadata in a backwardly-incompatible way. When upgrading, all old backups will be gradually deleted to allow for the new file format to be introduced.
$ gem install 'imap-backup'
In order to do backups, you need to add accounts via a menu-driven command line program:
Run:
$ imap-backup setup
By default, all folders are backed-up. You can override this by choosing specific folders.
setup
creates the file ~/.imap-backup/config.json
E.g.:
{
"accounts": [
{
"username": "[email protected]",
"password": "secret",
"local_path": "/path/to/backup/root",
"folders":
[
{"name": "[Gmail]/All Mail"},
{"name": "my_folder"}
]
}
]
}
It connects to GMail by default, but you can also specify a server:
{
"accounts": [
{
"username": "[email protected]",
"password": "secret",
"server": "my.imap.example.com",
"local_path": "/path/to/backup/root",
"folders":
[
{"name": "[Gmail]/All Mail"},
{"name": "my_folder"}
]
}
]
}
You can override the parameters passed to Net::IMAP
with connection_options
.
Specifically, if you are using a self-signed certificate and get SSL errors, e.g.
certificate verify failed
, you can choose to not verify the TLS connection:
{
"accounts": [
{
"username": "[email protected]",
"password": "secret",
"server": "my.imap.example.com",
"local_path": "/path/to/backup/root",
"folders": [
{"name": "[Gmail]/All Mail"},
{"name": "my_folder"}
],
"connection_options": {
"ssl": {"verify_mode": 0},
"port": 993
}
}
]
}
- Enable IMAP access to your account via the GMail interface (Settings/Forwarding and POP/IMAP),
- Under 'Sign-in & security', 'Signing in to Google', 'App passwords', generate a password for imap-backup,
- In imap-backup setup, set the server to imap.gmail.com
Note that email usernames and passwords are held in plain text in the configuration file.
The directory ~/.imap-backup, the configuration file and all backup directories have their access permissions set to only allow access by your user.
Manually, from the command line:
$ imap-backup
Alternatively, add it to your crontab.
Each folder is saved to an mbox file. Alongside each mbox is a file with extension '.imap', which lists the source IMAP UIDs to allow a full restore.
If you have problems:
- ensure that you have the latest release,
- turn on debugging output:
{
"accounts":
[
...
],
"debug": true
}
All missing messages are pushed to the IMAP server. Existing messages are left unchanged.
This functionality requires that the IMAP server supports the UIDPLUS extension to IMAP4.
List IMAP folders:
$ imap-backup folders
Get statistics of emails to download per folder:
$ imap-backup status
- Secure - use a local file protected by permissions
- Restartable - calculate start point based on already downloaded messages
- Standalone - do not rely on an email client or MTA
- https://github.com/thefloweringash/gmail-imap-backup
- https://github.com/mleonhard/imapbackup
- https://github.com/rgrove/larch - copies between IMAP servers
- https://github.com/OfflineIMAP/offlineimap
Integration tests (feature specs) are run against a Docker image (antespi/docker-imap-devel:latest).
In one shell, run the Docker image:
docker run \
--env [email protected] \
--env MAIL_PASS=pass \
--env MAILNAME=example.org \
--publish 8993:993 \
antespi/docker-imap-devel:latest
Currently, the integration tests with Docker are excluded from the CI run.
To run just the Docker-based tests:
rspec -t docker
To run all specs, including the integration tests, do the following:
rspec -O .rspec-all
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request