示例#1
0
    def process_binlog(self):
        stream = BinLogStreamReader(connection_settings=self.connectionSettings, server_id=self.serverId,
                                    log_file=self.startFile, log_pos=self.startPos, only_schemas=self.only_schemas,
                                    only_tables=self.only_tables, resume_stream=True)

        cur = self.connection.cursor()
        tmpFile = create_unique_file('%s.%s' % (self.connectionSettings['host'],self.connectionSettings['port'])) # to simplify code, we do not use file lock for tmpFile.
        ftmp = open(tmpFile ,"w")
        flagLastEvent = False
        eStartPos, lastPos = stream.log_pos, stream.log_pos
        try:
            for binlogevent in stream:
                if not self.stopnever:
                    if (stream.log_file == self.endFile and stream.log_pos == self.endPos) or (stream.log_file == self.eofFile and stream.log_pos == self.eofPos):
                        flagLastEvent = True
                    elif datetime.datetime.fromtimestamp(binlogevent.timestamp) < self.startTime:
                        if not (isinstance(binlogevent, RotateEvent) or isinstance(binlogevent, FormatDescriptionEvent)):
                            lastPos = binlogevent.packet.log_pos
                        continue
                    elif (stream.log_file not in self.binlogList) or (self.endPos and stream.log_file == self.endFile and stream.log_pos > self.endPos) or (stream.log_file == self.eofFile and stream.log_pos > self.eofPos) or (datetime.datetime.fromtimestamp(binlogevent.timestamp) >= self.stopTime):
                        break
                    # else:
                    #     raise ValueError('unknown binlog file or position')

                if isinstance(binlogevent, QueryEvent) and binlogevent.query == 'BEGIN':
                    eStartPos = lastPos

                if isinstance(binlogevent, QueryEvent):
                    sql = concat_sql_from_binlogevent(cursor=cur, binlogevent=binlogevent, flashback=self.flashback, nopk=self.nopk)
                    if sql:
                        print sql
                elif isinstance(binlogevent, WriteRowsEvent) or isinstance(binlogevent, UpdateRowsEvent) or isinstance(binlogevent, DeleteRowsEvent):
                    for row in binlogevent.rows:
                        sql = concat_sql_from_binlogevent(cursor=cur, binlogevent=binlogevent, row=row , flashback=self.flashback, nopk=self.nopk, eStartPos=eStartPos)
                        if self.flashback:
                            ftmp.write(sql + '\n')
                        else:
                            print sql

                if not (isinstance(binlogevent, RotateEvent) or isinstance(binlogevent, FormatDescriptionEvent)):
                    lastPos = binlogevent.packet.log_pos
                if flagLastEvent:
                    break
            ftmp.close()
            if self.flashback:
                with open(tmpFile) as ftmp:
                    for line in reversed_lines(ftmp):
                        print line.rstrip()
        finally:
            os.remove(tmpFile)
        cur.close()
        stream.close()
        return True
示例#2
0
    def process_binlog(self):
        stream = BinLogFileReader(
            self.filePath,
            ctl_connection_settings=self.connectionSettings,
            log_pos=self.startPos,
            only_schemas=self.only_schemas,
            only_tables=self.only_tables,
            resume_stream=True)

        cur = self.connection.cursor()
        tmpFile = create_unique_file(
            '%s.%s' %
            (self.connectionSettings['host'], self.connectionSettings['port'])
        )  # to simplify code, we do not use file lock for tmpFile.
        ftmp = open(tmpFile, "w")
        flagLastEvent = False
        eStartPos, lastPos = stream.log_pos, stream.log_pos
        try:
            for binlogevent in stream:
                if not self.stopnever:
                    if datetime.datetime.fromtimestamp(
                            binlogevent.timestamp) < self.startTime:
                        if not (isinstance(binlogevent, RotateEvent)
                                or isinstance(binlogevent,
                                              FormatDescriptionEvent)):
                            lastPos = binlogevent.packet.log_pos
                        continue
                    elif datetime.datetime.fromtimestamp(
                            binlogevent.timestamp) >= self.stopTime:
                        break
                    else:
                        pass

                if isinstance(binlogevent,
                              QueryEvent) and binlogevent.query == 'BEGIN':
                    eStartPos = lastPos

                if isinstance(binlogevent, QueryEvent):
                    sql = concat_sql_from_binlogevent(cursor=cur,
                                                      binlogevent=binlogevent,
                                                      flashback=self.flashback,
                                                      no_pk=self.nopk)
                    if sql:
                        print sql
                        print "\n"
                elif isinstance(binlogevent, WriteRowsEvent) or isinstance(
                        binlogevent, UpdateRowsEvent) or isinstance(
                            binlogevent, DeleteRowsEvent):
                    for row in binlogevent.rows:
                        sql = concat_sql_from_binlogevent(
                            cursor=cur,
                            binlogevent=binlogevent,
                            row=row,
                            flashback=self.flashback,
                            no_pk=self.nopk,
                            eStartPos=eStartPos)
                        if self.flashback:
                            ftmp.write(sql + '\n')
                        else:
                            print sql
                            print "\n"

                if not (isinstance(binlogevent, RotateEvent)
                        or isinstance(binlogevent, FormatDescriptionEvent)):
                    lastPos = binlogevent.packet.log_pos
                if flagLastEvent:
                    break
            ftmp.close()

            if self.flashback:
                self.print_rollback_sql(tmpFile)
        finally:
            os.remove(tmpFile)
        cur.close()
        stream.close()
        return True