├── .editorconfig ├── .github └── workflows │ ├── build_and_publish.yml │ └── pr.yml ├── .gitignore ├── CHANGELOG.md ├── Dockerfile ├── LICENSE.txt ├── README.md ├── RELEASING.md └── root ├── app └── sync.sh └── etc ├── cont-init.d ├── 10-adduser.sh └── 20-cron.sh └── services.d └── cron └── run /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.yaml] 12 | indent_style = space 13 | -------------------------------------------------------------------------------- /.github/workflows/build_and_publish.yml: -------------------------------------------------------------------------------- 1 | name: build and publish 2 | 3 | on: 4 | push: 5 | branches: 6 | - trunk 7 | tags: 8 | - '**' 9 | workflow_dispatch: 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v2 18 | 19 | - uses: crazy-max/ghaction-docker-meta@v1 20 | id: docker_meta 21 | with: 22 | images: | 23 | jakewharton/mbsync 24 | ghcr.io/jakewharton/mbsync 25 | tag-semver: | 26 | {{version}} 27 | {{major}} 28 | {{major}}.{{minor}} 29 | 30 | - uses: docker/login-action@v1 31 | with: 32 | username: jakewharton 33 | password: ${{ secrets.DOCKER_HUB_TOKEN }} 34 | 35 | - run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin 36 | 37 | - uses: docker/build-push-action@v2 38 | with: 39 | push: true 40 | tags: ${{ steps.docker_meta.outputs.tags }} 41 | labels: ${{ steps.docker_meta.outputs.labels }} 42 | 43 | - name: Extract release notes 44 | id: release_notes 45 | if: startsWith(github.ref, 'refs/tags/') 46 | uses: ffurrer2/extract-release-notes@v1 47 | 48 | - name: Create Release 49 | if: startsWith(github.ref, 'refs/tags/') 50 | uses: softprops/action-gh-release@v1 51 | with: 52 | body: ${{ steps.release_notes.outputs.release_notes }} 53 | env: 54 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 55 | -------------------------------------------------------------------------------- /.github/workflows/pr.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout 11 | uses: actions/checkout@v2 12 | 13 | - name: Build 14 | run: docker build . 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ IDEA 2 | .idea 3 | *.iml 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [Unreleased] 4 | 5 | 6 | ## [1.4.1] - 2023-02-21 7 | 8 | * Fix: Only create user and group if they do not already exist. 9 | 10 | ## [1.4.0] - 2023-02-20 11 | 12 | * Upgrade to isync 1.4.4 13 | 14 | ## [1.3.0] - 2021-11-23 15 | 16 | * Update base image to Alpine 3.14 17 | * Upgrade to isync 1.4.3 18 | 19 | ## [1.2.0] - 2020-12-24 20 | 21 | * Update base image to Alpine 3.12 22 | * Upgrade to isync 1.3.3 23 | 24 | ## [1.1.0] - 2020-10-26 25 | 26 | * New: `HEALTHCHECK_HOST` environment variable allows you to specify a health check host other 27 | than the default, publicly-available healthchecks.io. 28 | 29 | ## [1.0.0] - 2020-05-25 30 | 31 | * New: Switch to S6 overlay for running cron in the container. This should generally make it more well-behaved. 32 | * New: `PUID` and `PGID` environment variables control what user and group the program runs (and thus writes files). 33 | 34 | ## [0.2.0] - 2020-04-06 35 | 36 | * New: `HEALTHCHECK_ID` replaces `CHECK_URL`. This allows the container to ping the check both 37 | before and after the sync which will report its execution time. 38 | 39 | ## [0.1.0] - 2020-04-06 40 | 41 | Initial release 42 | 43 | 44 | [Unreleased]: https://github.com/JakeWharton/docker-mbsync/compare/1.4.1...HEAD 45 | [1.4.1]: https://github.com/JakeWharton/docker-mbsync/releases/tag/1.4.1 46 | [1.4.0]: https://github.com/JakeWharton/docker-mbsync/releases/tag/1.4.0 47 | [1.3.0]: https://github.com/JakeWharton/docker-mbsync/releases/tag/1.3.0 48 | [1.2.0]: https://github.com/JakeWharton/docker-mbsync/releases/tag/1.2.0 49 | [1.1.0]: https://github.com/JakeWharton/docker-mbsync/releases/tag/1.1.0 50 | [1.0.0]: https://github.com/JakeWharton/docker-mbsync/releases/tag/1.0.0 51 | [0.2.0]: https://github.com/JakeWharton/docker-mbsync/releases/tag/0.2.0 52 | [0.1.0]: https://github.com/JakeWharton/docker-mbsync/releases/tag/0.1.0 53 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:alpine AS build 2 | RUN apk add --no-cache shellcheck 3 | 4 | RUN mkdir /overlay 5 | COPY root/ /overlay/ 6 | RUN find /overlay -type f | xargs shellcheck -e SC1008 7 | 8 | 9 | FROM project42/s6-alpine:3.14 10 | LABEL maintainer="Jake Wharton " 11 | 12 | ENV \ 13 | # Fail if cont-init scripts exit with non-zero code. 14 | S6_BEHAVIOUR_IF_STAGE2_FAILS=2 \ 15 | CRON="" \ 16 | HEALTHCHECK_ID="" \ 17 | HEALTHCHECK_HOST="https://hc-ping.com" \ 18 | PUID="" \ 19 | PGID="" 20 | 21 | RUN echo @edge http://nl.alpinelinux.org/alpine/edge/community > /etc/apk/repositories \ 22 | && echo @edge http://nl.alpinelinux.org/alpine/edge/main >> /etc/apk/repositories \ 23 | && apk add --no-cache \ 24 | isync@edge \ 25 | curl@edge \ 26 | && rm -rf /var/cache/* \ 27 | && mkdir /var/cache/apk 28 | 29 | COPY root/ / 30 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2020 Jake Wharton 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Docker Mailbox Sync 2 | =================== 3 | 4 | A Docker container which runs the [`mbsync`][1] tool automatically to synchronize your email. 5 | 6 | [1]: http://isync.sourceforge.net/mbsync.html 7 | 8 | [![Docker Image Version](https://img.shields.io/docker/v/jakewharton/mbsync?sort=semver)][hub] 9 | [![Docker Image Size](https://img.shields.io/docker/image-size/jakewharton/mbsync)][layers] 10 | 11 | [hub]: https://hub.docker.com/r/jakewharton/mbsync/ 12 | [layers]: https://microbadger.com/images/jakewharton/mbsync 13 | 14 | 15 | Setup 16 | ----- 17 | 18 | Select and create two directories: 19 | 20 | * The "mail" directory where email will be stored. (From now on referred to as `/path/to/mail`) 21 | * The "config" directory where your configuration file will be stored. (From now on referred to as `/path/to/config`) 22 | 23 | 24 | ### Config 25 | 26 | In `/path/to/config`, create a file `mbsync.rc` for configuration. 27 | See the [`mbsync`][1] documentation for the syntax of this file. 28 | 29 | The examples in this README mount the destination `/path/to/mail` directory as `/mail` inside of the container. 30 | If you are going to follow that, you should use `/mail` as the path in your configuration. 31 | 32 | Here is an example, minimal configuration for synchronizing everything in a mailbox: 33 | ``` 34 | IMAPAccount example 35 | Host imap.example.com 36 | User me@example.com 37 | Pass abc123 38 | AuthMechs LOGIN 39 | SSLType IMAPS 40 | PipelineDepth 50 41 | 42 | IMAPStore example-remote 43 | Account example 44 | 45 | MaildirStore example-local 46 | Path /mail/ 47 | Inbox /mail/Inbox 48 | SubFolders Verbatim 49 | 50 | Channel example 51 | Master :example-remote: 52 | Slave :example-local: 53 | Patterns * 54 | Create Slave 55 | Expunge Slave 56 | SyncState * 57 | Sync Pull 58 | ``` 59 | 60 | 61 | ### Initial Sync 62 | 63 | The first time this container runs, it will download all of your historical emails. 64 | 65 | It is not required, but if you'd like to run this sync manually you can choose to do so. 66 | This allows you to temporarily interrupt it at any point and also restart if it gets stuck. 67 | 68 | ```bash 69 | $ docker run -it --rm 70 | -v /path/to/config:/config \ 71 | -v /path/to/mail:/mail \ 72 | jakewharton/mbsync:1 \ 73 | /app/sync.sh 74 | ``` 75 | 76 | This will run until all emails have been downloaded. At this point, you should set it up to run automatically on a schedule. 77 | 78 | 79 | ### Running Automatically 80 | 81 | To run the sync automatically on a schedule, pass a valid cron specifier as the `CRON` environment variable. 82 | 83 | ```bash 84 | $ docker run -it --rm 85 | -v /path/to/config:/config \ 86 | -v /path/to/mail:/mail \ 87 | -e "CRON=0 * * * *" \ 88 | jakewharton/mbsync:1 89 | ``` 90 | 91 | The above version will run every hour and download any new emails. For help creating a valid cron specifier, visit [cron.help][2]. 92 | 93 | [2]: https://cron.help/#0_*_*_*_* 94 | 95 | 96 | ### More 97 | 98 | To be notified when sync is failing visit https://healthchecks.io, create a check, and specify the ID to the container using the `HEALTHCHECK_ID` environment variable. 99 | 100 | Because the sync can occasionally fail, it's best to set a grace period on the check which is a multiple of your cron period. For example, if you run sync hourly give a grace period of two hours. 101 | 102 | To write data as a particular user, the `PUID` and `PGID` environment variables can be set to your user ID and group ID, respectively. 103 | 104 | 105 | ### Docker Compose 106 | 107 | ```yaml 108 | version: '2' 109 | services: 110 | mbsync: 111 | image: jakewharton/mbsync:1 112 | restart: unless-stopped 113 | volumes: 114 | - /path/to/config:/config 115 | - /path/to/mail:/mail 116 | environment: 117 | - "CRON=0 * * * *" 118 | #Optional: 119 | - "HEALTHCHECK_ID=..." 120 | - "PUID=..." 121 | - "PGID=..." 122 | ``` 123 | 124 | See https://hub.docker.com/r/jakewharton/mbsync/tags for other tags. 125 | 126 | 127 | 128 | LICENSE 129 | ====== 130 | 131 | MIT. See `LICENSE.txt`. 132 | 133 | Copyright 2020 Jake Wharton 134 | -------------------------------------------------------------------------------- /RELEASING.md: -------------------------------------------------------------------------------- 1 | Release Process 2 | =============== 3 | 4 | 1. Update `CHANGELOG.md` with version, date, and summary. 5 | 6 | 2. Commit changes 7 | 8 | ``` 9 | git commit -am "Prepare version X.Y.Z" 10 | ``` 11 | 12 | 3. Create annotated tag 13 | 14 | ``` 15 | git tag -a X.Y.Z -m 'Version X.Y.Z' 16 | ``` 17 | 18 | 4. Push commit and tag 19 | 20 | ``` 21 | git push && git push --tags 22 | ``` 23 | 24 | CI will pick it up, build, and push to Docker Hub automatically. 25 | -------------------------------------------------------------------------------- /root/app/sync.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/with-contenv sh 2 | 3 | echo "INFO: Starting sync.sh PID $$ $(date)" 4 | 5 | if [ -n "$HEALTHCHECK_ID" ]; then 6 | curl -sS -X POST -o /dev/null "$HEALTHCHECK_HOST/$HEALTHCHECK_ID/start" 7 | fi 8 | 9 | set -e 10 | 11 | mbsync -V -a -c /config/mbsync.rc 12 | 13 | echo "INFO: Completed sync.sh PID $$ $(date)" 14 | 15 | if [ -n "$HEALTHCHECK_ID" ]; then 16 | curl -sS -X POST -o /dev/null --fail "$HEALTHCHECK_HOST/$HEALTHCHECK_ID" 17 | fi 18 | -------------------------------------------------------------------------------- /root/etc/cont-init.d/10-adduser.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/with-contenv sh 2 | # 3 | # Copyright (c) 2017 Joshua Avalon 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | set -e 24 | 25 | PUID=${PUID:-1001} 26 | PGID=${PGID:-1001} 27 | 28 | getent group abc >/dev/null || addgroup -g "$PGID" abc 29 | id -u abc &>/dev/null || adduser -u "$PUID" -D -G abc abc 30 | 31 | echo " 32 | Initializing container 33 | 34 | User uid: $(id -u abc) 35 | User gid: $(id -g abc) 36 | " 37 | 38 | chown abc:abc /app 39 | -------------------------------------------------------------------------------- /root/etc/cont-init.d/20-cron.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/with-contenv sh 2 | 3 | if [ -z "$CRON" ]; then 4 | echo " 5 | Not running in cron mode 6 | " 7 | exit 0 8 | fi 9 | 10 | if [ ! -d /config ]; then 11 | echo " 12 | ERROR: '/config' directory must be mounted 13 | " 14 | exit 1 15 | fi 16 | if [ ! -f /config/mbsync.rc ]; then 17 | echo " 18 | ERROR: '/config/mbsync.rc' file must exist 19 | " 20 | exit 1 21 | fi 22 | 23 | if [ -z "$HEALTHCHECK_ID" ]; then 24 | echo " 25 | NOTE: Define HEALTHCHECK_ID with https://healthchecks.io to monitor sync job" 26 | fi 27 | 28 | # Set up the cron schedule. 29 | echo " 30 | Initializing cron 31 | 32 | $CRON 33 | " 34 | crontab -u abc -d # Delete any existing crontab. 35 | echo "$CRON /usr/bin/flock -n /app/sync.lock /app/sync.sh" >/tmp/crontab.tmp 36 | crontab -u abc /tmp/crontab.tmp 37 | rm /tmp/crontab.tmp 38 | -------------------------------------------------------------------------------- /root/etc/services.d/cron/run: -------------------------------------------------------------------------------- 1 | #!/usr/bin/with-contenv sh 2 | 3 | # Log level 5 (and below) is noisy during periodic wakeup where nothing happens. 4 | /usr/sbin/crond -f -l 6 5 | --------------------------------------------------------------------------------