Backup Data Sql Berdasarkan Tabel
public function backup()
{
//echo "ok";
$DbName = env('DB_DATABASE');
$get_all_table_query = "SHOW TABLES ";
$result = DB::select(DB::raw($get_all_table_query));
$prep = "Tables_in_$DbName";
/*
foreach ($result as $res) {
$tables[] = $res->$prep;
}
*/
$tables = ["barang", "barang_masuk", "bidang", "karyawan", "keranjang", "transaksi"];
$connect = DB::connection()->getPdo();
$get_all_table_query = "SHOW TABLES";
$statement = $connect->prepare($get_all_table_query);
$statement->execute();
$result = $statement->fetchAll();
$output = '';
foreach ($tables as $table) {
$show_table_query = "SHOW CREATE TABLE " . $table . "";
$statement = $connect->prepare($show_table_query);
$statement->execute();
$show_table_result = $statement->fetchAll();
$output .= "\n\n DROP TABLE IF EXISTS " . $table . ";\n\n";
foreach ($show_table_result as $show_table_row) {
$output .= "\n\n" . $show_table_row["Create Table"] . ";\n\n";
}
$select_query = "SELECT * FROM " . $table . "";
$statement = $connect->prepare($select_query);
$statement->execute();
$total_row = $statement->rowCount();
for ($count = 0; $count < $total_row; $count++) {
$single_result = $statement->fetch(\PDO::FETCH_ASSOC);
$table_column_array = array_keys($single_result);
$table_value_array = array_values($single_result);
$output .= "\nINSERT INTO $table (";
$output .= "" . implode(", ", $table_column_array) . ") VALUES (";
$output .= "'" . implode("','", $table_value_array) . "');\n";
}
}
$file_name = 'database_backup.sql';
$file_handle = fopen($file_name, 'w+');
fwrite($file_handle, $output);
fclose($file_handle);
/*
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file_name));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_name));
*/
ob_clean();
flush();
//readfile($file_name);
//unlink($file_name);
//prose upload ke server pusat
/*
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '' . server_induk() . 'sql',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('file' => new CURLFILE('/home/ullilfahri/Downloads/database_backup (1).sql')),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
*/
echo "<script>window.location='" . url('/kirim.php') . "'</script>";
Lanjut Part : 1
Muhammad Ullil Fahri
WA : 089696380422
Tautan WA : https://wa.me/message/SG4YA2XQP5FPE1