function backup($pWrite = true)
 {
     $timeout = 20 * 60 * 60;
     //20minutes
     @set_time_limit($timeout);
     @ini_set('max_execution_time', $timeout);
     MainWP_Helper::endSession();
     //Cleanup pid files!
     $dirs = MainWP_Helper::getMainWPDir('backup');
     $backupdir = trailingslashit($dirs[0]);
     /** @var $wp_filesystem WP_Filesystem_Base */
     global $wp_filesystem;
     MainWP_Helper::getWPFilesystem();
     $files = glob($backupdir . '*');
     //Find old files (changes > 3 hr)
     foreach ($files as $file) {
         if (MainWP_Helper::endsWith($file, '/index.php') | MainWP_Helper::endsWith($file, '/.htaccess')) {
             continue;
         }
         if (time() - filemtime($file) > 60 * 60 * 3) {
             @unlink($file);
         }
     }
     $fileName = isset($_POST['fileUID']) ? $_POST['fileUID'] : '';
     if ('full' === $_POST['type']) {
         $excludes = isset($_POST['exclude']) ? explode(',', $_POST['exclude']) : array();
         $excludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/mainwp';
         $uploadDir = MainWP_Helper::getMainWPDir();
         $uploadDir = $uploadDir[0];
         $excludes[] = str_replace(ABSPATH, '', $uploadDir);
         $excludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/object-cache.php';
         if (function_exists('posix_uname')) {
             $uname = @posix_uname();
             if (is_array($uname) && isset($uname['nodename'])) {
                 if (stristr($uname['nodename'], 'hostgator')) {
                     if (!isset($_POST['file_descriptors']) || '0' == $_POST['file_descriptors'] || $_POST['file_descriptors'] > 1000) {
                         $_POST['file_descriptors'] = 1000;
                     }
                     $_POST['file_descriptors_auto'] = 0;
                     $_POST['loadFilesBeforeZip'] = false;
                 }
             }
         }
         $file_descriptors = isset($_POST['file_descriptors']) ? $_POST['file_descriptors'] : 0;
         $file_descriptors_auto = isset($_POST['file_descriptors_auto']) ? $_POST['file_descriptors_auto'] : 0;
         if (1 === (int) $file_descriptors_auto) {
             if (function_exists('posix_getrlimit')) {
                 $result = @posix_getrlimit();
                 if (isset($result['soft openfiles'])) {
                     $file_descriptors = $result['soft openfiles'];
                 }
             }
         }
         $loadFilesBeforeZip = isset($_POST['loadFilesBeforeZip']) ? $_POST['loadFilesBeforeZip'] : true;
         $newExcludes = array();
         foreach ($excludes as $exclude) {
             $newExcludes[] = rtrim($exclude, '/');
         }
         $excludebackup = isset($_POST['excludebackup']) && '1' == $_POST['excludebackup'];
         $excludecache = isset($_POST['excludecache']) && '1' == $_POST['excludecache'];
         $excludezip = isset($_POST['excludezip']) && '1' == $_POST['excludezip'];
         $excludenonwp = isset($_POST['excludenonwp']) && '1' == $_POST['excludenonwp'];
         if ($excludebackup) {
             //Backup buddy
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/backupbuddy_backups';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/backupbuddy_temp';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/pb_backupbuddy';
             //ManageWP
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/managewp';
             //InfiniteWP
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/infinitewp';
             //WordPress Backup to Dropbox
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/backups';
             //BackUpWordpress
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/backups';
             //BackWPUp
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/backwpup*';
             //WP Complete Backup
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/plugins/wp-complete-backup/storage';
             //WordPress EZ Backup
             //This one may be hard to do since they add random text at the end for example, feel free to skip if you need to
             ///backup_randomkyfkj where kyfkj is random
             //Online Backup for WordPress
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/backups';
             //XCloner
             $newExcludes[] = '/administrator/backups';
         }
         if ($excludecache) {
             //W3 Total Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/w3tc-cache';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/w3tc';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/config';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/minify';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/page_enhanced';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/tmp';
             //WP Super Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/supercache';
             //Quick Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/quick-cache';
             //Hyper Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/hyper-cache/cache';
             //WP Fastest Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/all';
             //WP-Rocket
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/wp-rocket';
         }
         $file = false;
         if (isset($_POST['f'])) {
             $file = $_POST['f'];
         } else {
             if (isset($_POST['file'])) {
                 $file = $_POST['file'];
             }
         }
         $ext = 'zip';
         if (isset($_POST['ext'])) {
             $ext = $_POST['ext'];
         }
         $pid = false;
         if (isset($_POST['pid'])) {
             $pid = $_POST['pid'];
         }
         $append = isset($_POST['append']) && '1' == $_POST['append'];
         $res = MainWP_Backup::get()->createFullBackup($newExcludes, $fileName, true, true, $file_descriptors, $file, $excludezip, $excludenonwp, $loadFilesBeforeZip, $ext, $pid, $append);
         if (!$res) {
             $information['full'] = false;
         } else {
             $information['full'] = $res['file'];
             $information['size'] = $res['filesize'];
         }
         $information['db'] = false;
     } else {
         if ('db' == $_POST['type']) {
             $ext = 'zip';
             if (isset($_POST['ext'])) {
                 $ext = $_POST['ext'];
             }
             $res = $this->backupDB($fileName, $ext);
             if (!$res) {
                 $information['db'] = false;
             } else {
                 $information['db'] = $res['file'];
                 $information['size'] = $res['filesize'];
             }
             $information['full'] = false;
         } else {
             $information['full'] = false;
             $information['db'] = false;
         }
     }
     if ($pWrite) {
         MainWP_Helper::write($information);
     }
     return $information;
 }
 public static function cloneBackupExtract()
 {
     try {
         MainWP_Helper::endSession();
         $file = isset($_POST['f']) ? $_POST['f'] : $_POST['file'];
         $testFull = false;
         if ('' === $file) {
             $dirs = MainWP_Helper::getMainWPDir('backup', false);
             $backupdir = $dirs[0];
             $files = glob($backupdir . 'download-*');
             $archiveFile = false;
             foreach ($files as $file) {
                 if (MainWP_Helper::isArchive($file, 'download-')) {
                     $archiveFile = $file;
                     break;
                 }
             }
             if (false === $archiveFile) {
                 throw new Exception(__('No download file found', 'mainwp-child'));
             }
             $file = $archiveFile;
         } else {
             if (file_exists($file)) {
                 $testFull = true;
             } else {
                 $file = ABSPATH . $file;
                 if (!file_exists($file)) {
                     throw new Exception(__('Backup file not found', 'mainwp-child'));
                 }
                 $testFull = true;
             }
         }
         //return size in kb
         $cloneInstall = new MainWP_Clone_Install($file);
         //todo: RS: refactor to get those plugins after install (after .18 release)
         $cloneInstall->readConfigurationFile();
         $plugins = get_option('mainwp_temp_clone_plugins');
         $themes = get_option('mainwp_temp_clone_themes');
         if ($testFull) {
             $cloneInstall->testDownload();
         }
         $cloneInstall->removeConfigFile();
         $cloneInstall->extractBackup();
         $pubkey = get_option('mainwp_child_pubkey');
         $uniqueId = get_option('mainwp_child_uniqueId');
         $server = get_option('mainwp_child_server');
         $nonce = get_option('mainwp_child_nonce');
         $nossl = get_option('mainwp_child_nossl');
         $nossl_key = get_option('mainwp_child_nossl_key');
         $sitesToClone = get_option('mainwp_child_clone_sites');
         $cloneInstall->install();
         $cloneInstall->updateWPConfig();
         //            $cloneInstall->update_option('mainwp_child_pubkey', $pubkey);
         //            $cloneInstall->update_option('mainwp_child_uniqueId', $uniqueId);
         //            $cloneInstall->update_option('mainwp_child_server', $server);
         //            $cloneInstall->update_option('mainwp_child_nonce', $nonce);
         //            $cloneInstall->update_option('mainwp_child_nossl', $nossl);
         //            $cloneInstall->update_option('mainwp_child_nossl_key', $nossl_key);
         //            $cloneInstall->update_option('mainwp_child_clone_sites', $sitesToClone);
         //            $cloneInstall->update_option('mainwp_child_clone_permalink', true);
         MainWP_Helper::update_option('mainwp_child_pubkey', $pubkey, 'yes');
         MainWP_Helper::update_option('mainwp_child_uniqueId', $uniqueId);
         MainWP_Helper::update_option('mainwp_child_server', $server);
         MainWP_Helper::update_option('mainwp_child_nonce', $nonce);
         MainWP_Helper::update_option('mainwp_child_nossl', $nossl, 'yes');
         MainWP_Helper::update_option('mainwp_child_nossl_key', $nossl_key);
         MainWP_Helper::update_option('mainwp_child_clone_sites', $sitesToClone);
         if (!MainWP_Helper::startsWith(basename($file), 'download-backup-')) {
             MainWP_Helper::update_option('mainwp_child_restore_permalink', true, 'yes');
         } else {
             MainWP_Helper::update_option('mainwp_child_clone_permalink', true, 'yes');
         }
         $cloneInstall->clean();
         if (false !== $plugins) {
             $out = array();
             if (is_array($plugins)) {
                 $dir = WP_CONTENT_DIR . '/plugins/';
                 $fh = @opendir($dir);
                 while ($entry = @readdir($fh)) {
                     if (!is_dir($dir . $entry)) {
                         continue;
                     }
                     if ('.' === $entry || '..' === $entry) {
                         continue;
                     }
                     if (!in_array($entry, $plugins)) {
                         MainWP_Helper::delete_dir($dir . $entry);
                     }
                 }
                 @closedir($fh);
             }
             delete_option('mainwp_temp_clone_plugins');
         }
         if (false !== $themes) {
             $out = array();
             if (is_array($themes)) {
                 $dir = WP_CONTENT_DIR . '/themes/';
                 $fh = @opendir($dir);
                 while ($entry = @readdir($fh)) {
                     if (!is_dir($dir . $entry)) {
                         continue;
                     }
                     if ('.' === $entry || '..' === $entry) {
                         continue;
                     }
                     if (!in_array($entry, $themes)) {
                         MainWP_Helper::delete_dir($dir . $entry);
                     }
                 }
                 @closedir($fh);
             }
             delete_option('mainwp_temp_clone_themes');
         }
         $output = array('result' => 'ok');
         //todo: remove old tables if other prefix?
         wp_logout();
         wp_set_current_user(0);
     } catch (Exception $e) {
         $output = array('error' => $e->getMessage());
     }
     //return size in kb
     die(json_encode($output));
 }