Anda di halaman 1dari 2

Backup Menggunakan SQL Command melalui PHP

1 <?php
2 $dbhost = 'localhost';
3 $dbuser = 'root';
4 $dbpass = '';
5 $koneksi = mysql_connect($dbhost, $dbuser, $dbpass);
6 if(! $koneksi )
7 {
8 die('Gagal Koneksi: ' . mysql_error());
9 }
10 $table_name = "karyawan";
11 $backup_file = "/tmp/karyawan.sql";
12 $sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
13
14 mysql_select_db('test_db');
15 $backup = mysql_query( $sql, $koneksi );
16 if(! $backup )
17 {
18 die('Gagal Backup: ' . mysql_error());
19 }
20 echo "Backup Berhasil\n";
21 mysql_close($koneksi );
22 ?>

Restore Menggunakan SQL Command melalui PHP


<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$koneksi = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $koneksi )
{
die('Gagal Koneksi: ' . mysql_error());
}
$table_name = "karyawan";
$backup_file = "/tmp/karyawan.sql";
$sql = "LOAD DATA INFILE '$backup_file' INTO TABLE $table_name";

mysql_select_db('test_db');
$restore = mysql_query( $sql, $koneksi );
if(! $restore )
{
die('Gagal load data : ' . mysql_error());
}
echo "Load data berhasil\n";
mysql_close($koneksi);
?>

Menggunakan MySQL binary mysqldump melalui PHP


<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';


$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass ".
"test_db | gzip > $backup_file";

system($command);
?>

Anda mungkin juga menyukai