Copy rows to a set of backup tables, then purge rows.
    :param timestamp: Archive rows older than this timestamp
    :return:

def _archive_jobs(timestamp):
    '''
    Copy rows to a set of backup tables, then purge rows.
    :param timestamp: Archive rows older than this timestamp
    :return:
    '''
    source_tables = ['jids',
                     'salt_returns',
                     'salt_events']

    with _get_serv() as cur:
        target_tables = {}
        for table_name in source_tables:
            try:
                tmp_table_name = table_name + '_archive'
                sql = 'create table if not exists {0} like {1}'.format(tmp_table_name, table_name)
                cur.execute(sql)
                cur.execute('COMMIT')
                target_tables[table_name] = tmp_table_name
            except MySQLdb.Error as e:
                log.error('mysql returner archiver was unable to create the archive tables.')
                log.error(six.text_type(e))
                raise salt.exceptions.SaltRunnerError(six.text_type(e))

        try:
            sql = 'insert into `{0}` select * from `{1}` where jid in (select distinct jid from salt_returns where alter_time < %s)'.format(target_tables['jids'], 'jids')
            cur.execute(sql, (timestamp,))
            cur.execute('COMMIT')
        except MySQLdb.Error as e:
            log.error('mysql returner archiver was unable to copy contents of table \'jids\'')
            log.error(six.text_type(e))
            raise salt.exceptions.SaltRunnerError(six.text_type(e))
        except Exception as e:
            log.error(e)
            raise

        try:
            sql = 'insert into `{0}` select * from `{1}` where alter_time < %s'.format(target_tables['salt_returns'], 'salt_returns')
            cur.execute(sql, (timestamp,))
            cur.execute('COMMIT')
        except MySQLdb.Error as e:
            log.error('mysql returner archiver was unable to copy contents of table \'salt_returns\'')
            log.error(six.text_type(e))
            raise salt.exceptions.SaltRunnerError(six.text_type(e))

        try:
            sql = 'insert into `{0}` select * from `{1}` where alter_time < %s'.format(target_tables['salt_events'], 'salt_events')
            cur.execute(sql, (timestamp,))
            cur.execute('COMMIT')
        except MySQLdb.Error as e:
            log.error('mysql returner archiver was unable to copy contents of table \'salt_events\'')
            log.error(six.text_type(e))
            raise salt.exceptions.SaltRunnerError(six.text_type(e))

    return _purge_jobs(timestamp)