static function backup_and_rename_htaccess($src_file_path, $suffix = 'backup') { global $aio_wp_security; //Check to see if the main "backups" directory exists - create it otherwise $aiowps_backup_dir = WP_CONTENT_DIR . '/' . AIO_WP_SECURITY_BACKUPS_DIR_NAME; if (!AIOWPSecurity_Utility_File::create_dir($aiowps_backup_dir)) { $aio_wp_security->debug_logger->log_debug("backup_and_rename_htaccess - Creation of backup directory failed!", 4); return false; } $src_parts = pathinfo($src_file_path); $backup_file_name = $src_parts['basename'] . '.' . $suffix; $backup_file_path = $aiowps_backup_dir . '/' . $backup_file_name; if (!copy($src_file_path, $backup_file_path)) { //Failed to make a backup copy return false; } return true; }
/** * This function will perform a database backup */ function execute_backup() { global $wpdb, $aio_wp_security; $is_multi_site = false; @ini_set('auto_detect_line_endings', true); if (function_exists('is_multisite') && is_multisite()) { //Let's get the current site's table prefix $site_pref = esc_sql($wpdb->prefix); $db_query = "SHOW TABLES LIKE '" . $site_pref . "%'"; $tables = $wpdb->get_results($db_query, ARRAY_N); $is_multi_site = true; } else { //get all of the tables $tables = $wpdb->get_results('SHOW TABLES', ARRAY_N); if (empty($tables)) { $aio_wp_security->debug_logger->log_debug("execute_backup() - no tables found!", 4); return FALSE; } } $return = ''; //cycle through each table foreach ($tables as $table) { $result = $wpdb->get_results('SELECT * FROM `' . $table[0] . '`;', ARRAY_N); $num_fields = sizeof($wpdb->get_results('DESCRIBE `' . $table[0] . '`;')); $return .= 'DROP TABLE IF EXISTS `' . $table[0] . '`;'; $row2 = $wpdb->get_row('SHOW CREATE TABLE `' . $table[0] . '`;', ARRAY_N); if (empty($row2)) { $aio_wp_security->debug_logger->log_debug("execute_backup() - get_row returned NULL for table: " . $table[0], 4); } $return .= PHP_EOL . PHP_EOL . $row2[1] . ";" . PHP_EOL . PHP_EOL; foreach ($result as $row) { $return .= 'INSERT INTO `' . $table[0] . '` VALUES('; for ($j = 0; $j < $num_fields; $j++) { $row[$j] = addslashes($row[$j]); //$row[$j] = ereg_replace( PHP_EOL, "\n", $row[$j] ); //deprecated! $row[$j] = preg_replace("/" . PHP_EOL . "/", "\n", $row[$j]); if (isset($row[$j])) { $return .= '"' . $row[$j] . '"'; } else { $return .= '""'; } if ($j < $num_fields - 1) { $return .= ','; } } $return .= ");" . PHP_EOL; } $return .= PHP_EOL . PHP_EOL; } $return .= PHP_EOL . PHP_EOL; //Check to see if the main "backups" directory exists - create it otherwise $aiowps_backup_dir = WP_CONTENT_DIR . '/' . AIO_WP_SECURITY_BACKUPS_DIR_NAME; $aiowps_backup_url = content_url() . '/' . AIO_WP_SECURITY_BACKUPS_DIR_NAME; if (!AIOWPSecurity_Utility_File::create_dir($aiowps_backup_dir)) { $aio_wp_security->debug_logger->log_debug("Creation of DB backup directory failed!", 4); return false; } //Generate a random prefix for more secure filenames $random_suffix = AIOWPSecurity_Utility::generate_alpha_numeric_random_string(10); if ($is_multi_site) { global $current_blog; $blog_id = $current_blog->blog_id; //Get the current site name string for use later $site_name = get_bloginfo('name'); $site_name = strtolower($site_name); //make alphaunermic $site_name = preg_replace("/[^a-z0-9_\\s-]/", "", $site_name); //Cleanup multiple instances of dashes or whitespaces $site_name = preg_replace("/[\\s-]+/", " ", $site_name); //Convert whitespaces and underscore to dash $site_name = preg_replace("/[\\s_]/", "-", $site_name); $file = 'database-backup-site-name-' . $site_name . '-' . current_time('Ymd-His') . '-' . $random_suffix; //We will create a sub dir for the blog using its blog id $dirpath = $aiowps_backup_dir . '/blogid_' . $blog_id; //Create a subdirectory for this blog_id if (!AIOWPSecurity_Utility_File::create_dir($dirpath)) { $aio_wp_security->debug_logger->log_debug("Creation failed of DB backup directory for the following multisite blog ID: " . $blog_id, 4); return false; } } else { $dirpath = $aiowps_backup_dir; $file = 'database-backup-' . current_time('Ymd-His') . '-' . $random_suffix; } $handle = @fopen($dirpath . '/' . $file . '.sql', 'w+'); $fw_res = @fwrite($handle, $return); if (!$fw_res) { $aio_wp_security->debug_logger->log_debug("execute_backup() - Write to DB backup file failed", 4); return false; } @fclose($handle); //zip the file if (class_exists('ZipArchive')) { $zip = new ZipArchive(); $archive = $zip->open($dirpath . '/' . $file . '.zip', ZipArchive::CREATE); $zip->addFile($dirpath . '/' . $file . '.sql', $file . '.sql'); $zip->close(); //delete .sql and keep zip @unlink($dirpath . '/' . $file . '.sql'); $fileext = '.zip'; } else { $fileext = '.sql'; } $this->last_backup_file_name = $file . $fileext; //database-backup-YYYYMMDD-HHIISS-<random-string>.zip or database-backup-YYYYMMDD-HHIISS-<random-string>.sql $this->last_backup_file_path = $dirpath . '/' . $file . $fileext; if ($is_multi_site) { $this->last_backup_file_dir_multisite = $aiowps_backup_dir . '/blogid_' . $blog_id; } $this->aiowps_send_backup_email(); //Send backup file via email if applicable $this->aiowps_delete_backup_files(); return true; }