Backup of mysql

Tips submitted by PHPMaker users
Post Reply
Oliveira - Uibaí-Ba - Brazil

Backup of mysql

Post by Oliveira - Uibaí-Ba - Brazil »

the file is written out of the web server and then read with readfile and available for download

##backup.php"##
<?php
session_start();
$usuario_backup = $_SESSION[USER_NAME];
$file = $usuario_backup.".".sql;
system("mysqldump.exe --user $usuario_backup -pUser_Password_Database --databases $usuario_backup > d:\bkp/$usuario_backup.sql");

header('Content-Description: File Transfer');
header('Content-Type: application/sql');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: text');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile(\'d:\bkp\\'\.$file);
?>
as I am in windows, I had to copy the mysqldump.exe to the folder with the backup.php

It is assumed that was logged in a separate table by putting the username in the session.
assumed to have a bench in mysql with the same name used in the User login and User in mysql with the same name of the bank and with the necessary privileges on it.


Oliveira

Post by Oliveira »

For linux
backup.php
<?php
session_start();
$usuario_backup = $_SESSION[EW_SESSION_USER_NAMES];
system("mysqldump --user $usuario_backup -senha --databases $usuario_backup > /home/olive/bkp/$usuario_backup.sql");
system("zip -9 /home/olive/bkp/$usuario_backup.zip /home/olive/bkp/$usuario_backup.sql | rm /home/olive/bkp/$usuario_backup.sql");
header ("Location: backup2.php");
?>

backup2.php
<?php
session_start();
$usuario_backup = $_SESSION[EW_SESSION_USER_NAMES];
$file = $usuario_backup.".".zip;
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile(\'/home/olive/bkp//'\.$file);
?>


nyukuri
User
Posts: 123

Post by nyukuri »

Hello Oliveira,

I read your script about creating a backup of the database. It looks like the windows version creates a simple sql file while your linux version creates a zipped file.
How do I have to change your code to get also a zipped backup in windows?
Thank you!


oliveira
User
Posts: 45

Post by oliveira »

You just have to research how to compress the file from the command line in Windows, then run system replacing the zip compression program:
https://technet.microsoft.com/en-us/lib ... 90884.aspx


mpol_ch
User
Posts: 877
Location: Switzerland

Post by mpol_ch »

Hallo Oliveira

Can I use this code in "Custom File" (V11)? If yes, could you please give some informtation howto?

thanks
mpol_ch


Post Reply