Hello fellows,
How to create backup script with date? I was a website developer in a specific company here in our place and I was told by our IT department project manager to create some backup every time we have some updates with the website. I am using mysql server over php programming language, I need your assistance.
Regards,
Amanda Mitchel.
How to create backup script with date?
Hello Amanda,
The code will help you backup up your MySQL server using PHP, and the database script also adds date to the backup file name. Use this code:
Â
set_time_limit(0);
$username = 'username';
$password = 'password';
$host = 'localhost';
/**
* Use your path here – Should be an existing path and should have the write permission
*/
$dump_path = './dumps/';
$dbc = mysqli_connect($host,$username,$password);
$result = mysqli_query($dbc,'SHOW DATABASES');Â
$database_list = array();
while($row = mysqli_fetch_array($result)) {
  $database_list[] = $row['Database'];
}
foreach ($database_list as $database) {
  $dump_name = $database.'_'.date('d-m-y');
  $dump_string = 'mysqldump –host '.$host.' –user='.$username.' –password='.$password.' '.$database.' > '.$dump_path.$dump_name.'.sql';
  exec($dump_string);
  $zip_cmd = 'gzip '.$dump_path.$dump_name.'.sql';
  exec ($zip_cmd );
}
Â
Â
Hope this would be helpful. Regards!