public static function executeBackupTask($task, $nrOfSites = 0, $updateRun = true)
 {
     if ($updateRun) {
         MainWP_DB::Instance()->updateBackupRun($task->id);
     }
     $task = MainWP_DB::Instance()->getBackupTaskById($task->id);
     $completed_sites = $task->completed_sites;
     if ($completed_sites != '') {
         $completed_sites = json_decode($completed_sites, true);
     }
     if (!is_array($completed_sites)) {
         $completed_sites = array();
     }
     $sites = array();
     if ($task->groups == '') {
         if ($task->sites != '') {
             $sites = explode(',', $task->sites);
         }
     } else {
         $groups = explode(',', $task->groups);
         foreach ($groups as $groupid) {
             $group_sites = MainWP_DB::Instance()->getWebsitesByGroupId($groupid);
             foreach ($group_sites as $group_site) {
                 if (in_array($group_site->id, $sites)) {
                     continue;
                 }
                 $sites[] = $group_site->id;
             }
         }
     }
     $errorOutput = null;
     $lastStartNotification = $task->lastStartNotificationSent;
     if ($updateRun && get_option('mainwp_notificationOnBackupStart') == 1 && $lastStartNotification < $task->last_run) {
         $email = MainWP_DB::Instance()->getUserNotificationEmail($task->userid);
         if ($email != '') {
             $output = 'A scheduled backup has started with MainWP on ' . MainWP_Utility::formatTimestamp(MainWP_Utility::getTimestamp(time())) . ' for the following ' . count($sites) . ' sites:<br />';
             foreach ($sites as $siteid) {
                 $website = MainWP_DB::Instance()->getWebsiteById($siteid);
                 $output .= '&nbsp;&bull;&nbsp;<a href="' . $website->url . '">' . MainWP_Utility::getNiceURL($website->url) . '</a><br />';
             }
             $output .= '<br />Backup Details:<br /><br />';
             $output .= '<strong>Backup Task</strong>' . ' - ' . $task->name . '<br />';
             $output .= '<strong>Backup Type</strong>' . ' - ' . ($task->type == 'db' ? 'DATABASE BACKUP' : 'FULL BACKUP') . '<br />';
             $output .= '<strong>Backup Schedule</strong>' . ' - ' . strtoupper($task->schedule) . '<br />';
             wp_mail($email, 'A Scheduled Backup has been Started - MainWP', MainWP_Utility::formatEmail($email, $output), 'content-type: text/html');
             MainWP_DB::Instance()->updateBackupTaskWithValues($task->id, array('lastStartNotificationSent' => time()));
         }
     }
     $currentCount = 0;
     foreach ($sites as $siteid) {
         if (isset($completed_sites[$siteid]) && $completed_sites[$siteid] == true) {
             continue;
         }
         $website = MainWP_DB::Instance()->getWebsiteById($siteid);
         try {
             $subfolder = str_replace('%task%', MainWP_Utility::sanitize($task->name), $task->subfolder);
             $backupResult = MainWP_Manage_Sites::backupSite($siteid, $task, $subfolder);
             //When we receive a timeout, we return false..
             if ($backupResult === false) {
                 continue;
             }
             if ($errorOutput == null) {
                 $errorOutput = '';
             }
             $error = false;
             $tmpErrorOutput = '';
             if (isset($backupResult['error'])) {
                 $tmpErrorOutput .= $backupResult['error'] . '<br />';
                 $error = true;
             }
             if (isset($backupResult['ftp']) && $backupResult['ftp'] != 'success') {
                 $tmpErrorOutput .= 'FTP: ' . $backupResult['ftp'] . '<br />';
                 $error = true;
             }
             if (isset($backupResult['dropbox']) && $backupResult['dropbox'] != 'success') {
                 $tmpErrorOutput .= 'Dropbox: ' . $backupResult['dropbox'] . '<br />';
                 $error = true;
             }
             if (isset($backupResult['amazon']) && $backupResult['amazon'] != 'success') {
                 $tmpErrorOutput .= 'Amazon: ' . $backupResult['amazon'] . '<br />';
                 $error = true;
             }
             if ($error) {
                 $errorOutput .= 'Site: <strong>' . MainWP_Utility::getNiceURL($website->url) . '</strong><br />';
                 $errorOutput .= $tmpErrorOutput . '<br />';
             }
         } catch (Exception $e) {
             if ($errorOutput == null) {
                 $errorOutput = '';
             }
             $errorOutput .= 'Site: <strong>' . MainWP_Utility::getNiceURL($website->url) . '</strong><br />';
             $errorOutput .= MainWP_Error_Helper::getErrorMessage($e) . '<br />';
             $_error_output = MainWP_Error_Helper::getErrorMessage($e);
         }
         $_backup_result = isset($backupResult) ? $backupResult : (isset($_error_output) ? $_error_output : '');
         do_action('mainwp_managesite_schedule_backup', $website, array('type' => $task->type), $_backup_result);
         $currentCount++;
         $task = MainWP_DB::Instance()->getBackupTaskById($task->id);
         $completed_sites = $task->completed_sites;
         if ($completed_sites != '') {
             $completed_sites = json_decode($completed_sites, true);
         }
         if (!is_array($completed_sites)) {
             $completed_sites = array();
         }
         $completed_sites[$siteid] = true;
         MainWP_DB::Instance()->updateCompletedSites($task->id, $completed_sites);
         if ($nrOfSites != 0 && $nrOfSites <= $currentCount) {
             break;
         }
     }
     //update completed sites
     if ($errorOutput != null) {
         MainWP_DB::Instance()->updateBackupErrors($task->id, $errorOutput);
     }
     if (count($completed_sites) == count($sites)) {
         MainWP_DB::Instance()->updateBackupCompleted($task->id);
         if (get_option('mainwp_notificationOnBackupFail') == 1) {
             $email = MainWP_DB::Instance()->getUserNotificationEmail($task->userid);
             if ($email != '') {
                 $task = MainWP_DB::Instance()->getBackupTaskById($task->id);
                 if ($task->backup_errors != '') {
                     $errorOutput = 'Errors occurred while executing task: <strong>' . $task->name . '</strong><br /><br />' . $task->backup_errors;
                     wp_mail($email, 'A Scheduled Backup had an Error - MainWP', MainWP_Utility::formatEmail($email, $errorOutput), 'content-type: text/html');
                     MainWP_DB::Instance()->updateBackupErrors($task->id, '');
                 }
             }
         }
     }
     return $errorOutput == '';
 }