def deleted_old_backups_from_ftp_servers(backups):
    ftpServersCleaned = []
    for vmName in backups:
        connectionInfo = _get_connectionInfo_by_vmName(vmName)
        if connectionInfo[0] not in ftpServersCleaned:
            logging.warn("* a connection to ftp server [{0}] will be performed to see if contains old backups. "\
                         "If old backups are found, they will be deleted".format(connectionInfo[0]))
            ftpServersCleaned.append(connectionInfo[0])
            ftphost = _get_ftpHost_by_vmName(vmName)
            ftphost.connect_to_host()

            backupsOnRemoteFtpServer =  backupManager.getBackupsFromFtpServer(ftphost)
            logging.debug("** Ftp Server [{0}] stores the following backups: \n{1}".format(connectionInfo[0],
                backupRender.get_backups_infos(backupsOnRemoteFtpServer)))

            backupsToDelete, backupsToUpload = backupManager.get_backups_for_upload_and_delete(backups, ftphost)
            if len(backupsToDelete) > 0:
                logging.warn(
                    "** Ftp Server [{0}] contains {1} old backups that will be now deleted.".format(connectionInfo[0],
                        len(backupsToDelete)))
                logging.debug("** this are the backups that will be deleted:\n{0}".format(
                    backupRender.get_backups_infos(backupsToDelete)))
                if _use_real_ftp_sync:
                    backupManager.delete_backups_from_ftpHost(backupsToDelete, ftphost)
            else:
                logging.info(
                    "Ftp Server [{0}] does not contains old backups. No file deletions will be performed.".format(
                        connectionInfo[0]))

            ftphost.disconnect_from_host()
def display_dump_file(dumpFilePath):
    '''
    displays the content of the given dump file into the console
    Args: dumpFilePath: str -> the path of the dump file to display
    '''
    backupsToDisplay = backupSerializer.get_backups_from_dump_file_or_None(dumpFilePath)
    print(backupRender.get_backups_infos(backupsToDisplay))
def main(params):


    _configure_logger(params.verbosity)
    _draw_welcome_banner()
    _import_ftp_config(params.configFtp)

    global _use_real_ftp_sync
    _use_real_ftp_sync = not params.simulate

    global _upload_method
    _upload_method = params.uploadMethod

    global _send_mail
    _send_mail = params.sendMail


    if(_use_real_ftp_sync == False):
        logging.warn("* You have provided the -s parameter and no real action to ftp sync will be performed!")

    try:
        if(params.displayBackups):
            logging.info("* backups stored into ftp servers are: \n: {0}".
            format(backupRender.get_backups_infos(get_backups_from_ftp_servers())))
        else:
            start_backup(params.folder, params.numberOfBackups)

        if params.execute != None:
            logging.debug('-x has been specified. running: {0}'.format(params.execute))
            p = Popen(params.execute)
            stdout, stderr = p.communicate()
            logging.debug(stdout)

    except Exception as ex:
        logging.error(ex)
        return 1

    logging.debug("the program has terminated. byee!")
    return 0
def start_backup(vmFolderTreePath, numberOfBackupsToKeep):
    '''
    starts the backup programs.
    Args:   vmFolderTreePath: str -> path of the folder that contains the virtual machines backups
            vmBackupHistoryDumpFilePath: str -> path to the dump file that stores the backupHistory
            numberOfBackupsToKeep: int -> number of tha max backups to keep. old backups will be removed
    '''

    try:

        backupsToUpload= backupManager.getBackupsFromFolderTree(vmFolderTreePath)
        if len(backupsToUpload) == 0:
            logging.warn("No new backups are found in folder {0} . there is no need to continue! exiting....".format(vmFolderTreePath))
            sys.exit()

        logging.debug("* the provided local path [{0}] contains the following backups that will be uploaded to respective" \
                      " ftp servers: \n {1}".format(vmFolderTreePath, backupRender.get_backups_infos(backupsToUpload)))
        backupsOnFtpServers = get_backups_from_ftp_servers()
        logging.info("* backups stored into ftp servers are: \n: {0}".format(backupRender.get_backups_infos(backupsOnFtpServers)))




        logging.debug('---- backup to upload-------')
        logging.debug('FIX- upload_new_backups_to_ftp_server')
        logging.debug(backupsToUpload)
        logging.debug('-----------')
        for vmName in backupsToUpload:
            logging.debug(backupsToUpload[vmName])
        logging.debug('end fix -----------')



        logging.debug('------ backup on server -----')
        logging.debug('FIX- upload_new_backups_to_ftp_server')
        logging.debug(backupsOnFtpServers)
        logging.debug('-----------')
        for vmName in backupsOnFtpServers:
            logging.debug(backupsOnFtpServers[vmName])
        logging.debug('end fix -----------')





        backups = backupManager.get_merge_of_backups(backupsToUpload, backupsOnFtpServers)



        logging.debug('------ backup  merge-----')
        logging.debug('FIX- upload_new_backups_to_ftp_server')
        logging.debug(backups)
        logging.debug('-----------')
        for vmName in backups:
            logging.debug(backups[vmName])
        logging.debug('end fix -----------')



        logging.info("* the merge of backups found in backup folder and those present int the dump file has finished"
                      " successfully. The next step is to remove old backus.")
        sort_and_remove_old_backups(backups, numberOfBackupsToKeep)
        logging.debug("* removing of old backups (max {0} backups) has finished.".format(numberOfBackupsToKeep))
        logging.info("* the current backup status is:\n{1}".format(numberOfBackupsToKeep,
            backupRender.get_backups_infos(backups)))
        logging.info("* This program will now start to synchronize the current VM backup status with the remote ftp servers."
                     " This means old backups will be deleted and new ones will be uploaded to specified ftp servers")
        if _use_real_ftp_sync:
            try:
                sync_backups_with_ftp_servers(vmFolderTreePath, backups)
            except Exception as ex:
                logging.error("An error occurred while syncing the backup: {0}\n trace: {1}".format(ex, traceback.format_exc()))
                raise ex
        else: logging.info("As the parameter -S (Simulate) has been provided,  ftp sync will be skipped")

        _send_mail_with_log(False)

    except Exception:
        _send_mail_with_log(True)
        raise