def tablerestore()

in src/MultipleTableRestoreUtility/multitablerestore.py [0:0]


def tablerestore(tgtdbname, srcdbname, snapshotid, clusterid, filename):
    previous_status = None
    try:
        with open(filename) as data_file:
            datac = json.load(data_file)
            #  Check json for valid key
            if 'TableRestoreList' not in datac:
                print 'ERROR: \'%s\' key in %s list is an invalid key. Valid key is \'TableRestoreList\'.' \
                      % (datac.keys()[0], filename)
                exit()
    #  Check restore list file exists
    except IOError:
        print 'Table restore list %s does not exist. Check file and try again.' % filename
        exit()
    except Exception as e:
        print e
        exit()

    count_succeeded = 0
    count_failed = 0
    count_canceled = 0
    count_unknown = 0
    total_restore_size = 0

    for i in datac['TableRestoreList']:
        try:
            srcschema = i['Schemaname']
            srctable = i['Tablename']
        except KeyError as e:
            print 'ERROR: Expected key %s is missing in %s.' % (e, filename)
            print 'DETAIL: %s' % i
            exit()
        tgtschema = srcschema
        trgttable = srctable
        tlr = RsRestore(clusterid, snapshotid, srcdbname, srcschema, tgtdbname, tgtschema)
        tlr.restoretable(srctable, trgttable)
        print "%s Starting Table Restores %s" % ('-' * 50, '-' * 50)
        print "[%s] Requestid: %s " % (str(datetime.datetime.now()), tlr.restorestatus('TableRestoreRequestId'))
        print "[%s] INFO: Starting restore of %s to schema %s in database %s" % (str(datetime.datetime.now()),
                                                                                 trgttable, tgtschema, tgtdbname)
        current_status = tlr.restorestatus('Status')
        while current_status != 'SUCCEEDED' and current_status != 'FAILED' and current_status != 'CANCELED':
            if current_status != previous_status:
                previous_status = current_status
                tlr.printmessage(current_status)
            sleep(2)
            current_status = tlr.restorestatus('Status')

        if current_status == 'SUCCEEDED':
            count_succeeded += 1
            total_restore_size += tlr.restorestatus('TotalDataInMegaBytes')
        elif current_status == 'FAILED':
            count_failed += 1
        elif current_status == 'CANCELED':
            count_canceled += 1
        else:
            count_unknown += 1
        tlr.printmessage(current_status)
    print "%s Table Restore Summary %s" % ('-' * 51, '-' * 51)
    print "[%s] Succeeded: %d Failed: %d Canceled: %d Unknown: %d Total Size: %dMB" \
          % (str(datetime.datetime.now()), count_succeeded, count_failed, count_canceled,
             count_unknown, total_restore_size)
    print "%s" % ('-' * 125)