Custom File: Backup database as ZIP File

Tips submitted by PHPMaker users
Post Reply
mpol_ch
User
Posts: 877
Location: Switzerland

Custom File: Backup database as ZIP File

Post by mpol_ch »

The following code can help to download the database tables as a ZIP file. I am using xampp on windows 8.1. The users of linux should do the crrespondig adjustments regarding to $command. Be aware ttat the views of tables are not going to be downloaded. This code can be inserted as custom file...

Step 1: Create a custom file under "Custom Files"
Step 2: Insert the below code into "Custom Templates->Table-Specific->Custom File->Content" under Code (Server Events, Client Scripts and Custom Templates)
Step 3: Generate the Code and visit the Menu Link. You will get prompt to download the zip file.

Enjoy it
mpol_ch

---START THE CODE HERE------

<?php
$username = EW_CONN_USER;
$password = EW_CONN_PASS;
$hostname = EW_CONN_HOST;
$dbname = EW_CONN_DB;

$dumpfname = $dbname . "_" . date("d_m_Y_H_i_s").".sql";
$command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname --user=$username ";
if ($password)
$command.= "--password=". $password ." ";
$command.= $dbname;
$command.= " > " . $dumpfname;
system($command);

// zip the dump file
$zipfname = $dbname . "_" . date("d_m_Y_H_i_s").".zip";
$zip = new ZipArchive();
if($zip->open($zipfname,ZIPARCHIVE::CREATE))
{
$zip->addFile($dumpfname,$dumpfname);
$zip->close();
}

// read zip file and send it to standard output
if (file_exists($zipfname)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($zipfname));
flush();
readfile($zipfname);
exit;
}
?>

----- END CODE HERE -----


Post Reply