Viewing file: backup.php (2.12 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<? require_once("includes/config.php"); require_once("includes/funciones.php");
$carpeta_backup="backups/"; $fecha=date("Y-m-d_H-i-s"); $nombre_backup_cliente="backup-$fecha.txt";
$nombre_backup_servidor="backup-ultima.txt";
//exec('mysqldump --user='.$username_CDS.' --password='.$password_CDS.' --host='.$hostname_CDS.' '.$database_CDS.' > backup.sql'); //exec('mysqldump --user='.$username_CDS.' --password='.$password_CDS.' --host='.$hostname_CDS.' '.$database_CDS);
$con = mysqli_connect ( $hostname_CDS, $username_CDS, $password_CDS, $database_CDS ); //$con->set_charset("utf8"); esto hace lio por alguna razon $return="";
$tables = array (); $result = mysqli_query ( $con, 'SHOW TABLES' ); while ( $row = mysqli_fetch_array ( $result ) ) { $tables [] = $row [0]; }
// cycle through the tables foreach ( $tables as $table ) { $result = mysqli_query ( $con, "SELECT * FROM `$table`"); $num_fields = mysqli_num_fields ( $result ); $num_rows = mysqli_num_rows( $result ); $return .= "--\n-- Structure for the table $table\n--\n\n"; $return .= "DROP TABLE IF EXISTS `$table`;"; $row2 = mysqli_fetch_array ( mysqli_query ( $con, "SHOW CREATE TABLE `$table`" ) ); $return .= "\n\n" . $row2 [1] . ";\n\n"; if ($num_rows > 0) { $return .= "--\n-- Data dump for the table $table\n--\n\n"; } $i = 0; while ( $row = mysqli_fetch_array ( $result ) ) { if ($i == 0) { $return .= "INSERT INTO `$table` VALUES\n"; } $i++; for($j = 0; $j < $num_fields; $j ++) { if ($j == 0) { $return .= '('; } $row [$j] = addslashes ( $row [$j] ); $row [$j] = mysqli_real_escape_string ( $con, $row [$j] ); if (isset ( $row [$j] )) { $return .= '"' . $row [$j] . '"'; } else { $return .= '""'; } if ($j < ($num_fields - 1)) { $return .= ','; } } if ($i < $num_rows) { $return .= "),\n"; } else { $return .= ");\n\n"; } } }
//echo $return;
$fp = fopen($carpeta_backup.$nombre_backup_servidor,"wb"); fwrite($fp,$return); fclose($fp);
header("Content-disposition: attachment; filename=".$nombre_backup_cliente); header("Content-type: text/plain"); readfile($carpeta_backup.$nombre_backup_servidor); ?>
|