.php 파일을 사용한 MySQL 덤프 생성

다음은 제가 가지고 있는 정보입니다.

MySQL과 PHP5를 사용하는 Linux 기반 시스템을 사용하고 있습니다.I can’t knowledge를 생성할 수 있어야 합니다.mysqldump.php 파일 내에서 해당 덤프를 지정한 위치에 있는 서버상의 파일에 저장합니다.

저는 PHP nooblet이기 때문에 제가 원하는 것을 할 수 있는 지원, 가이드 또는 코드를 제공해 주셨으면 합니다.이것은, 인터넷에서 리모트로 실행할 필요가 있습니다.



질문에 대한 답변



함수를 사용하여 외부 명령을 실행할 수 있습니다.

주의: 사이shell_exec()그리고.exec()두 번째 것을 선택하면 출력이 PHP 스크립트로 반환되지 않습니다.PHP 스크립트는 SQL 덤프 전체를 문자열로 가져올 필요가 없습니다.파일에 쓸 필요가 있는 것은 명령어 자체뿐입니다.


이 외부 명령어는 다음과 같습니다.

  • 을 필요로 하다mysqldump올바른 파라미터를 사용하여
  • 출력을 파일로 리다이렉트합니다.

예를 들어 다음과 같습니다.

mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql 


즉, PHP 코드는 다음과 같습니다.

exec('mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql'); 


물론 올바른 연결 정보를 사용하여 이 연결 정보를 교환할 수단은 사용자에게 달려 있습니다....그걸 가지고요.




브라우저를 통해 다운로드하기 위해 백업을 생성하는 경우 파일을 사용하지 않고도 백업을 생성할 수 있습니다.

php 함수는 mysqldump의 출력을 브라우저로 직접 리다이렉트합니다.이 예에서는 압축도 됩니다.

장점: 임시 파일을 처리할 필요가 없습니다.

단점: Windows에서는 동작하지 않습니다.대규모 데이터 세트에는 한계가 있을 수 있습니다.

<?php
$DBUSER="user"; $DBPASSWD="password"; $DATABASE="user_db";
$filename = "backup-" . date("d-m-Y") . ".sql.gz"; $mime = "application/x-gzip";
header( "Content-Type: " . $mime ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE
gzip --best";
passthru( $cmd );
exit(0); ?> 



여기를 보세요.https://github.com/ifsnop/mysqldump-php! 이것은 php로 작성된 네이티브 솔루션입니다.

composer를 사용하여 설치할 수 있으며 다음과 같이 간단합니다.

<?php
use IfsnopMysqldump as IMysqldump;
try {
$dump = new IMysqldumpMysqldump('database', 'username', 'password');
$dump->start('storage/work/dump.sql'); } catch (Exception $e) {
echo 'mysqldump-php error: ' . $e->getMessage(); }
?> 

원래 mysqldump에서 복사된 많은 옵션을 사용하여 고급 사용자를 지원합니다.

모든 옵션은 github 페이지에 설명되어 있지만, 다음과 같이 자동 설명이 가능합니다.

$dumpSettingsDefault = array(
'include-tables' => array(),
'exclude-tables' => array(),
'compress' => 'None',
'no-data' => false,
'add-drop-database' => false,
'add-drop-table' => false,
'single-transaction' => true,
'lock-tables' => false,
'add-locks' => true,
'extended-insert' => true,
'disable-foreign-keys-check' => false,
'where' => '',
'no-create-info' => false ); 



보안상의 이유로 명령어가 아닌 컨피규레이션파일로 패스워드를 지정하는 것이 좋습니다(사용자는,ps aux grep mysqldump패스워드를 참조해 주세요.

//create a temporary file $file
= tempnam(sys_get_temp_dir(), 'mysqldump');
//store the configuration options file_put_contents($file, "[mysqldump] user={$user} password="{$password}"");
//execute the command and output the result passthru("mysqldump --defaults-file=$file {$dbname}");
//delete the temporary file unlink($file); 



여기서 PMA와 같이 mysql 구조 및 데이터를 덤프할 수 있는 포괄적인 솔루션을 찾을 수 있습니다(exec, 패스스루 등을 사용하지 않음).

https://github.com/antarasi/MySQL-Dump-with-Foreign-keys

이것은 나의 향상된 dsymczuk 프로젝트의 포크입니다.

용도는 간단합니다.

<?php //MySQL connection parameters $dbhost = 'localhost'; $dbuser = 'dbuser'; $dbpsw = 'pass'; $dbname = 'dbname';
//Connects to mysql server $connessione = @mysql_connect($dbhost,$dbuser,$dbpsw);
//Set encoding mysql_query("SET CHARSET utf8"); mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
//Includes class require_once('FKMySQLDump.php');
//Creates a new instance of FKMySQLDump: it exports without compress and base-16 file $dumper = new FKMySQLDump($dbname,'fk_dump.sql',false,false);
$params = array(
//'skip_structure' => TRUE,
//'skip_data' => TRUE, );
//Make dump $dumper->doFKDump($params);
?> 

매력적으로 작용한다:-)