Not sure why all the complexity. I just set my Gmail account to forward to an address on my self-hosted domain, and set it to delete after. Then I can check my “gmail” using standard IMAP on my own server (I also run RainLoop for a webmail interface). Sending mail back through Gmail is more complex, though, since Google put some protections on it to prevent spam. Since I don’t have to send from my Gmail account very often, I just log in to the web interface those rare times I need to do it.
You can use Thunderbird to copy your old messages from your Gmail account to your self-hosted IMAP.