예제 #1
0
def main():

    parser = optparse.OptionParser(version="%prog 1.0")
    parser.add_option("-m", "--media-type", dest="media_type", type="string", help="Specify media type (video/audio/image). Default is video")
    parser.add_option("-s", "--session", dest="use_session_ids", action="store_true", help="Arguments must be session ids. (All media for each session will be restored)")
    parser.add_option("-f", "--file", dest="use_files", action="store_true", help="Arguments must be files. (All media listed in each file will be restored)")
    (options, args) = parser.parse_args()
    
    ltoRestoreUtil.check_restore_args(options, args)
    if options.media_type == None or options.media_type == "":
        domain = 'video' 
    else:
        domain = options.media_type
    items = args

    config = ConfigParser.ConfigParser()
    ltoUtil.load_config(config)
    ltoUtil.config_checks(config)
    ltoUtil.path_check(ltoUtil.get_restore_dir(config))

    if options.use_files:
        items = ltoRestoreUtil.parse_files(args)
        
    if options.use_session_ids:
        items = ltoRestoreUtil.get_media_ids(config, args, domain)
    
    ltoRestoreUtil.check_media_ids_exist(config, items, domain)
    item_vectors = ltoRestoreUtil.get_item_vectors(config, items, domain)
    ltoRestoreUtil.check_total_size(config, item_vectors)
    ltoRestoreUtil.restore_media_items(config, domain, item_vectors)
def restore_media_items(config, domain, vectors):
    tape = ltoUtil.get_tape_device(config)
    restore_dir = ltoUtil.get_restore_dir(config)
    bs = int(ltoUtil.get_blocksize(config))
    blocking_factor = bs/512
    setup_done = False
    prev_tape_id = ""
    
    for v in vectors:
        tape_id = v[0]
        offset = v[1]
        filesize = v[2]
        record_offset = v[3]
        filename = v[4]
        md5 = v[5]
        
        blocks = int(filesize/bs)+2
        trim = (record_offset*512)%bs
        
        if not prev_tape_id == tape_id:
            raw_input('Insert tape: '+tape_id+' [Press Enter to continue]')
            if not setup_done:
                ltoWriteUtil.setup_tape_drive(config)
                setup_done = True
    
        print '\nSeeking to tape block: '+str(offset)
        p = subprocess.Popen('mt -f '+tape+' seek '+str(offset), shell=True)
        sts = os.waitpid(p.pid, 0)
    
        print 'Restoring file: '+filename
        p = subprocess.Popen('dd if='+tape+' bs='+str(bs)+' count='+str(blocks)+' | dd bs='+str(trim)+' skip=1 | tar -x -C '+restore_dir+' '+filename+' --occurrence', shell=True, stderr=subprocess.PIPE)
        sts = os.waitpid(p.pid, 0)
        
        #md5 check restored files
        if md5 == ltoUtil.get_md5_hash(restore_dir+'/'+filename):
            print 'Verified MD5: '+filename
        else: 
            print 'MD5 verification failed for '+filename
        
        #Rename files to include event metadata
        extra_metadata_str = get_session_full_name(config, session_id)
        filepath = restore_dir+'/'+filename
        ltoUtil.move(filepath, filepath[:-4]+'_'+extra_metadata_str+'.'+ltoUtil.get_file_extn(filepath))
        
        prev_tape_id = tape_id

    print '\nAll files successfully restored to '+ltoUtil.get_restore_dir(config)
def check_total_size(config, vectors):
    total_size = 0
    for v in vectors:
        total_size += v[2]
    free_bytes = ltoUtil.get_freespace(ltoUtil.get_restore_dir(config))
    if total_size > free_bytes - (1024*1024):
        print 'Insufficient free space on drive ('+ltoUtil.format_bytes_to_gbs(free_bytes)+') to restore all files ('+ltoUtil.format_bytes_to_gbs(total_size)+').'
        print ltoUtil.get_script_name()+' script terminated.'
        sys.exit(2)