How to create backup script with date?

Asked By 30 points N/A Posted on -
qa-featured

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.

SHARE
Answered By 20 points N/A #166241

How to create backup script with date?

qa-featured

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!

Related Questions