imapbackup icon indicating copy to clipboard operation
imapbackup copied to clipboard

ERROR: command: FETCH => socket error: EOF

Open greatestradioshow opened this issue 1 year ago • 3 comments

Hello,

I know the last update was 3 years ago, but still I have to say it is one of my favorite scripts when it comes to creating MBOX backups.

I'm just facing the issue right now that I want to back up the "All Mail" label from a huge Gmail inbox ( 200 GB / 5,500,000 emails).

When I'm running the script, I get already the "ERROR: command: FETCH => socket error: EOF" during the initialization phase.

To overcome this, asked AI to help me to adjust the "connect_and_login" function. At first, it looked quite promising. The download started and roughly 1 GB was downloaded during 2,5 h.

def connect_and_login(config):
    """Connects to the server and logs in.  Returns IMAP4 object."""
    try:
        assert not (('keyfilename' in config) ^ ('certfilename' in config))
        if config['timeout']:
            socket.setdefaulttimeout(config['timeout'])

        server = None
        while True:
            try:
                if config['usessl'] and 'keyfilename' in config:
                    print("Connecting to '%s' TCP port %d," % (
                        config['server'], config['port']),)
                    print("SSL, key from %s," % (config['keyfilename']),)
                    print("cert from %s " % (config['certfilename']))
                    server = imaplib.IMAP4_SSL(
                        config['server'], config['port'], config['keyfilename'], config['certfilename'])
                elif config['usessl']:
                    print("Connecting to '%s' TCP port %d, SSL" % (
                        config['server'], config['port']))
                    server = imaplib.IMAP4_SSL(config['server'], config['port'])
                else:
                    print("Connecting to '%s' TCP port %d" % (
                        config['server'], config['port']))
                    server = imaplib.IMAP4(config['server'], config['port'])

                # speed up interactions on TCP connections using small packets
                server.sock.setsockopt(
                    socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

                print("Logging in as '%s'" % (config['user']))
                r, d = server.login(config['user'], config['pass'])
                assert r == 'OK', 'login failed'

                # If login is successful, break out of the loop
                break
            except (socket.error, imaplib.IMAP4.error) as e:
                if isinstance(e, socket.error):
                    print("ERROR: could not connect to '%s' (%s)" % (
                        config['server'], e))
                else:
                    print("ERROR:", e)

                # If there's a socket error, try reconnecting
                if server is not None:
                    try:
                        server.logout()
                    except:
                        pass
                server = None
                time.sleep(1)  # Wait for a second before attempting to reconnect

        # Return the connected server object
        return server

    except socket.gaierror as e:
        (err, desc) = e
        print("ERROR: problem looking up server '%s' (%s %s)" % (
            config['server'], err, desc))
        sys.exit(3)

But after 2,5 h the download was interrupted, and I saw again the same error: "ERROR: command: FETCH => socket error: EOF"

As I was running a backup with a different tool at the same time for the same account and this tool also stopped, I'm assuming the Google server interrupted the connection.

My question, how the "ERROR: command: FETCH => socket error: EOF" be resolved, and the script adjusted in that manner, that if the connection is lost or an error occurs it is not terminating, instead it tries to reconnect and continues?

In the end, I would like to use this script to back up the account unattended over the next 5 - 10 days.

Thanks in advance.

greatestradioshow avatar Feb 27 '24 21:02 greatestradioshow