Use at your own risk. Make sure to dry-run on an inconsequential account to make sure you do not wipe out precious mails.
To take regular incremental backups of Gmail, dovecot and imapsync are very good options. First you have to install and configure dovecot on your Debian or Ubuntu PC using apt install dovecot-imapd. You may want to change the default location for mail storage, particularly inbox. Gmail will offer you SSL-protected data; to push to an SSL-protected target, make sure dovecot listens only for imaps (port 993) and not imap (port 143).
You can test your dovecot installation by registering the account on Thunderbird (or some other imap client) and creating some folders and copying, pasting, and deleting some mails. If you have enough storage on your PC or laptop, you can set up dovecot right there. Why not use Thunderbird itself to backup to local storage? Thunderbird won't do incremental backups, or recover from broken connections.
So the second step is to install imapsync, which does not come apt-packaged as of 2021/12/10. Here are the instructions to install it on Ubuntu 20.04.
# apt install git rcs make makepasswd cpanminus gcc libssl-dev libauthen-ntlm-perl libclass-load-perl libcrypt-ssleay-perl liburi-perl libdata-uniqid-perl libdigest-hmac-perl libdist-checkconflicts-perl libfile-copy-recursive-perl libio-compress-perl libio-socket-inet6-perl libio-socket-ssl-perl libio-tee-perl libmail-imapclient-perl libmodule-scandeps-perl libnet-ssleay-perl libpar-packer-perl libreadonly-perl libsys-meminfo-perl libterm-readkey-perl libtest-fatal-perl libtest-mock-guard-perl libtest-pod-perl libtest-requires-perl libtest-simple-perl libunicode-string-perl apt-file # apt-file update # cpanm Crypt::OpenSSL::RSA Crypt::OpenSSL::Random File::Tail Mail::IMAPClient JSON::WebToken Test::MockObject Unicode::String Data::Uniqid Encode::IMAPUTF7 Regexp::Common Test::Deep CGI # cd /opt # git clone https://github.com/imapsync/imapsync.git # cd imapsync # make testf # make install
Your make testf might fail in the IPv6 tests if your network does not support IPv6. Finally, follow the imapsync instructions to sync from Gmail to your personal dovecot. Make sure to include bandwidth throttle options if you have gigabytes of emails, otherwise Gmail may cut off your critical devices from accessing mail, or allow very slow access. You also have to enable imap access to your Gmail and bypass Gmail's OAUTH2 authentication and "allow less secure apps" temporarily, but if you were using Thunderbird to access Gmail, you were doing this already. I haven't tried this yet with 2FA-protected accounts.