def tar_uncompres_files(unZipSrc, targeDir):
    if not os.path.isfile(unZipSrc):
        return
    if not os.path.isdir(targeDir):
        os.makedirs(targeDir)
    start_time = time.time()
    os.system("tar -C %s -zxvf %s " % (targeDir, unZipSrc))
    fun.print_cost_time("untar files", start_time)
def tar_uncompres_files(unZipSrc,targeDir):
	if not os.path.isfile(unZipSrc):
		return
	if not os.path.isdir(targeDir):
		os.makedirs(targeDir)
	start_time = time.time()
	os.system("tar -C %s -zxvf %s "
		%(targeDir,unZipSrc))
	fun.print_cost_time("untar files", start_time)	
def restore_oplog_mongodb(db_host, db_port,db_user, db_passwd, db_restore_path,mongo_shell_path=""):
	start_time = time.time()
	if db_user:
		cmd="%smongorestore --oplogReplay -h %s --port %s -u %s -p %s %s" %(mongo_shell_path,db_host, db_port,db_user, db_passwd, db_restore_path)
		print(cmd)
		os.system(cmd)
	else:
		cmd="%smongorestore --oplogReplay -h %s --port %s %s" %(mongo_shell_path,db_host, db_port, db_restore_path)
		print(cmd)
		os.system(cmd)
	fun.print_cost_time("restore oplog", start_time)
def restore_full_mongodb(db_host, db_port,db_user, db_passwd, db_name, db_restore_path,is_drop=False,mongo_shell_path=""):
	start_time = time.time()
	add_comm=""
	if is_drop:
		add_comm=" --drop "
	if db_user:
		add_comm=add_comm+" -u %s -p %s --authenticationDatabase admin " % (db_user,db_passwd)
	if db_name:
		add_comm=add_comm+" -d %s " % (db_name)
	cmd="%smongorestore -h %s --port %s  %s %s" %(mongo_shell_path,db_host, db_port, add_comm,db_restore_path)
	print(cmd)
	os.system(cmd)
	fun.print_cost_time("restore mongodb", start_time)
def restore_oplog_mongodb(db_host,
                          db_port,
                          db_user,
                          db_passwd,
                          db_restore_path,
                          mongo_shell_path=""):
    start_time = time.time()
    if db_user:
        cmd = "%smongorestore --oplogReplay -h %s --port %s -u %s -p %s %s" % (
            mongo_shell_path, db_host, db_port, db_user, db_passwd,
            db_restore_path)
        print(cmd)
        os.system(cmd)
    else:
        cmd = "%smongorestore --oplogReplay -h %s --port %s %s" % (
            mongo_shell_path, db_host, db_port, db_restore_path)
        print(cmd)
        os.system(cmd)
    fun.print_cost_time("restore oplog", start_time)
def restore_full_mongodb(db_host,
                         db_port,
                         db_user,
                         db_passwd,
                         db_name,
                         db_restore_path,
                         is_drop=False):
    start_time = time.time()
    add_comm = ""
    if is_drop:
        add_comm = " --drop "
    if db_user:
        add_comm = add_comm + " -u %s -p %s " % (db_user, db_passwd)
    if db_name:
        add_comm = add_comm + " -d %s " % (db_name)
    cmd = "%smongorestore -h %s --port %s  %s %s" % (
        mongo_shell_path, db_host, db_port, add_comm, db_restore_path)
    print(cmd)
    os.system(cmd)
    fun.print_cost_time("restore mongodb", start_time)
	else:
		print db_backup_zip_name+" upload fail fail !!! remote_file_size:%d , local_size:%d " % (remote_file_size,local_size)	
	#fun.list_bucket_files(oss,config.bucket)



if __name__ == "__main__":
	lock=lock_check.LockCheck();
	is_lock=lock.is_lock()
	if not is_lock:
		try:
			lock.lock();
			start_time = time.time()
			print("begin to dump mongodb database ...");
			
			is_inc_backup=config.is_inc_backup
			fun.mongo_shell_path=config.mongo_shell_path
			if is_inc_backup==1:
				do_inc_backup()
			else:
				do_full_backup()
			#time.sleep(10)
			fun.print_cost_time("all done ", start_time)
			
		except Exception, e:
			raise e
		finally:
			lock.release()
	else:
		print "now is running backup db,exit!"
    start_time = time.time()
    if db_user:
        cmd = "%smongorestore --oplogReplay -h %s --port %s -u %s -p %s %s" % (
            mongo_shell_path, db_host, db_port, db_user, db_passwd,
            db_restore_path)
        print(cmd)
        os.system(cmd)
    else:
        cmd = "%smongorestore --oplogReplay -h %s --port %s %s" % (
            mongo_shell_path, db_host, db_port, db_restore_path)
        print(cmd)
        os.system(cmd)
    fun.print_cost_time("restore oplog", start_time)


if __name__ == "__main__":
    start_time = time.time()

    fun.mongo_shell_path = mongo_shell_path

    db_inc_backup_path = oplog_backup(restore_local_temp_path)
    #time.sleep(15)
    #db_inc_backup_path="H:\\pythoncode\\temp\\oplog_temp\\oplog_backup\\oplog_20151224194545"
    print db_inc_backup_path
    if db_inc_backup_path:
        restore_oplog_mongodb(target_db_host, target_db_port, target_db_user,
                              target_db_passwd, db_inc_backup_path,
                              mongo_shell_path)

    fun.print_cost_time("syn done ", start_time)

def restore_oplog_mongodb(db_host, db_port,db_user, db_passwd, db_restore_path,mongo_shell_path=""):
	start_time = time.time()
	if db_user:
		cmd="%smongorestore --oplogReplay -h %s --port %s -u %s -p %s %s" %(mongo_shell_path,db_host, db_port,db_user, db_passwd, db_restore_path)
		print(cmd)
		os.system(cmd)
	else:
		cmd="%smongorestore --oplogReplay -h %s --port %s %s" %(mongo_shell_path,db_host, db_port, db_restore_path)
		print(cmd)
		os.system(cmd)
	fun.print_cost_time("restore oplog", start_time)



	
if __name__ == "__main__":
	start_time = time.time()
	
	fun.mongo_shell_path=mongo_shell_path
	
	db_inc_backup_path=oplog_backup(restore_local_temp_path)
	#time.sleep(15)
	#db_inc_backup_path="H:\\pythoncode\\temp\\oplog_temp\\oplog_backup\\oplog_20151224194545"
	print db_inc_backup_path
	if db_inc_backup_path:
		restore_oplog_mongodb(target_db_host, target_db_port,target_db_user, target_db_passwd, db_inc_backup_path,mongo_shell_path)
			
	fun.print_cost_time("syn done ", start_time)