&1"; $ret_val = 0; /* Run the tar command, while ignoring its output */ ob_start(); passthru( $backup_cmd , $ret_val ); $tar_output = ob_get_contents(); ob_clean(); sixscan_backup_func_clear_file( $tmp_backup_dir ); /* Check for error that might've occured while running tar. Retval 0 is ok */ if ( $ret_val == 0 ){ $backed_up_filename = $temp_file_archived; return TRUE; } else{ $backed_up_filename[ 'internal_message' ] = "Failed running tar. Retval = $ret_val , Tar error: $tar_output"; $backed_up_filename[ 'user_result_message' ] = "Your hosting environment does not support the tar command, required for backup archiving."; $backed_up_filename[ 'success' ] = FALSE; return FALSE; } } /* Used to compress database backup file */ function sixscan_backup_func_compress( $filename , $compressed_filename ){ require_once ( ABSPATH . 'wp-admin/includes/class-pclzip.php' ); $zip_archive = new PclZip( $compressed_filename ); if ( $zip_archive->add( $filename , PCLZIP_OPT_REMOVE_ALL_PATH ) == 0 ){ return $zip_archive->errorInfo( true ); } return TRUE; } /* Run DB backup */ function sixscan_backup_func_db( &$backed_up_filename ){ /* Generate random seed and random file name */ srand( (double) microtime() * 1000000 ); $tmp_sql_dmp = "sql_dump" . date("Y-m-d-H-i-s") . "_" . substr( md5 ( rand( 0, 32000 )) , 0 , 10 ); $temp_sql_file_name = get_temp_dir() . $tmp_sql_dmp . ".sql"; $temp_sql_file_zipped = get_temp_dir() . $tmp_sql_dmp.".zip"; /* If a previous file was not deleted from some reason, delete it now and save the current filename */ sixscan_backup_func_delete_previous( SIXSCAN_BACKUP_LAST_DB_NAME , $temp_sql_file_zipped ); $sql_backup_result = sixscan_backup_sql( DB_HOST , DB_USER , DB_PASSWORD , DB_NAME , $temp_sql_file_name ); if ( $sql_backup_result != TRUE ){ $backed_up_filename[ 'internal_message' ] = "Could not create SQL file. Error returned: $sql_backup_result"; $backed_up_filename[ 'user_result_message' ] = "Failed creating SQL backup (Reported error: $sql_backup_result )"; $backed_up_filename[ 'success' ] = FALSE; return FALSE; } $zip_result = sixscan_backup_func_compress( $temp_sql_file_name, $temp_sql_file_zipped ); if ( $zip_result !== TRUE ) { $backed_up_filename[ 'internal_message' ] = "Could not create zip file. Error returned: $zip_result"; $backed_up_filename[ 'user_result_message' ] = "Failed creating ZIP file (write permissions at " . get_temp_dir() . " ?)"; $backed_up_filename[ 'success' ] = FALSE; return FALSE; } @unlink( $temp_sql_file_name ); $backed_up_filename = $temp_sql_file_zipped; return TRUE; } /* If, from any reason (like OOM, server reset or anything else), the script was interrupted while creating/uploading a backup, we should cleanup the archive files. */ function sixscan_backup_func_delete_previous( $backup_type , $new_backup_filename ){ $last_db_backup_name = get_option( $backup_type ); if ( is_array( $last_db_backup_name) ){ foreach ( $last_db_backup_name as $one_file ){ sixscan_backup_func_clear_file( $one_file ); } } else { sixscan_backup_func_clear_file( $last_db_backup_name ); } update_option( $backup_type , $new_backup_filename ); } function sixscan_backup_func_clear_file( $fname ){ if ( strlen( $fname ) > 0 ){ $cleanup_cmd = "rm -rf $fname 2>&1"; passthru( $cleanup_cmd ); } } /* Backup the db OR just a table */ function sixscan_backup_sql( $host , $user , $pass , $name , $sql_output_file ) { /* Dump to file every 1000 records, to avoid storing large chunks of data in memory */ define( 'DUMP_RECORD_COUNT' , 1000 ); /* Access the SQL database */ $sql_link = mysql_connect( $host , $user , $pass ); if ( $sql_link === FALSE ) return "Failed connecting to MySQL database on $host"; mysql_select_db( $name , $sql_link ); /* Getting table list */ $tables = array(); $result = mysql_query( 'SHOW TABLES' , $sql_link ); if ( $result == FALSE ) return "Failed enumerating tables"; while( $row = mysql_fetch_row( $result ) ) { $tables[] = $row[0]; } $dumped_record_count = 0; $sql_dump_data = ''; foreach( $tables as $table ){ $result = mysql_query( 'SELECT * FROM ' . $table , $sql_link ); if ( $result == FALSE ) return "Failed reading table $table"; $num_fields = mysql_num_fields( $result ); /* Create tables syntax: */ $sql_dump_data.= 'DROP TABLE IF EXISTS `' . $table . '`;'; $row2 = mysql_fetch_row( mysql_query( 'SHOW CREATE TABLE `' . $table . '`' , $sql_link ) ); $sql_dump_data .= "\n\n" . $row2[ 1 ] . ";\n\n"; /* Dump each table's data */ for ( $i = 0 ; $i < $num_fields ; $i++ ) { while( $row = mysql_fetch_row( $result ) ){ /* Prepare insert query for each row */ $sql_dump_data .= 'INSERT INTO `' . $table .'` VALUES('; for( $j = 0 ; $j < $num_fields ; $j++ ) { $row[ $j ] = addslashes( $row[ $j ] ); $row[ $j ] = str_replace( "\n" , "\\n" , $row[ $j ] ); /* Add the data itself */ if ( isset( $row[ $j ] ) ){ $sql_dump_data.= '"'.$row[$j].'"' ; } else { $sql_dump_data.= '""'; } /* Separating the values with comma (except the last one ) */ if ( $j < ( $num_fields - 1 ) ) { $sql_dump_data.= ','; } } $sql_dump_data.= ");\n"; $dumped_record_count++; if ( $dumped_record_count == DUMP_RECORD_COUNT ){ // append data to avoid large memory demands if ( file_put_contents( $sql_output_file , $sql_dump_data , FILE_APPEND ) === FALSE ) return "Failed writing data to SQL backup file $sql_output_file"; $sql_dump_data = ''; $dumped_record_count = 0; } } } $sql_dump_data.="\n\n\n"; // append data to avoid large memory demands if ( file_put_contents( $sql_output_file , $sql_dump_data , FILE_APPEND ) === FALSE ){ return "Failed writing data to SQL backup file $sql_output_file"; } $sql_dump_data = ''; } mysql_close( $sql_link ); return TRUE; } ?>